[SDL] Limiting game speed

Sweeney, Steven (FNB) SweeneS at fnb.co.uk
Mon Nov 4 03:53:03 PST 2002


Hi...

When I write SDL games, I like to limit the frame rate to around 60fps. I do
it like so,

long then;

// main game loop
while (true)
{
	then = SDL_GetTicks();

	... // routines and graphics

	while (SDL_GetTicks() < then + 16){}  // don't do anything
}

Now... the bit that I'm concerned about is the

while (SDL_GetTicks() < then + 16){}

Whilst it works and doesn't really affect the system too much,  it is
sitting there in a while loop for a bit until it reaches a point where it is
allowed to continue (ie - after 1/60th of a second has be reached).

I figured that I could do,

while (SDL_GetTicks() < then + 16)
{
	SDL_Delay(1);
}

which seems kinder to the system... but then I lose around 10fps.

Any advice?

Thanks,
Steve




More information about the SDL mailing list