added delay to run loop in order to cap to a given fps

This commit is contained in:
Bob Polis 2020-11-13 17:18:16 +01:00
parent 32ac8b4e0f
commit 502664615a

View File

@ -20,9 +20,9 @@ void Application::run() {
// measure start time for this tick // measure start time for this tick
auto start = SDL_GetTicks(); auto start = SDL_GetTicks();
// handle events // handle all events from event queue
SDL_Event event; SDL_Event event;
if (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
quit = true; quit = true;
@ -33,10 +33,11 @@ void Application::run() {
default: default:
quit = handle_event(event); quit = handle_event(event);
} }
if (quit) break;
} }
// update windows // update windows
for (const Window& window : Window::windows()) { for (Window& window : Window::windows()) {
window.update(); window.update();
} }
@ -51,11 +52,11 @@ void Application::run() {
// calculate how long this tick took, and how much is left // calculate how long this tick took, and how much is left
// if we want to maintain a steady fps // if we want to maintain a steady fps
auto elapsed = end - start; auto elapsed = end - start;
auto available = 1000 / _fps - elapsed; int available = 1000 / _fps - elapsed;
// if there's any time left, sleep that much // if there's any time left, sleep that much
if (available > 0) { if (available > 0) {
// SDL_Delay(available); SDL_Delay(available);
} }
} }
} }