From 502664615a8e6ca89911c156982c86aa9c47a1d3 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Fri, 13 Nov 2020 17:18:16 +0100 Subject: [PATCH] added delay to run loop in order to cap to a given fps --- src/Application.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index e287eb0..536995e 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -20,9 +20,9 @@ void Application::run() { // measure start time for this tick auto start = SDL_GetTicks(); - // handle events + // handle all events from event queue SDL_Event event; - if (SDL_PollEvent(&event)) { + while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: quit = true; @@ -33,10 +33,11 @@ void Application::run() { default: quit = handle_event(event); } + if (quit) break; } // update windows - for (const Window& window : Window::windows()) { + for (Window& window : Window::windows()) { window.update(); } @@ -51,11 +52,11 @@ void Application::run() { // calculate how long this tick took, and how much is left // if we want to maintain a steady fps auto elapsed = end - start; - auto available = 1000 / _fps - elapsed; + int available = 1000 / _fps - elapsed; // if there's any time left, sleep that much if (available > 0) { - // SDL_Delay(available); + SDL_Delay(available); } } }