From e2fe2d4c2b55be8a11dc7b5bfd8b494dc4d42676 Mon Sep 17 00:00:00 2001 From: Misa Date: Tue, 28 Apr 2020 14:37:58 -0700 Subject: [PATCH] Replace SDL_Delay() with an accumulator Alright, this is the start of the over-30-FPS patch! First things first, we'll need to make it possible to have a separate deltatime loop outside of the fixed timestep loop. And for that, we can't be using SDL_Delay(), as SDL_Delay() (as you might imagine) blocks the whole program. Instead we'll be using this thing called an accumulator. It looks at how long the previous poll took (the raw deltatime), and lets timesteps pass accordingly. On a side note, I've had to split the `time` and `timePrev` declaration each onto their own separate line, otherwise there's undefined behavior from `time` not being initialized. I use `accumulator = fmodf(...)` instead of `accumulator -= timesteplimit` because otherwise it'll fast-forward if it's behind, which is a jarring thing to see. Also in preparation for what's going to come down the over-30-FPS road, I've also added `deltatime` and `alpha`. `deltatime` is going to be used if the game is in slowdown mode, and `alpha` is going to be used for linear interpolation of animations. By the way, what was the main game loop previously (and is now the new timestep loop) is now in an extra set of curly braces, but I haven't indented it yet to reduce the noise in this commit. --- desktop_version/src/main.cpp | 43 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index b6ccbe42..07bb9356 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -302,42 +302,38 @@ int main(int argc, char *argv[]) } #endif - volatile Uint32 time, timePrev = 0; + volatile Uint32 time = 0; + volatile Uint32 timePrev = 0; + volatile Uint32 accumulator = 0; game.infocus = true; key.isActive = true; game.gametimer = 0; while(!key.quitProgram) { + timePrev = time; time = SDL_GetTicks(); // Update network per frame. NETWORK_update(); - //framerate limit to 30 - Uint32 timetaken = time - timePrev; - if(game.gamestate==EDITORMODE) + //timestep limit to 30 + const float rawdeltatime = static_cast(time - timePrev); + accumulator += rawdeltatime; + + Uint32 timesteplimit; + if (game.gamestate == EDITORMODE) { - if (timetaken < 24) - { - volatile Uint32 delay = 24 - timetaken; - SDL_Delay( delay ); - time = SDL_GetTicks(); - } - timePrev = time; - - }else{ - if (timetaken < game.gameframerate) - { - volatile Uint32 delay = game.gameframerate - timetaken; - SDL_Delay( delay ); - time = SDL_GetTicks(); - } - timePrev = time; - + timesteplimit = 24; + } + else + { + timesteplimit = game.gameframerate; } - + while (accumulator >= timesteplimit) + { + accumulator = fmodf(accumulator, timesteplimit); key.Poll(); if(key.toggleFullscreen) @@ -556,6 +552,9 @@ int main(int argc, char *argv[]) graphics.processfade(); game.gameclock(); gameScreen.FlipScreen(); + } + const float deltatime = rawdeltatime/1000.0f * 34.0f / timesteplimit; + const float alpha = static_cast(accumulator) / timesteplimit; } game.savestats();