|
@@ -13,7 +13,6 @@ public class Engine
|
|
|
|
|
|
private static long nanosPerTick = 10_000_000;
|
|
|
private static final int MAX_TICKS_PER_FRAME = 20;
|
|
|
- private static long nanosPerFrame = 1000000000l / 60l;
|
|
|
|
|
|
private static Timer fpsTimer;
|
|
|
private static Timer tpsTimer;
|
|
@@ -80,13 +79,13 @@ public class Engine
|
|
|
});
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
- glfwSwapInterval(1);
|
|
|
+ glfwSwapInterval(2);
|
|
|
|
|
|
GL.createCapabilities();
|
|
|
|
|
|
renderer = new Renderer(width, height);
|
|
|
- fpsTimer = new Timer(60);
|
|
|
- tpsTimer = new Timer(1_000_000_000l / nanosPerTick);
|
|
|
+ fpsTimer = new Timer(60, 0.0f);
|
|
|
+ tpsTimer = new Timer(1_000_000_000l / nanosPerTick, height * 0.25f);
|
|
|
|
|
|
glfwSetFramebufferSizeCallback(window, (w, fwidth, fheight) ->
|
|
|
{
|
|
@@ -97,95 +96,48 @@ public class Engine
|
|
|
glfwShowWindow(window);
|
|
|
}
|
|
|
|
|
|
- private static void sleep(long nanos)
|
|
|
- {
|
|
|
- if(nanos < 0)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- long end = System.nanoTime() + nanos - 10000;
|
|
|
- try
|
|
|
- {
|
|
|
- Thread.sleep(nanos / 1_000_000);
|
|
|
- }
|
|
|
- catch(InterruptedException ex)
|
|
|
- {
|
|
|
- }
|
|
|
- int i = 0;
|
|
|
- while(System.nanoTime() < end)
|
|
|
- {
|
|
|
- i++;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private static void loop(IGame game)
|
|
|
{
|
|
|
- long newTime = System.nanoTime();
|
|
|
- long oldTime;
|
|
|
+ long oldTime = System.nanoTime();
|
|
|
long lag = 0;
|
|
|
- long frameLag = 0;
|
|
|
- long lastFrame = 0;
|
|
|
|
|
|
while(!glfwWindowShouldClose(window))
|
|
|
{
|
|
|
- oldTime = newTime;
|
|
|
- newTime = System.nanoTime();
|
|
|
+ long newTime = System.nanoTime();
|
|
|
lag += newTime - oldTime;
|
|
|
- frameLag += newTime - oldTime;
|
|
|
+ oldTime = newTime;
|
|
|
|
|
|
- if(lag >= nanosPerTick || frameLag >= nanosPerFrame)
|
|
|
+ int ticksPerFrame = 0;
|
|
|
+ while(lag >= nanosPerTick)
|
|
|
{
|
|
|
- int ticksPerFrame = 0;
|
|
|
- while(lag >= nanosPerTick)
|
|
|
- {
|
|
|
- lag -= nanosPerTick;
|
|
|
+ lag -= nanosPerTick;
|
|
|
|
|
|
- tpsTimer.update();
|
|
|
- KeyHandler.tick();
|
|
|
- game.tick();
|
|
|
-
|
|
|
- ticksPerFrame++;
|
|
|
+ tpsTimer.update();
|
|
|
+ GamepadHandler.tick();
|
|
|
+ KeyHandler.tick();
|
|
|
+ game.tick();
|
|
|
|
|
|
- if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
|
|
|
- {
|
|
|
- long skip = lag / nanosPerTick;
|
|
|
- lag -= skip * nanosPerTick;
|
|
|
- if(skip > 0)
|
|
|
- {
|
|
|
- System.out.println("skipped " + skip + " game ticks " + lag);
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ ticksPerFrame++;
|
|
|
|
|
|
- if(frameLag >= nanosPerFrame)
|
|
|
+ if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
|
|
|
{
|
|
|
- frameLag -= nanosPerFrame;
|
|
|
- // make sure no frames are rendered immediately after each other
|
|
|
- // this happens if the game tick takes too long
|
|
|
- if(lastFrame + nanosPerFrame - 1000000 < System.nanoTime())
|
|
|
+ long skip = lag / nanosPerTick;
|
|
|
+ lag -= skip * nanosPerTick;
|
|
|
+ if(skip > 0)
|
|
|
{
|
|
|
- lastFrame = System.nanoTime();
|
|
|
-
|
|
|
- game.renderTick(renderer, (float) lag / nanosPerTick);
|
|
|
- tpsTimer.draw(renderer);
|
|
|
- fpsTimer.draw(renderer);
|
|
|
- fpsTimer.update();
|
|
|
-
|
|
|
- glfwSwapBuffers(window);
|
|
|
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
+ System.out.println(String.format("skipped %d game ticks %d", skip, lag));
|
|
|
}
|
|
|
+ break;
|
|
|
}
|
|
|
-
|
|
|
- glfwPollEvents();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- // wait until next frame
|
|
|
- long waitingTime = Math.min(nanosPerFrame - frameLag, nanosPerTick - lag);
|
|
|
- sleep(waitingTime);
|
|
|
}
|
|
|
+
|
|
|
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
+ game.renderTick(renderer, (float) lag / nanosPerTick);
|
|
|
+ tpsTimer.draw(renderer);
|
|
|
+ fpsTimer.draw(renderer);
|
|
|
+ fpsTimer.update();
|
|
|
+ glfwSwapBuffers(window);
|
|
|
+ glfwPollEvents();
|
|
|
}
|
|
|
game.onStop();
|
|
|
}
|
|
@@ -215,12 +167,7 @@ public class Engine
|
|
|
{
|
|
|
return fpsTimer.getCallsPerSecond();
|
|
|
}
|
|
|
-
|
|
|
- public static void setMaxFramesPerSecond(int max)
|
|
|
- {
|
|
|
- nanosPerFrame = 1_000_000_000 / max;
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
public static void setRenderFramesPerSecond(boolean active)
|
|
|
{
|
|
|
fpsTimer.setActive(active);
|