-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTimer.cpp
180 lines (161 loc) · 3.57 KB
/
CTimer.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************
CTimer.cpp
Timer class - works like a stopwatch
********************************************/
#include "Windows.h"
#include "CTimer.h"
//////////////////////////////
// Constructor
CTimer::CTimer()
{
// Try to initialise performance timer, will use low-resolution timer on failure
m_HighRes = (QueryPerformanceFrequency( &m_HighResFreq ) != 0);
// Reset and start the timer
Reset();
m_Running = true;
}
//////////////////////////////
// Timer control
// Start the timer running
void CTimer::Start()
{
if (!m_Running)
{
m_Running = true;
// Get restart time - add time passed since stop time to the start and lap times
// Select high or low-resolution timer
if (m_HighRes)
{
LARGE_INTEGER newHighResTime;
QueryPerformanceCounter( &newHighResTime );
m_HighResStart.QuadPart += (newHighResTime.QuadPart - m_HighResStop.QuadPart);
m_HighResLap.QuadPart += (newHighResTime.QuadPart - m_HighResStop.QuadPart);
}
else
{
DWORD newLowResTime;
newLowResTime = timeGetTime();
m_LowResStart += (newLowResTime - m_LowResStop);
m_LowResLap += (newLowResTime - m_LowResStop);
}
}
}
// Stop the timer running
void CTimer::Stop()
{
m_Running = false;
// Get stop time
// Select high or low-resolution timer
if (m_HighRes)
{
QueryPerformanceCounter( &m_HighResStop );
}
else
{
m_LowResStop = timeGetTime();
}
}
// Reset the timer to zero
void CTimer::Reset()
{
// Reset start, lap and stop times to current time
// Select high or low-resolution timer
if (m_HighRes)
{
QueryPerformanceCounter( &m_HighResStart );
m_HighResLap = m_HighResStart;
m_HighResStop = m_HighResStart;
}
else
{
m_LowResStart = timeGetTime();
m_LowResLap = m_LowResStart;
m_LowResStop = m_LowResStart;
}
}
//////////////////////////////
// Timing
// Get frequency of the timer being used (in counts per second)
float CTimer::GetFrequency()
{
// Select high or low-resolution timer
if (m_HighRes)
{
return static_cast<float>(m_HighResFreq.QuadPart);
}
else
{
return 1000.0f;
}
}
// Get time passed (seconds) since since timer was started or last reset
float CTimer::GetTime()
{
float fTime;
if (m_HighRes)
{
LARGE_INTEGER newHighResTime;
if (m_Running)
{
QueryPerformanceCounter( &newHighResTime );
}
else
{
newHighResTime = m_HighResStop;
}
double dTime = static_cast<double>(newHighResTime.QuadPart - m_HighResStart.QuadPart) /
static_cast<double>(m_HighResFreq.QuadPart);
fTime = static_cast<float>(dTime);
}
else
{
DWORD newLowResTime;
if (m_Running)
{
newLowResTime = timeGetTime();
}
else
{
newLowResTime = m_LowResStop;
}
fTime = static_cast<float>(newLowResTime - m_LowResStart) / 1000.0f;
}
return fTime;
}
// Get time passed (seconds) since last call to this function. If this is the first call, then
// the time since timer was started or the last reset is returned
float CTimer::GetLapTime()
{
float fTime;
if (m_HighRes)
{
LARGE_INTEGER newHighResTime;
if (m_Running)
{
QueryPerformanceCounter( &newHighResTime );
}
else
{
newHighResTime = m_HighResStop;
}
double dTime = static_cast<double>(newHighResTime.QuadPart - m_HighResLap.QuadPart) /
static_cast<double>(m_HighResFreq.QuadPart);
fTime = static_cast<float>(dTime);
m_HighResLap = newHighResTime;
}
else
{
DWORD newLowResTime;
if (m_Running)
{
newLowResTime = timeGetTime();
}
else
{
newLowResTime = m_LowResStop;
}
fTime = static_cast<float>(newLowResTime - m_LowResLap) / 1000.0f;
m_LowResLap = newLowResTime;
}
return fTime;
}