Frame rate cap

Hi,

I’d like to restrict the framerate of my game - when its running on the
desktop - so that it uses minimal CPU. What is the best way of doing this in
a cross-platform manner?

I’ve done it by timing the render time for the frame, then if it is quicker
than the required frame time, sleep using SDL_Delay. But SDL_Delay may not
be accurate enough on some platforms to use this way.

I’m also interested in knowing if SDL_Delay( 0 ) has the same effect on all
platforms as it does on Windows, ie. give up remaining time slice.

Thanks in advance…

Will McGugan

That’s the only way to do it in my knowledge…
I assume by “render time” you mean your game loop
time (i.e. rendering, physics, input handling, etc.)

The way its done in most games, is how you describe it below…
e.g. for a 60fps game you’ll have something like

void foo::MainGameLoop()
{
// get current time and store it in startTime

// flip buffers

// handle input

// update physics

// draw scene

// get current time and store it in endTime

// sleep for a desired amount of time if
// (endTime - startTime) is less than 1/60 (or
// whatever framerate you like)
}

As far as accuracy is concerned, I wouldn’t worry about it.
I doubt your loop’s timing will need to be more accurate
than what SDL_Delay can offer.

cheers,
Kos.> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Will McGugan
Sent: 07 May 2004 12:07
To: SDL mailing list
Subject: [SDL] Frame rate cap

Hi,

I’d like to restrict the framerate of my game - when its running on the
desktop - so that it uses minimal CPU. What is the best way of doing this in
a cross-platform manner?

I’ve done it by timing the render time for the frame, then if it is quicker
than the required frame time, sleep using SDL_Delay. But SDL_Delay may not
be accurate enough on some platforms to use this way.

I’m also interested in knowing if SDL_Delay( 0 ) has the same effect on all
platforms as it does on Windows, ie. give up remaining time slice.

Thanks in advance…

Will McGugan


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi,

I’d like to restrict the framerate of my game - when its running on
the desktop - so that it uses minimal CPU. What is the best way of
doing this in a cross-platform manner?

I’ve done it by timing the render time for the frame, then if it is
quicker than the required frame time, sleep using SDL_Delay. But
SDL_Delay may not be accurate enough on some platforms to use this
way.

On most systems, the scheduler has a granularity of approximately 10
ms, so you’re right; it’s not always accurate enough. What you can do
is something like this:

while(time_left > 0)
	if(time_left > 10)
		SDL_Delay(10);

where “time_left” is some expression involving SDL_GetTicks(), that
returns the number of ms left until it’s time to render the next
frame.

I’m also interested in knowing if SDL_Delay( 0 ) has the same
effect on all platforms as it does on Windows, ie. give up
remaining time slice.

Nope, SDL_Delay(0) does nothing on Linux and other Un*x-like platforms

  • or at least, that’s the way it was last time I looked. (Some 1.2.x
    version.)

I once suggested it should call sched_yield() or something, so it does
the same thing on all platforms, but for one reason or another,
nothing was done.

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Friday 07 May 2004 13.06, Will McGugan wrote:

I’d like to restrict the framerate of my game - when its running on the
desktop - so that it uses minimal CPU. What is the best way of doing this in
a cross-platform manner?

I’ve done it by timing the render time for the frame, then if it is quicker
than the required frame time, sleep using SDL_Delay. But SDL_Delay may not
be accurate enough on some platforms to use this way.

I’m also interested in knowing if SDL_Delay( 0 ) has the same effect on all
platforms as it does on Windows, ie. give up remaining time slice.

I wound up doing the following:

  • Create a timer that runs @ the frame rate I want
  • Create a semaphore for throttling on
  • In the timer callback, do a SDL_SemPost()
  • In the foreground code, after a frame is rendered, do a SDL_SemWait on
    that semaphore

That keeps CPU utilization at zero for that thread when there’s nothing
else going on.

–>Neil-------------------------------------------------------------------------------
Neil Bradley "Your mistletoe is no match for my T.O.W. missile!"
Synthcom Systems, Inc. - Santabot - Futurama
ICQ #29402898