[gameprogrammer] Re: game timers
- From: David Olofson <david@xxxxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Tue, 25 Oct 2005 00:53:32 +0200
On Monday 24 October 2005 17.24, Josh Stewart wrote:
> Does anyone know if QueryPerformanceCounter and
> QueryPerformanceFrequency are the best way to do timers to control
> the game loop in a win32 / glut / opengl game? I am developing under
> MSVC .NET (2003).
>
> Is there an alternative that will integrate quickly into my code
> base, that might be more portable?
Portable across Windows systems (including certain versions running on
laptops with CPU clock throttling), and/or to other platforms, such
as Mac OS X, Linux, BeOS etc?
AFAIK, the Win32 multimedia timers are more reliable, but don't take
my word for it; Windows is not my primary target platform...
On Un*x platforms (maybe including Mac OS X), I suppose gettimeofday()
will do the job. It seems to provide at least millisecond accuracy on
most platforms.
> Does anyone have any sample code?
SDL (Simple DirectMedia Layer; http://www.libsdl.org ) runs on a wide
range of platforms and seems to get it right everywhere, so you might
want to have a look at how it implements SDL_GetTicks() on the
platforms you're interested in.
> Also how do I go about debugging my code with such timers.. it seems
> that as i step through code in my update cycle, my timer is still
> running away.
> If I am looking at code that is say, updating some physics based on
> the elapsed time since the last frame, then the elaped time is much
> larger than usual.. Is there a way around this?
Well, the point with this approach is to lock logic time to wall clock
time, and that's what it tries to do at all times - even if the
rendering frame rate drops to a few minutes per frame or something
while you're debugging...!
One easy hack is to disable this timing code when debugging, and just
assume a fixed rendering frame rate of somewhere around what you're
getting when running full speed on your system. Of course, it won't
accurately simulate the game running at full speed, but it's at least
a bit closer.
An more sophisticated while probably even simpler alternative is to
just cap the delta times before feeding them to the game logic;
if(dt > 100)
dt = 100;
advance_game_logic(dt);
That way, you can have the game logic believe it's running at 10 fps
(in this example) minimum, regardless of how long you stall the
process, and still have it operate normally when you run it full
speed. This also has the bonus of dealing somewhat nicely with the
operating system freezing the game for a while and stuff like that.
Just don't set the limit too high, because that'll make the game logic
slow down on machines that run at an actual frame rate lower than
your limit! :-)
//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 ---
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- References:
- [gameprogrammer] game timers
- From: Josh Stewart
Other related posts:
- » [gameprogrammer] game timers
- » [gameprogrammer] Re: game timers
- [gameprogrammer] game timers
- From: Josh Stewart