From e7662baaeaec8bac1fd0b3c5fdd4fefde2d8dce6 Mon Sep 17 00:00:00 2001 From: CobaltXII Date: Fri, 28 Dec 2018 15:11:21 -0500 Subject: [PATCH] Added a 60 Hz framerate cap --- src/main.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index f69b66a..67d2998 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -119,6 +119,11 @@ int main(int argc, char** argv) while (sdl_running) { + // Remember the time at the start of the frame. At the end of the + // frame, this timestamp will be used to cap the framerate. + + auto frame_start_time = std::chrono::high_resolution_clock::now(); + // Poll events. SDL_Event e; @@ -187,6 +192,17 @@ int main(int argc, char** argv) // appear on the screen. SDL_GL_SwapWindow(sdl_window); + + // Cap the framerate to 60 Hz. + + float frame_elapsed_time = std::chrono::duration(std::chrono::high_resolution_clock::now() - frame_start_time).count(); + + if (frame_elapsed_time < 1000.0f / 60.0f) + { + int frame_sleep_time = round(1000.0f / 60.0f - frame_elapsed_time); + + std::this_thread::sleep_for(std::chrono::milliseconds(frame_sleep_time)); + } } // Destroy all OpenGL related objects.