Advertisement

Timers in games

Started by October 27, 2000 08:32 AM
1 comment, last by openzelda 24 years, 2 months ago
I am currently working on a project called OpenZelda, its a game which uses directX with the Win32 API. I have a problem, until recently the game was using the standard windows timer to draw each frame, using the WM_TIMER message. Since then I changed the program to use a high-reolution timer, I got the code out of a book on DirectX 3, it goes like this: TIMECAPS caps; timeGetDevCaps( ∩︀, sizeof(caps) ); gapp.ticksPerSecond = 1000 / caps.wPeriodMin; timeBeginPeriod(1000 / gapp.ticksPerSecond ); gapp.timerid = timeSetEvent( 10,10,TimerFunc,0,(UINT)TIME_PERIODIC ); And then there is a callback called TimerFunc which sends a WM_USER message which actually draws each frame. this did make my game run about 6 times faster, and it looks great on my computer, but I have noticed that it runs at drastically different speeds on other computers, obviously this is bad. When it was using the windows timer it ran at pretty much the same speed on a P120, now it runs slow as hell. does anyone know of a better way of implementing high-res timers? or could you tell me a way to ''level out'' the above timer to make it run at the same speed on any computer? -thanks
I use the windows API function DWORD GetTickCount() to get the clock cycle (in milliseconds). I get a start time and then when I want to test I subtract the start time from the current time to see how much time has elapsed. I use it to lock in framerates at the end of each game loop like this:
    DWORD tick_count = GetTickCount();////////////////////// main loop here //////////////////////// lock to 60fpswhile((GetTickCount() - tick_count) > 60);    

Try using GetTickCount() and then when time has elapsed, generate your own message via SendMessage() or PostMessage(). Hope this helps.

==============================
"Need more eeenput..."

- #5, "Short Circuit"
==============================

Drew Sikora
Executive Producer
GameDev.net

Advertisement
if you need really exact time, look at this:
Note:

timescale == 0.01 -> 1/100 sec.
timescale == 0.001 -> 1/1000 sec.
and so on.

void GetPerformanceFrequency(DWORD &freq) must be called once to get the PerformanceCounter's frequency. Then pass the freq to the other function.


The PerformanceCounter is *very* exact. Much exacter than GetTickCount().
BTW: ignore the pragma's

//---------------------------------------------------------------
#include
#pragma hdrstop

#include "PerformanceTime.h"
//---------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------
static DWORD ticks;
//---------------------------------------------------------------
void GetPerformanceFrequency(DWORD &freq)
{
QueryPerformanceFrequency((LARGE_INTEGER *) &freq);
}
//---------------------------------------------------------------
DWORD GetPerformanceTickCount(DWORD freq, float timescale)
{
if (freq)
{
QueryPerformanceCounter((LARGE_INTEGER *) &ticks);
return ticks / (freq * timescale);
}
else return GetTickCount();
}


Edited by - Wunibald on October 27, 2000 10:07:21 AM
(Computer && !M$ == Fish && !Bike)

This topic is closed to new replies.

Advertisement