I find myself often doing this in my games’ loops:
do
{
last_time = SDL_GetTicks();
... handle events
... manage objects (movement, collisions, etc.)
... blit everybody to the surface
SDL_Flip(screen);
if (SDL_GetTicks() < last_time + (1000 / FPS))
SDL_Delay(last_time + (1000 / FPS) - SDL_GetTicks());
}
while (!gameover);
What this does is ensure that my game will run at the same framerate on
both my 450Mhz machine, and next year’s 50 GHz computers.
(Assuming a CPU’s speed is a mistake from the DOS world. Try playing
PacMan meant for a PS/2 on a 486 or a Pentium III.)
I think it’d be nice (and I know I can roll my own here, obviously, but
still, it’d be nice!) if there were a function that handled all of this
at once.
Perhaps something like:
void SDL_FlipDelay(SDL_Surface * screen, int fps)
{
Uint32 current_tick;
SDL_Flip(screen);
current_tick = SDL_GetTicks();
if (current_tick < (screen -> last_flip) + (1000 / fps))
SDL_Delay((screen -> last_flip) + (1000 / fps) - current_tick);
screen -> last_flip = SDL_GetTicks();
}
I don’t know how you thread-using folks feel about this, and I’m not sure
if everyone would agree to adding another member to the SDL_Surface type,
but that’s just one way to do it. (Of course, that memeber would be set to
a suitably low number when the surface gets created.)
I suppose another would be something like:
void SDL_FlipDelay(SDL_Surface * screen, int fps, Uint32 * last_flip)
{
Uint32 current_tick;
SDL_Flip(screen);
current_tick = SDL_GetTicks();
if (current_tick < *last_flip + (1000 / fps))
SDL_Delay(*last_flip + (1000 / fps) - current_tick);
*last_flip = SDL_GetTicks();
}
ie, a pass-by-reference variable which the user has to manage…
SDL_Surface * screen;
Uint32 flip_clock = 0;
int fps = 30;
…
SDL_FlipDelay(screen, fps, &flip_clock);
Anyway, what does everyone else think of it?
-bill!
PS - Noticing all of the new functions in mixerlib 1.0!
Can’t wait, hint-hint!