mirror of
https://github.com/TerryCavanagh/VVVVVV.git
synced 2024-12-23 10:09:43 +01:00
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.
This commit is contained in:
parent
62441edbc9
commit
e2fe2d4c2b
1 changed files with 21 additions and 22 deletions
|
@ -302,42 +302,38 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
volatile Uint32 time, timePrev = 0;
|
volatile Uint32 time = 0;
|
||||||
|
volatile Uint32 timePrev = 0;
|
||||||
|
volatile Uint32 accumulator = 0;
|
||||||
game.infocus = true;
|
game.infocus = true;
|
||||||
key.isActive = true;
|
key.isActive = true;
|
||||||
game.gametimer = 0;
|
game.gametimer = 0;
|
||||||
|
|
||||||
while(!key.quitProgram)
|
while(!key.quitProgram)
|
||||||
{
|
{
|
||||||
|
timePrev = time;
|
||||||
time = SDL_GetTicks();
|
time = SDL_GetTicks();
|
||||||
|
|
||||||
// Update network per frame.
|
// Update network per frame.
|
||||||
NETWORK_update();
|
NETWORK_update();
|
||||||
|
|
||||||
//framerate limit to 30
|
//timestep limit to 30
|
||||||
Uint32 timetaken = time - timePrev;
|
const float rawdeltatime = static_cast<float>(time - timePrev);
|
||||||
if(game.gamestate==EDITORMODE)
|
accumulator += rawdeltatime;
|
||||||
|
|
||||||
|
Uint32 timesteplimit;
|
||||||
|
if (game.gamestate == EDITORMODE)
|
||||||
{
|
{
|
||||||
if (timetaken < 24)
|
timesteplimit = 24;
|
||||||
{
|
}
|
||||||
volatile Uint32 delay = 24 - timetaken;
|
else
|
||||||
SDL_Delay( delay );
|
{
|
||||||
time = SDL_GetTicks();
|
timesteplimit = game.gameframerate;
|
||||||
}
|
|
||||||
timePrev = time;
|
|
||||||
|
|
||||||
}else{
|
|
||||||
if (timetaken < game.gameframerate)
|
|
||||||
{
|
|
||||||
volatile Uint32 delay = game.gameframerate - timetaken;
|
|
||||||
SDL_Delay( delay );
|
|
||||||
time = SDL_GetTicks();
|
|
||||||
}
|
|
||||||
timePrev = time;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (accumulator >= timesteplimit)
|
||||||
|
{
|
||||||
|
accumulator = fmodf(accumulator, timesteplimit);
|
||||||
|
|
||||||
key.Poll();
|
key.Poll();
|
||||||
if(key.toggleFullscreen)
|
if(key.toggleFullscreen)
|
||||||
|
@ -556,6 +552,9 @@ int main(int argc, char *argv[])
|
||||||
graphics.processfade();
|
graphics.processfade();
|
||||||
game.gameclock();
|
game.gameclock();
|
||||||
gameScreen.FlipScreen();
|
gameScreen.FlipScreen();
|
||||||
|
}
|
||||||
|
const float deltatime = rawdeltatime/1000.0f * 34.0f / timesteplimit;
|
||||||
|
const float alpha = static_cast<float>(accumulator) / timesteplimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
game.savestats();
|
game.savestats();
|
||||||
|
|
Loading…
Reference in a new issue