1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-18 10:38:31 +02: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:
Misa 2020-04-28 14:37:58 -07:00 committed by Ethan Lee
parent 62441edbc9
commit e2fe2d4c2b

View File

@ -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<float>(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<float>(accumulator) / timesteplimit;
}
game.savestats();