I was trying to create a gameloop with fps dependent on the speed of its iterations. To achieve this I wanted to use a platform specific timer that (in case of windows) used the timeGetTime function () to calculate how much time has passed since the last iteration. But I found that the time it costs to call this function is already quite a lot (for a computer). Now I'm wondering if this is the right approach.

I created a simple test that looks like this:

Timer timer(); for (int i=0; i < 60; i++) cout << timer->get_elt() << endl; delete timer; 

The timer class looks like this: (begin is a DWORD)

Timer::Timer() { begin = timeGetTime(); } int Timer::get_elt() { return timeGetTime() - begin; } 

Not very interesting, but here is a example of the result:

0 0 1 3 4 14 15 15 15 16 16 17 17 17 17 18 19 19 19 19 20 20 20 20 20 21 21 21 21 21 22 22 22 22 22 22 23 23 23 25 38 39 39 55 56 56 66 68 68 69 71 71 72 73 73 73 73 73 74 74 

I was expecting this to take about 10 milliseconds at most, but on average it took about 64. What surprised me most about it was how erratic the results were. Sometimes it prints up to 7 times the same number, whereas at other times there are gaps of 12 milliseconds between iterations. I realize this is also because the timer is not accurate, but still. As far as I know your pc should execute this program as fast as it possibly can, is that even true? If you want to run your game at say 60 fps, you'd have about 16 milliseconds for every loop, and if calling the timer alone takes about 2 milliseconds on average every time, and you still need to process input, update, and render, how is that even possible? So what should I do here, is timeGetTime something you could use in a gameloop (it's been suggested a lot), or should I think of another function?

4

2 Answers

I would suggest using the QueryPerformanceCounter instead

(v=vs.85).aspx

6

The Timers from Windows Multimedia API is a good choice for animation, games, etc. The have greatest precision on Windows Platform. Qt use and qualifies this timers also as precise ones.

On Windows, Qt will use Windows's Multimedia timer facility (if available) for Qt::PreciseTimer and normal Windows timers for Qt::CoarseTimer and Qt::VeryCoarseTimer.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.