Problem with GetTickCount

Started by
2 comments, last by Uphoreum 15 years, 6 months ago
I'm using GetTickCount to calculate the number of milliseconds it takes to do one frame of the game in order to do time based movement. My method is this: Find the current tick count Get the difference by subtracting the above tick count from the previous frame's tick count Do logic based on that difference I've never had a problem with this until now. I noticed that the game was not running smoothly, so I have it printing out the value of the milliseconds-per-frame variable. For some reason, it is alternating between a value of around 16 and around 30 (there isn't much being drawn). It keeps jumping between which makes the game 'jerk around' a lot. So, can anyone think of a reason for this? I tried commenting out all the code except for the drawing of milliseconds-per-frame, and I still have the issue.
Advertisement
The reason is due to timer precision. GetTickCount by default has a timer precision of roughly what you are seeing, 16 - 32ms. You will want to use a higher resolution timer, such as timeGetTime or the highest resolution timer avaliable on Windows, QueryPerformanceCounter.

Here's an example of using QPC, which involves more work but you get the most accurate timings possible. This might be overkill though.

Using timeGetTime is just like GetTickCount, but you will need to call timeBeginPeriod and timeEndPeriod if you need a resolution less than 10ms (the default). Make sure to link in the appropriate library and header file for timeGetTime if you use it!
I have it working with timeGetTime and it's running smoothly. Thanks!
Hmm... I'm using the same method in a different project that worked before (exact same timer class and everything), but now I never get a millisecond difference greater than zero. For the first few frames of the game it works, but after that I always get a time difference of zero, meaning the game doesn't run at all.

Is it possible that there is so little going on and my computer is fast enough that I'm getting a zero time difference? Or must it be an error in the code? If I grab the window and move it, which I guess causes the message loop to not exit, then the game jumps because it gets a large time difference value.

On question I have is where to put timeBeginPeriod and timeEndPeriod. Do I put them at the beginning and end of the program or right before and after I call timeGetTime? I've tried both ways and it didn't seem to make a difference.

This topic is closed to new replies.

Advertisement