Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LWJGL 3 ups & fps counter goes crazy  (Read 5137 times)
0 Members and 1 Guest are viewing this topic.
Offline Aboogesnickle

Senior Newbie


Exp: 1 month or less



« Posted 2016-01-18 16:47:06 »

When the window is moved or resized the fps counter and ups counter goes crazy.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
   public static void run() {
      long lastTime = System.nanoTime();
      long now;
      long timer = System.currentTimeMillis();
      double delta = 0;
      int frames = 0;
      int updates = 0;
      while (running) {
         if (Window.shouldClose() == true) {
            stop();
         }
         now = System.nanoTime();
         delta += (now - lastTime) / Final.NS;
         lastTime = now;
         while (delta >= 1) {
            update();
            updates++;
            delta--;
         }
         render();
         frames++;
         if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            System.out.println(updates + " ups, " + frames + " fps");
            updates = 0;
            frames = 0;
         }
      }
      dispose();
   }


This is the game loop it is suppose to run the update method 60 times a second, and the render method has no limit per second.
The counters work fine until you move or resize the window.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
package engine;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWCursorPosCallback;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWMouseButtonCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryUtil;

import engine.input.Cursor;
import engine.input.Keyboard;
import engine.input.Mouse;
import engine.util.Final;

public class Window {

   private static String title = Final.TITLE;
   private static long id;
   private static int width = Final.WINDOW_WIDTH;
   private static int height = Final.WINDOW_HEIGHT;
   private static boolean fullscreen = Final.WINDOW_FULLSCREEN;
   private static boolean vsync = Final.WINDOW_VSYNC;
   private static boolean visible = Final.WINDOW_VISIBILITY;
   private static boolean resizable = Final.WINDOW_RESIZABLE;

   private static final GLFWFramebufferSizeCallback FRAMEBUFFER_SIZE_CALLBACK = new GLFWFramebufferSizeCallback(){
      @Override
      public void invoke(long window, int width, int height) {
         GL11.glViewport(0, 0, width, height);
      }
   };
   private static final GLFWWindowSizeCallback WINDOW_SIZE_CALLBACK = new GLFWWindowSizeCallback(){
      @Override
      public void invoke(long window, int width, int height) {
         Window.width = width;
         Window.height = height;
      }
   };
   private static final GLFWKeyCallback KEY_CALLBACK = new Keyboard();;
   private static final GLFWMouseButtonCallback MOUSE_BUTTON_CALLBACK = new Mouse();
   private static final GLFWCursorPosCallback CURSOR_POS_CALLBACK = new Cursor();

   public Window() {
      if (GLFW.glfwInit() != GLFW.GLFW_TRUE) {
         throw new IllegalStateException("Unable to initialize GLFW");
      }
      GLFW.glfwDefaultWindowHints();
      GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
      GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);
      GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
      GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE);
      GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, visible ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
      GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, resizable ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
      id = GLFW.glfwCreateWindow(width, height, title, fullscreen ? GLFW.glfwGetPrimaryMonitor() : MemoryUtil.NULL, MemoryUtil.NULL);
      if (id == MemoryUtil.NULL) {
         GLFW.glfwTerminate();
         throw new RuntimeException("Failed to create the GLFW window");
      }
      GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
      GLFW.glfwSetWindowPos(id, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
      GLFW.glfwSwapInterval(vsync ? 1 : 0);
      GLFW.glfwMakeContextCurrent(id);
      FRAMEBUFFER_SIZE_CALLBACK.set(id);
      WINDOW_SIZE_CALLBACK.set(id);
      KEY_CALLBACK.set(id);
      MOUSE_BUTTON_CALLBACK.set(id);
      CURSOR_POS_CALLBACK.set(id);
      GL.createCapabilities();
   }

   public static void update() {
      GLFW.glfwSwapBuffers(id);
      GLFW.glfwPollEvents();
   }

   public static void dispose() {
      GLFW.glfwDestroyWindow(id);
      FRAMEBUFFER_SIZE_CALLBACK.release();
      WINDOW_SIZE_CALLBACK.release();
      KEY_CALLBACK.release();
      MOUSE_BUTTON_CALLBACK.release();
      CURSOR_POS_CALLBACK.release();
      GLFW.glfwTerminate();
   }

   public static void setWidth(int width) {
      Window.width = width;
   }

   public static int getWidth() {
      return width;
   }

   public static void setHeight(int height) {
      Window.height = height;
   }

   public static int getHeight() {
      return height;
   }

   public static void setTitle(String title) {
      Window.title = title;
   }

   public static void setVsync(boolean vsync) {
      Window.vsync = vsync;
      GLFW.glfwSwapInterval(vsync ? 1 : 0);
   }

   public static void setVisiblity(boolean visible) {
      Window.visible = visible;
      if(visible){
         GLFW.glfwShowWindow(id);
      }else{
         GLFW.glfwHideWindow(id);
      }
   }

   public static long getID() {
      return id;
   }

   public static boolean shouldClose() {
      return GLFW.glfwWindowShouldClose(id) == GLFW.GLFW_TRUE;
   }
}


