Getting the Time for Each Frame
To accurately time your animations, you need to call the QueryPerformanceCounter function
twice within the game loop: once before you start a drawing, and once after all drawing
has been completed. Both values returned contain the number of counts from the system
at the time the function was called. Because the performance counter has such a high resolution,
both of these values should be unique. You can use the difference between these
two values to determine the number of counts that has passed between the calls.
For example, you could write the following code:
LARGE_INTEGER timeStart;
LARGE_INTEGER timeEnd;
QueryPerformanceCounter(&timeStart);
Render( );
QueryPerformanceCounter(&timeEnd);
LARGE_INTEGER numCounts = ( timeEnd.QuadPart – timeStart.QuadPart )
LARGE_INTEGER timeEnd;
QueryPerformanceCounter(&timeStart);
Render( );
QueryPerformanceCounter(&timeEnd);
LARGE_INTEGER numCounts = ( timeEnd.QuadPart – timeStart.QuadPart )
Now that you’ve updated the sprite code, you have to add in the timer code. The timer
code requires three new global variables:
LARGE_INTEGER timeStart; // holds the starting count
LARGE_INTEGER timeEnd; // holds the ending count
LARGE_INTEGER timerFreq; // holds the frequency of the counter
LARGE_INTEGER timeEnd; // holds the ending count
LARGE_INTEGER timerFreq; // holds the frequency of the counter
Next, you need to make a call to QueryPerformanceFrequency to get the frequency of the
counter. You should call this function right before the main message loop:
QueryPerformanceFrequency(&timerFreq);
Finally, you need to add the calls around the Render function to QueryPerformanceCounter.
The first one should go right before the call to Render.
QueryPerformanceCounter(&timeStart);
The second call should go after the call to Render.
QueryPerformanceCounter(&timeEnd);
Immediately following the last QueryPerformanceCounter function, you must determine the
new animation rate.
anim_rate = ( (float)timeEnd.QuadPart - (float)timeStart.QuadPart ) /
timerFreq.QuadPart;
timerFreq.QuadPart;
不要用GetTickCount()了,精度不够.