-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess_timer.cpp
64 lines (47 loc) · 1.47 KB
/
chess_timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "chess_timer.h"
bool ChessHiResTimer::initialize()
{
if (!QueryPerformanceFrequency(&ticksPerSecond)) {
// system doesn't support hi-res timer
return false;
} else {
QueryPerformanceCounter(&startTime);
return true;
}
}
float ChessHiResTimer::getElapsedSeconds(unsigned long elapsedFrames /*= 1*/)
{
static LARGE_INTEGER s_lastTime = startTime;
LARGE_INTEGER currentTime;
QueryPerformanceCounter(¤tTime);
float seconds = ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart) / (float)ticksPerSecond.QuadPart;
// reset the timer
s_lastTime = currentTime;
return seconds;
}
float ChessHiResTimer::getFPS(unsigned long elapsedFrames /*= 1*/)
{
static LARGE_INTEGER s_lastTime = startTime;
LARGE_INTEGER currentTime;
QueryPerformanceCounter(¤tTime);
float fps = (float)elapsedFrames * (float)ticksPerSecond.QuadPart / ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart);
// reset the timer
s_lastTime = currentTime;
return fps;
}
float ChessHiResTimer::lockFPS(unsigned char targetFPS)
{
if (targetFPS == 0)
targetFPS = 1;
static LARGE_INTEGER s_lastTime = startTime;
LARGE_INTEGER currentTime;
float fps;
// delay to maintain a constant frame rate
do {
QueryPerformanceCounter(¤tTime);
fps = (float)ticksPerSecond.QuadPart/((float)(currentTime.QuadPart - s_lastTime.QuadPart));
} while (fps > (float)targetFPS);
// reset the timer
s_lastTime = startTime;
return fps;
}