-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTimer.cpp
125 lines (103 loc) · 3.13 KB
/
GameTimer.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
//***************************************************************************************
// GameTimer.cpp by Frank Luna (C) 2011 All Rights Reserved.
//***************************************************************************************
#include <windows.h>
#include "GameTimer.h"
GameTimer::GameTimer()
: m_SecondsPerCount(0.0), m_DeltaTime(-1.0), m_BaseTime(0), m_StopTime(0),
m_PausedTime(0), m_PrevTime(0), m_CurrTime(0), m_Stopped(false)
{
__int64 countsPerSec;
QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);
m_SecondsPerCount = 1.0 / (double)countsPerSec;
}
// Returns the total time elapsed since Reset() was called, NOT counting any
// time when the clock is stopped.
float GameTimer::TotalTime()const
{
// If we are stopped, do not count the time that has passed since we stopped.
// Moreover, if we previously already had a pause, the distance
// m_StopTime - m_BaseTime includes paused time, which we do not want to count.
// To correct this, we can subtract the paused time from m_StopTime:
//
// |<--paused time-->|
// ----*---------------*-----------------*------------*------------*------> time
// m_BaseTime m_StopTime startTime m_StopTime m_CurrTime
if( m_Stopped )
{
return (float)(((m_StopTime - m_PausedTime)-m_BaseTime)*m_SecondsPerCount);
}
// The distance m_CurrTime - m_BaseTime includes paused time,
// which we do not want to count. To correct this, we can subtract
// the paused time from m_CurrTime:
//
// (m_CurrTime - m_PausedTime) - m_BaseTime
//
// |<--paused time-->|
// ----*---------------*-----------------*------------*------> time
// m_BaseTime m_StopTime startTime m_CurrTime
else
{
return (float)(((m_CurrTime-m_PausedTime)-m_BaseTime)*m_SecondsPerCount);
}
}
float GameTimer::DeltaTime()const
{
return (float)m_DeltaTime;
}
void GameTimer::Reset()
{
__int64 currTime;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
m_BaseTime = currTime;
m_PrevTime = currTime;
m_StopTime = 0;
m_PausedTime = 0; // 涉及到多次Reset的话需要将其归0
m_Stopped = false;
}
void GameTimer::Start()
{
__int64 startTime;
QueryPerformanceCounter((LARGE_INTEGER*)&startTime);
// Accumulate the time elapsed between stop and start pairs.
//
// |<-------d------->|
// ----*---------------*-----------------*------------> time
// m_BaseTime m_StopTime startTime
if( m_Stopped )
{
m_PausedTime += (startTime - m_StopTime);
m_PrevTime = startTime;
m_StopTime = 0;
m_Stopped = false;
}
}
void GameTimer::Stop()
{
if( !m_Stopped )
{
__int64 currTime;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
m_StopTime = currTime;
m_Stopped = true;
}
}
void GameTimer::Tick()
{
if( m_Stopped )
{
m_DeltaTime = 0.0;
return;
}
__int64 currTime;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
m_CurrTime = currTime;
// Time difference between this frame and the previous.
m_DeltaTime = (m_CurrTime - m_PrevTime)*m_SecondsPerCount;
// Prepare for next frame.
m_PrevTime = m_CurrTime;
if(m_DeltaTime < 0.0)
{
m_DeltaTime = 0.0;
}
}