-
Notifications
You must be signed in to change notification settings - Fork 9
/
timer.cpp
130 lines (115 loc) · 2.52 KB
/
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
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
#include "Timer.h"
#include "Misc.h"
// ----
CTimer::CTimer(int _milliseconds)
{
SoundSystemRef = NULL;
Method = HIGH_ACCURACY_TIMER;
MilliSeconds = _milliseconds;
if ( (!QueryPerformanceFrequency(&SampleFrequency))
|| (!QueryPerformanceCounter(&LastSample)))
{
// Time to resort to the back up plan...
Method = LOWER_ACCURACY_TIMER;
LastTickCount = GetTickCount();
}
}
// ----
CTimer::~CTimer()
{
}
// ----
// Returns the number of time slices that have
// passed since the last time it was called.
int CTimer::GetNumberOfEvents()
{
#ifdef TEST_IN_EMULATION
return 1;
#endif
if (MilliSeconds <= 0)
return 1;
LARGE_INTEGER NowSample;
DWORD NowTickCount;
int Ret;
if (Method == HIGH_ACCURACY_TIMER)
{
QueryPerformanceCounter(&NowSample);
float temp = (float)(NowSample.QuadPart - LastSample.QuadPart) / (float)SampleFrequency.QuadPart;
temp *= 1000.0f;
Ret = temp / (float)MilliSeconds;
// LastSample = NowSample;
}
else
{
NowTickCount = GetTickCount();
if (NowTickCount > LastTickCount)
{
Ret = (NowTickCount - LastTickCount) / MilliSeconds;
}
else
{
// Clock ticked over.
Ret = (NowTickCount / MilliSeconds) + ((4294967295 - LastTickCount) / MilliSeconds);
}
// LastTickCount = GetTickCount();
}
return Ret;
}
// ----
void CTimer::ClearNumberOfEvents()
{
if (Method == HIGH_ACCURACY_TIMER)
{
QueryPerformanceCounter(&LastSample);
}
else
{
LastTickCount = GetTickCount();
}
}
// ----
// Sits in a loop until at least one event has passed -
// will drop out immediately if one, or more, have
// already passed since GetNumberOfEvents was last
// called.
void CTimer::BlockUntilClock()
{
#if (defined TEST_IN_EMULATION) || (defined DONT_LIMIT_FRAME_RATE)
return;
#endif
if (MilliSeconds <= 0)
return;
LARGE_INTEGER NowSample;
DWORD NowTickCount;
bool Stay = true;
if (Method == HIGH_ACCURACY_TIMER)
{
while (Stay)
{
if (SoundSystemRef)
SoundSystemRef->Process();
QueryPerformanceCounter(&NowSample);
float temp = (float)(NowSample.QuadPart - LastSample.QuadPart) / (float)SampleFrequency.QuadPart;
temp *= 1000.0f;
Stay = ((temp / (float)MilliSeconds) < 1.0f);
}
}
else
{
while (Stay)
{
if (SoundSystemRef)
SoundSystemRef->Process();
NowTickCount = GetTickCount();
if (NowTickCount > LastTickCount)
{
Stay = (((NowTickCount - LastTickCount) / MilliSeconds) >= 1);
}
else
{
// Clock ticked over.
Stay = (((NowTickCount / MilliSeconds) + ((4294967295 - LastTickCount) / MilliSeconds)) < 1);
}
}
}
}