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.net — http://www.reologica.se —On Friday 07 May 2004 13.06, Will McGugan wrote: