After much changing of Spooky's time syncing code for DBPro, I have developed this :
[code#include "gamespeed.h"
struct __GAMESPEED gameSpeed;
bool initialiseTimer(void)
{
if (QueryPerformanceFrequency(&gameSpeed.t1)==false)
{
MessageBox(NULL,
"Sorry. This computer doesn't have a high-frequency timer.",
"* No Timer *",
MB_OK | MB_ICONEXCLAMATION);
return (false);
}
ZeroMemory(&gameSpeed,sizeof(gameSpeed));
return (true);
}
double updateTimer(void)
{
register __int64 diff;
QueryPerformanceCounter(&gameSpeed.t2);
gameSpeed.t3.QuadPart++;
diff=gameSpeed.t2.QuadPart-gameSpeed.t1.QuadPart;
if (diff>=SPEEDSTEP || gameSpeed.t4.QuadPart==0)
{
gameSpeed.speed=(double) min(((diff/(double) SPEEDSTEP)/gameSpeed.t3.QuadPart)/1000,1.0);
if (diff>=SPEEDSTEP)
{
gameSpeed.t1.QuadPart=gameSpeed.t2.QuadPart;
gameSpeed.t3.QuadPart=0;
gameSpeed.t4.QuadPart=1;
}
}
return (gameSpeed.speed);
}
[/code]
[code]#include "windows.h"
typedef struct __GAMESPEED {
LARGE_INTEGER t1;
LARGE_INTEGER t2;
LARGE_INTEGER t3;
LARGE_INTEGER t4;
double speed;
} __GAMESPEED;
#define SPEEDSTEP (double) 1000.0
extern bool initialiseTimer(void);
extern double updateTimer(void);[quote]
It uses the high-frequency timer because it is quite possible for any code to run quicker than the lower-resolution one. Plus, it allows minute adjustment during program execution.
Beware the cat... The alien... The heretic...