From c54e6323a74a812abc648389a48501fc5c0d7a32 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Fri, 17 Jun 2016 16:32:59 +1000 Subject: [PATCH] Use GLFW swap interval for force vsync to monitor --- src/dengine.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/dengine.cpp b/src/dengine.cpp index 2fb6b80..3fc2745 100644 --- a/src/dengine.cpp +++ b/src/dengine.cpp @@ -92,14 +92,19 @@ int main() glfwSetWindowUserPointer(window, static_cast(&game)); + f32 startTime = static_cast(glfwGetTime()); f32 secondsElapsed = 0.0f; // Time between current frame and last frame +#if 0 // TODO(doyle): Get actual monitor refresh rate - i32 monitorRefreshHz = 60; - f32 targetSecondsPerFrame = 1.0f / static_cast(monitorRefreshHz); - f32 startTime = static_cast(glfwGetTime()); - - //glfwSwapInterval(1); + i32 monitorRefreshHz = 60; + f32 targetSecondsPerFrame = 1.0f / static_cast(monitorRefreshHz); +#else + // TODO(doyle): http://gafferongames.com/game-physics/fix-your-timestep/ + // NOTE(doyle): Prevent glfwSwapBuffer until a vertical retrace has + // occurred, i.e. limit framerate to monitor refresh rate + glfwSwapInterval(1); +#endif /* Main game loop */ while (!glfwWindowShouldClose(window)) @@ -120,15 +125,17 @@ int main() /* Swap the buffers */ glfwSwapBuffers(window); - f32 endTime = static_cast(glfwGetTime()); + f32 endTime = static_cast(glfwGetTime()); secondsElapsed = endTime - startTime; +#if 0 // TODO(doyle): Busy waiting, should sleep while (secondsElapsed < targetSecondsPerFrame) { endTime = static_cast(glfwGetTime()); secondsElapsed = endTime - startTime; } +#endif LOCAL_PERSIST f32 titleUpdateFrequencyInSeconds = 0.5f;