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
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);
}
}
}