[bot] Implement GetElapsedTime()

This commit is contained in:
RD42 2024-04-24 22:15:44 +08:00
parent f775572598
commit 16635b4ea0

View File

@ -114,6 +114,62 @@ void CNetGame::Init(PCHAR szHostOrIp, int iPort,
//---------------------------------------------------- //----------------------------------------------------
#ifdef WIN32
#pragma comment(lib, "winmm.lib")
float GetElapsedTime()
{
static BOOL bTimerInit = false;
static BOOL bUsingOPF = false;
static LONGLONG nTicksPerSec = 0;
if (!bTimerInit)
{
bTimerInit = true;
LARGE_INTEGER qwTicksPerSec;
bUsingOPF = QueryPerformanceFrequency(&qwTicksPerSec);
if (bUsingOPF) nTicksPerSec = qwTicksPerSec.QuadPart;
}
if (bUsingOPF)
{
LARGE_INTEGER qwTime;
QueryPerformanceCounter(&qwTime);
static LONGLONG llLastTime = qwTime.QuadPart;
double fElapsedTime = (double)(qwTime.QuadPart - llLastTime) / (double) nTicksPerSec;
llLastTime = qwTime.QuadPart;
return (float)fElapsedTime;
} else {
double fTime = timeGetTime() * 0.001;
static double fLastTime = fTime;
double fElapsedTime = (double)(fTime - fLastTime);
fLastTime = fTime;
return (float)fElapsedTime;
}
}
#else
float GetElapsedTime()
{
static timeval lasttv;
timeval tv;
float fRet;
gettimeofday(&tv, NULL);
if (!timerisset(&lasttv)) memcpy(&lasttv, &tv, sizeof(timeval));
fRet = (float)((tv.tv_sec - lasttv.tv_sec) * 1000000) + (tv.tv_usec - lasttv.tv_usec);
fRet /= 1000000.0f;
memcpy(&lasttv,&tv,sizeof(timeval));
return fRet;
}
#endif // WIN32
//---------------------------------------------------- //----------------------------------------------------
void CNetGame::Process() void CNetGame::Process()