This is the window class I am using. The only thing I can think of is that LWJGL's callbacks are messing it up. But I do not know why they would.
Offline KudoDEV

JGO Ninja


Medals: 79
Exp: 6 years


Game Dev Hobbyist


« Reply #1 - Posted 2016-01-18 17:15:15 »

When you drag or resize the window the program pauses. So when you stop dragging/resizing the elapsed time is greater.

Try capping the delta.

Offline Aboogesnickle

Senior Newbie


Exp: 1 month or less



« Reply #2 - Posted 2016-01-18 17:25:25 »

When you drag or resize the window the program pauses. So when you stop dragging/resizing the elapsed time is greater.

Wouldn't it just mess up for one iteration, and then start working right again? The counter does not work after the the resize or move.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KudoDEV

JGO Ninja


Medals: 79
Exp: 6 years


Game Dev Hobbyist


« Reply #3 - Posted 2016-01-18 17:30:27 »

I do this in my loop instead:
1  
2  
3  
4  
   
delta = (now - lastTime) / Final.NS;
lastTime = now;
update(delta);


Offline thedanisaur

JGO Knight


Medals: 59



« Reply #4 - Posted 2016-01-18 17:32:29 »

That's the problem with saying, "Does not work"/"Goes Crazy". It doesn't mean anything to us. What do you mean doesn't work? It stops updating entirely, seems to be fast, seems to be slow?

Every village needs an idiot Cool
Offline Aboogesnickle

Senior Newbie


Exp: 1 month or less



« Reply #5 - Posted 2016-01-18 17:38:43 »

That's the problem with saying, "Does not work"/"Goes Crazy". It doesn't mean anything to us. What do you mean doesn't work? It stops updating entirely, seems to be fast, seems to be slow?

It runs at 60 ups, 45157442 fps until I move it and then it goes to 63 ups, 3 fps. So the ups get higher and the fps get lower.
Offline KudoDEV

JGO Ninja


Medals: 79
Exp: 6 years


Game Dev Hobbyist


« Reply #6 - Posted 2016-01-18 17:48:12 »

It runs at 60 ups, 45157442 fps until I move it and then it goes to 63 ups, 3 fps. So the ups get higher and the fps get lower.

Well that's expected. If you don't want this to happen when you drag/resize then you have to cap your delta.

What is this loop technique called?


Offline Aboogesnickle

Senior Newbie


Exp: 1 month or less



« Reply #7 - Posted 2016-01-18 18:01:47 »

its a standard game loop

while(running){
     update();
     render();
}
dispose();


Final.NS = 1000000000.0 / UPS_CAP;
UPS_CAP = 60;
Offline KudoDEV

JGO Ninja


Medals: 79
Exp: 6 years


Game Dev Hobbyist


« Reply #8 - Posted 2016-01-18 18:08:35 »

Your updates are independent of the frames the way you have it. There should be 1 update in a standard game-loop.

try something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
public static void run() {
      long lastTime = System.nanoTime();
      long now;
      long timer = System.currentTimeMillis();
      double delta = 0;
      int frames = 0;
      while (running) {
         if (Window.shouldClose() == true) {
            stop();
         }

         now = System.nanoTime();
         delta = (now - lastTime) / (1000000000f / targetFps);
         lastTime = now;

         update(delta); //interpolate using the delta
         render();

         frames++;
         if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            System.out.println(frames + " fps");
            frames = 0;
         }
      }
      dispose();
   }

Offline KaiHH

JGO Kernel


Medals: 472



« Reply #9 - Posted 2016-01-18 19:16:52 »

You can keep rendering separate from window message processing (i.e. moving/resizing, etc.) by using the main thread to do only glfwWaitEvents() and a separate thread for the OpenGL calls and swap buffers.
This way the scene still renders even when you resize/move the window.
But beware that this means multithreading, which can become difficult.

You can see a demo of this in the lwjgl3 GitHub repository and the lwjgl3-demos repository.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (107 views)
2017-09-30 02:57:34

theagentd (134 views)
2017-09-26 18:23:31

cybrmynd (244 views)
2017-08-02 12:28:51

cybrmynd (236 views)
2017-08-02 12:19:43

cybrmynd (237 views)
2017-08-02 12:18:09

Sralse (251 views)
2017-07-25 17:13:48

Archive (863 views)
2017-04-27 17:45:51

buddyBro (1006 views)
2017-04-05 03:38:00

CopyableCougar4 (1564 views)
2017-03-24 15:39:42

theagentd (1372 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!