-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameTimer.cpp
More file actions
105 lines (86 loc) · 2.35 KB
/
GameTimer.cpp
File metadata and controls
105 lines (86 loc) · 2.35 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
//***************************************************************************************
// 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;
}
//去掉暂停停止后的总时间
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);
}
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);
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;
m_DeltaTime = (m_CurrTime - m_PrevTime)*m_SecondsPerCount;
m_PrevTime = m_CurrTime;
if(m_DeltaTime < 0.0)
{
m_DeltaTime = 0.0;
}
}