Working in C++ on a Mac; developing for X on Unix.
Is there a canonical way to create a frame timer for game loops? I assume some things about it are OS specific, but are there some general strategies that are considered standard?
So far, I've been using the really basic method:
while (game_hasnt_quit)
{
run_a_frame();
while ( frame_duration_hasnt_passed() ) {}
}
But this uses 100% of my CPU. I'd like a solution that uses a minimal amount of resources. One thing I considered was this:
while (game_hasnt_quit)
{
time_t start = get_start_time();
run_a_frame();
time_t end = get_end_time();
do_nothing_for( frame_duration - (end - start) );
}
But I can't find an appropriate do_nothing_for(milliseconds) function. Does one exist?
Or is there some other, better way altogether to make timers?
Is there a standard timer strategy?
Started by jcb, Apr 13 2006 08:04 PM
7 replies to this topic
#1
Posted 13 April 2006 - 08:04 PM
#2
Posted 13 April 2006 - 08:11 PM
games always (there are exceptions but...) use all the cpu power they get.
The exceptions mentioned are usually card games and similar things (where most of the time the game waits for the user to click something, and there isn't even animation on the screen).
These are event driven, and thus the "do nothing" comes quite natural from the event system.
The exceptions mentioned are usually card games and similar things (where most of the time the game waits for the user to click something, and there isn't even animation on the screen).
These are event driven, and thus the "do nothing" comes quite natural from the event system.
#3
Posted 13 April 2006 - 09:26 PM
Maby try sleep(0). I guess it is available in one form or another.
#4
Posted 13 April 2006 - 11:05 PM
geon said:
Maby try sleep(0). I guess it is available in one form or another.
To address the original post, one of the many ways to 'do nothing for x millisec' would be select, as in http://unixhelp.ed.a...an-cgi?select+2
#5
Posted 14 April 2006 - 04:48 PM
I found usleep() in unistd.h. Microsecond sleeping seems an elegant. Activity Monitor reports minimal CPU usage from this, so my guess is that the program actually is "doing nothing" during the sleep intervals.
#6
Posted 15 April 2006 - 08:24 AM
You could use getitimer(), setitimer(), to get a true interval timer instead of sleeping. You need to tune the sleep value yourself depending on how long your computations took, which is not always ideal.
#7
Posted 17 April 2006 - 09:36 AM
Don't assume the duration you set on a sleep is accurate ... Sleep( 0 ) or POSIX equivalent is the best plan as it simply yields and you won't end up with a really variable frame rate :)
#8
Posted 24 April 2006 - 09:30 PM
This is a fairly common game loop: http://www.iguanadem...hp?view=FixLoop
You can add
if (!numLoops) Sleep(0);
or Sleep(10) in there in case you have a terribly high framerate. Check Brad Wardell's comments on GPU throttling in http://www.gamasutra...ardell_03.shtml
I don't know the unix and mac versions of these calls.
You can add
if (!numLoops) Sleep(0);
or Sleep(10) in there in case you have a terribly high framerate. Check Brad Wardell's comments on GPU throttling in http://www.gamasutra...ardell_03.shtml
I don't know the unix and mac versions of these calls.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











