-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTimer.cpp
More file actions
105 lines (80 loc) · 2.59 KB
/
CTimer.cpp
File metadata and controls
105 lines (80 loc) · 2.59 KB
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
#include "CTimer.h"
//---------------------- default constructor ------------------------------
//
//-------------------------------------------------------------------------
CTimer::CTimer(): m_FPS(0),
m_TimeElapsed(0.0f),
m_FrameTime(0),
m_LastTime(0),
m_PerfCountFreq(0)
{
//how many ticks per sec do we get
QueryPerformanceFrequency( (LARGE_INTEGER*) &m_PerfCountFreq);
m_TimeScale = 1.0f/m_PerfCountFreq;
}
//---------------------- constructor -------------------------------------
//
// use to specify FPS
//
//-------------------------------------------------------------------------
CTimer::CTimer(float fps): m_FPS(fps),
m_TimeElapsed(0.0f),
m_LastTime(0),
m_PerfCountFreq(0)
{
//how many ticks per sec do we get
QueryPerformanceFrequency( (LARGE_INTEGER*) &m_PerfCountFreq);
m_TimeScale = 1.0f/m_PerfCountFreq;
//calculate ticks per frame
m_FrameTime = (LONGLONG)(m_PerfCountFreq / m_FPS);
}
//------------------------Start()-----------------------------------------
//
// call this immediately prior to game loop. Starts the timer (obviously!)
//
//--------------------------------------------------------------------------
void CTimer::Start()
{
//get the time
QueryPerformanceCounter( (LARGE_INTEGER*) &m_LastTime);
//update time to render next frame
m_NextTime = m_LastTime + m_FrameTime;
return;
}
//-------------------------ReadyForNextFrame()-------------------------------
//
// returns true if it is time to move on to the next frame step. To be used if
// FPS is set.
//
//----------------------------------------------------------------------------
bool CTimer::ReadyForNextFrame()
{
if (!m_FPS)
{
MessageBox(NULL, "No FPS set in timer", "Doh!", 0);
return false;
}
QueryPerformanceCounter( (LARGE_INTEGER*) &m_CurrentTime);
if (m_CurrentTime > m_NextTime)
{
m_TimeElapsed = (m_CurrentTime - m_LastTime) * m_TimeScale;
m_LastTime = m_CurrentTime;
//update time to render next frame
m_NextTime = m_CurrentTime + m_FrameTime;
return true;
}
return false;
}
//--------------------------- TimeElapsed --------------------------------
//
// returns time elapsed since last call to this function. Use in main
// when calculations are to be based on dt.
//
//-------------------------------------------------------------------------
double CTimer::TimeElapsed()
{
QueryPerformanceCounter( (LARGE_INTEGER*) &m_CurrentTime);
m_TimeElapsed = (m_CurrentTime - m_LastTime) * m_TimeScale;
m_LastTime = m_CurrentTime;
return m_TimeElapsed;
}