BOOL QueryPerformanceCounter( LARGE_INTEGER *lpPerformanceCount );
lpPerformanceCount is a pointer to a LARGE_INTEGER that will be filled with the current value of the performance counter.
Note that the existence of a high performance timer is completely dependent
on hardware. If QueryPerformanceCounter() returns false, then no
high performance timer exists on the system.
BOOL QueryPerformanceFrequency( LARGE_INTEGER *lpFrequency);
lpFrequency is a pointer to a LARGE_INTEGER
that will be filled with the number of timer increments that occur in one
second.
void InitTimer(const int updateFreq,
LARGE_INTEGER
& timerDiff)
{
LARGE_INTEGER freq;
// exit if the system does not support a high
performance timer
if (!QueryPerformanceFrequency(&freq))
exit(1);
timerDiff.QuadPart = freq.QuadPart/updateFreq;
// set the timer diff for DESIRED_FREQ times per second
}
void CheckTimer(const LARGE_INTEGER & timerDiff)
{
static LARGE_INTEGER oldTimerVal;
LARGE_INTEGER newTimerVal;
QueryPerformanceCounter(&newTimerVal);
// check for timer roll-over
if (oldTimerVal.QuadPart > newTimerVal.QuadPart)
{
PostMessage(mainWindow,
messageCodes[UPDATE_SCREEN],
0, 0);
oldTimerVal =
newTimerVal;
return;
}
if (newTimerVal.QuadPart-oldTimerVal.QuadPart
> timerDiff.QuadPart)
{
PostMessage(mainWindow,
messageCodes[UPDATE_SCREEN],
0, 0);
oldTimerVal =
newTimerVal;
}
}
And the following is a sample message loop that uses these high-level functions:
// messageCodes is a global array initialized before this point.
const int UPDATE_FREQ = 70;
LARGE_INTEGER timerDiff;
InitTimer(UPDATE_FREQ, timerDiff);
bool ContinueFlag = true;
while (ContinueFlag)
{
while(!PeekMessage(&message,NULL,0,0, PM_NOREMOVE
))
CheckTimer(timerDiff);
ContinueFlag = GetMessage(&message, NULL, 0,0) != 0;
TranslateMessage(&message);
DispatchMessage(&message);
}
Combined, this code posts an UPDATE_SCREEN message UPDATE_FREQ (70)
times per second. While this code can certainly be improved upon
and made more general, its purpose it to convey an idea.
Author:RodneyMyers--5/15/99