Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Time Syncing

Author
Message
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 11th Dec 2004 23:55
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...

Login to post a reply

Server time is: 2024-04-19 00:28:33
Your offset time is: 2024-04-19 00:28:33