🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How can I create a very accurate timer with very small intervals?(win32 platform)

Started by
4 comments, last by GameDev.net 24 years, 6 months ago
Look into QueryPerformanceFrequency() and QueryPerformanceCounter() in the Win32 API. timeGetTime() is a less accurate method, but still gives you resolution of 1 ms in Windows 9x (default 5 ms in NT). The frequency of the performance counter is usually 1.19 MHz, which is 0.84 microsecond resolution. Not bad, huh?
Advertisement
He was asking about a timer not functions that give you the current time. You can create a high resolution multimedia timer with a resolution of 1ms.
Multimedia timer 1ms interval ?
I created this
setTimer(hWnd,1,1,NULL);
and this
WM_TIMER:
x++;
break;

Normaly will x be 1000 after 1 sec but it is rater 4 ??????
I need at least 200 updates each sec. Because I use in my meanloop(called around 100 times/sec):
if (keys[VK_UP])
world.movez(x);
if (keys[VK_DOWN])
world.movez(-x);
if (keys[VK_LEFT])
world.rotateY(x/constant);
if (keys[VK_RIGHT])
world.rotateY(-x/constant);

And I have a framrate of around 100. So if the update speed is slower then this framerate will it look like there is a slower framrate.

How can I create a very accurate timer with very small intervals?(win32 platform)
I forgot(after the key if's):
x=0;
Ok, as far as I know, there are THREE common timer systems on the windows platform. The standard Windows timer, the MultiMedia timer, and the High Performance timer. The first has a resolution of 55ms (useless), the second SUPPOSEDLY 1ms (but it has a higher latency than that), and the third (the one I recommend) is REALLY more like getting the time than a built in timer.

I''m currently writting a gamedev.net article on this subject, but I''ll give the you basic idea. What you do is layer your own concept of a timer on top of calls to QueryPerformanceCounter() in the message loop. Each iteration (or after a fixed number of iterations) through the message loop, you query the timer, then check to see if each of your timers has elapsed its time interval. If it has, post ITS associated message. This makes it behave just like the Multimedia timer, but with much greater accuracy, and only about 20-200 lines of code (depending on the level of sophistication you need).

This topic is closed to new replies.

Advertisement