-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHiResTimer.h
144 lines (111 loc) · 4.23 KB
/
HiResTimer.h
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
// -----------------------------------------------------------------
// Learning Team B
// Members:
// Adam LeMmon
// Faith Satterthwaite
// Tom Fletcher
// Justin Ball
// CS 4280 – 11:30 am
// Final Project
// Dr. Rague
// Due: 12/06/12
// Version: 2.4
// -----------------------------------------------------------------
// We made five major improvements to this game
// 1) New controls
// 2) Enemy attack
// 3) HUD (heads up display)
// 4) Enemy health bars
// 5) New Weapon
// -----------------------------------------------------------------
/****************************************************************************
HiResTimer.h
Wrapper for the high-resolution timer. Can't be used if the hi-res timer
doesn't exist.
Author : Dave Astle
Date : 2/1/2001
Written for OpenGL Game Programming
*****************************************************************************/
#ifndef __TIMER_H_INCLUDED__
#define __TIMER_H_INCLUDED__
#include <windows.h>
class CHiResTimer
{
public:
CHiResTimer() {}
~CHiResTimer() {}
/*****************************************************************************
Init()
If the hi-res timer is present, the tick rate is stored and the function
returns true. Otherwise, the function returns false, and the timer should
not be used.
*****************************************************************************/
bool Init()
{
if (!QueryPerformanceFrequency(&m_ticksPerSecond))
{
// system doesn't support hi-res timer
return false;
}
else
{
QueryPerformanceCounter(&m_startTime);
return true;
}
} // end Init()
float GetElapsedSeconds(unsigned long elapsedFrames = 1)
{
static LARGE_INTEGER s_lastTime = m_startTime;
LARGE_INTEGER currentTime;
QueryPerformanceCounter(¤tTime);
float seconds = ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart) / (float)m_ticksPerSecond.QuadPart;
// reset the timer
s_lastTime = currentTime;
return seconds;
} // end GetElapsedSeconds()
/***************************************************************************
GetFPS()
Returns the average frames per second over elapsedFrames, which defaults to
one. If this is not called every frame, the client should track the number
of frames itself, and reset the value after this is called.
***************************************************************************/
float GetFPS(unsigned long elapsedFrames = 1)
{
static LARGE_INTEGER s_lastTime = m_startTime;
LARGE_INTEGER currentTime;
QueryPerformanceCounter(¤tTime);
float fps = (float)elapsedFrames * (float)m_ticksPerSecond.QuadPart / ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart);
// reset the timer
s_lastTime = currentTime;
return fps;
} // end GetFPS
/***************************************************************************
LockFPS()
Used to lock the frame rate to a set amount. This will block until enough
time has passed to ensure that the fps won't go over the requested amount.
Note that this can only keep the fps from going above the specified level;
it can still drop below it. It is assumed that if used, this function will
be called every frame. The value returned is the instantaneous fps, which
will be <= targetFPS.
***************************************************************************/
float LockFPS(unsigned char targetFPS)
{
if (targetFPS == 0)
targetFPS = 1;
static LARGE_INTEGER s_lastTime = m_startTime;
LARGE_INTEGER currentTime;
float fps;
// delay to maintain a constant frame rate
do {
QueryPerformanceCounter(¤tTime);
fps = (float)m_ticksPerSecond.QuadPart/((float)(currentTime.QuadPart - s_lastTime.QuadPart));
} while (fps > (float)targetFPS);
// reset the timer
s_lastTime = m_startTime;
return fps;
} // end LockFPS()
private:
LARGE_INTEGER m_startTime;
LARGE_INTEGER m_ticksPerSecond;
};
#endif // __TIMER_H_INCLUDED_