From 6baffc5f6c953fbba486bbe40433e164458bc67b Mon Sep 17 00:00:00 2001 From: DaniTheSkunk <> Date: Tue, 6 Dec 2022 05:24:17 +0000 Subject: [PATCH] added timing code, not yet variable frame rate --- com/danitheskunk/skunkworks/BaseGame.java | 42 ++++++++++++------- .../skunkworks/backends/gl/Window.java | 1 - 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/com/danitheskunk/skunkworks/BaseGame.java b/com/danitheskunk/skunkworks/BaseGame.java index cd1e4c4..7714491 100644 --- a/com/danitheskunk/skunkworks/BaseGame.java +++ b/com/danitheskunk/skunkworks/BaseGame.java @@ -7,9 +7,9 @@ import com.danitheskunk.skunkworks.audio.nodes.Node; import com.danitheskunk.skunkworks.audio.nodes.SamplePlayer; import com.danitheskunk.skunkworks.audio.nodes.TTS; import com.danitheskunk.skunkworks.gfx.IRenderContext; -import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.font.IFont; +import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; import com.danitheskunk.skunkworks.nodes.NodeRoot; import org.lwjgl.glfw.GLFW; @@ -38,7 +38,7 @@ public abstract class BaseGame { /** * Create with given window size and title * - * @param windowSize the size of the game's window + * @param windowSize the size of the game's window * @param windowTitle the title of the game's window */ public BaseGame(Vec2i windowSize, String windowTitle) { @@ -89,7 +89,7 @@ public abstract class BaseGame { /** * Play sample once or looped * - * @param sample the sample to be played + * @param sample the sample to be played * @param looping loops forever if true, otherwise play once */ protected void playSample(ISample sample, boolean looping) { @@ -141,28 +141,40 @@ public abstract class BaseGame { * closes. */ public void run() { - double lastTime, currentTime, delta; + double lastTime, currentTime, delta, currentFrameTime; + + currentFrameTime = 0; lastTime = GLFW.glfwGetTime(); while(!window.shouldClose()) { currentTime = GLFW.glfwGetTime(); delta = currentTime - lastTime; lastTime = currentTime; + currentFrameTime += delta; engine.tick(); window.tick(); //todo: frame rate control - rootNode.tick(); - update(1.0 / 60.0); - var rc3d = window.renderStart3D(); - render3D(rc3d); - window.renderFinish3D(rc3d); - var rc = window.renderStart(); - renderPre(rc); - rootNode.render(rc); - render(rc); - window.renderFinish(rc); + if(currentFrameTime >= 1.0 / 60.0) { + rootNode.tick(); + update(1.0 / 60.0); + var rc3d = window.renderStart3D(); + render3D(rc3d); + window.renderFinish3D(rc3d); + var rc = window.renderStart(); + renderPre(rc); + rootNode.render(rc); + render(rc); + window.renderFinish(rc); - window.runScaler(); + window.runScaler(); + currentFrameTime -= 1.0 / 60.0; + } else { + try { + Thread.sleep(1); + } catch(InterruptedException e) { + throw new RuntimeException(e); + } + } audioEngine.refill(); } } diff --git a/com/danitheskunk/skunkworks/backends/gl/Window.java b/com/danitheskunk/skunkworks/backends/gl/Window.java index e89ca08..06a87d4 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Window.java +++ b/com/danitheskunk/skunkworks/backends/gl/Window.java @@ -452,7 +452,6 @@ public class Window extends BaseWindow { glEnd(); - glfwSwapBuffers(window); }