-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameTimer.cpp
More file actions
136 lines (118 loc) · 3.4 KB
/
GameTimer.cpp
File metadata and controls
136 lines (118 loc) · 3.4 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
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
/// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved
#include "pch.h"
#include "GameTimer.h"
GameTimer::GameTimer():
m_active(false)
{
LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency))
{
throw ref new Platform::FailureException();
}
m_secondsPerCount = 1.0f / static_cast<float>(frequency.QuadPart);
Reset();
}
float GameTimer::Now()
{
LARGE_INTEGER currentTime;
if (!QueryPerformanceCounter(¤tTime))
{
throw ref new Platform::FailureException();
}
return (float)(static_cast<double>(currentTime.QuadPart) * (double)m_secondsPerCount);
}
// Returns the total time elapsed since Reset() was called, NOT counting any
// time when the clock is stopped.
float GameTimer::PlayingTime()
{
if (m_active)
{
// The distance m_currentTime - m_baseTime includes paused time,
// which we do not want to count. To correct this, we can subtract
// the paused time from m_currentTime:
return static_cast<float>(((m_currentTime.QuadPart - m_pausedTime.QuadPart) - m_baseTime.QuadPart)*m_secondsPerCount);
}
else
{
// The clock is currently not running so don't count the time since
// the clock was stopped
return static_cast<float>(((m_stopTime.QuadPart - m_pausedTime.QuadPart) - m_baseTime.QuadPart)*m_secondsPerCount);
}
}
void GameTimer::PlayingTime(float time)
{
// Reset the internal state to reflect a PlayingTime of 'time'
// Offset the baseTime by this 'time'.
m_active = false;
m_stopTime = m_currentTime;
m_pausedTime.QuadPart = 0;
m_baseTime.QuadPart = m_stopTime.QuadPart - static_cast<__int64>(time / m_secondsPerCount);
}
float GameTimer::DeltaTime()
{
return m_deltaTime;
}
void GameTimer::Reset()
{
LARGE_INTEGER currentTime;
if (!QueryPerformanceCounter(¤tTime))
{
throw ref new Platform::FailureException();
}
m_baseTime = currentTime;
m_previousTime = currentTime;
m_stopTime = currentTime;
m_currentTime = currentTime;
m_pausedTime.QuadPart = 0;
m_active = false;
}
void GameTimer::Start()
{
LARGE_INTEGER startTime;
if (!QueryPerformanceCounter(&startTime))
{
throw ref new Platform::FailureException();
}
if (!m_active)
{
// Accumulate the time elapsed between stop and start pairs.
m_pausedTime.QuadPart += (startTime.QuadPart - m_stopTime.QuadPart);
m_previousTime = startTime;
m_stopTime.QuadPart = 0;
m_active = true;
}
}
void GameTimer::Stop()
{
if (m_active)
{
// Set the stop time to the time of the last update.
m_stopTime = m_currentTime;
m_active = false;
}
}
void GameTimer::Update()
{
if (!m_active)
{
m_deltaTime = 0.0;
return;
}
LARGE_INTEGER currentTime;
if (!QueryPerformanceCounter(¤tTime))
{
throw ref new Platform::FailureException();
}
m_currentTime = currentTime;
m_deltaTime = (m_currentTime.QuadPart - m_previousTime.QuadPart)*m_secondsPerCount;
m_previousTime = m_currentTime;
if (m_deltaTime < 0.0)
{
m_deltaTime = 0.0;
}
}