-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
SivTimer.cpp
259 lines (205 loc) · 4.42 KB
/
SivTimer.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//-----------------------------------------------
//
// This file is part of the Siv3D Engine.
//
// Copyright (c) 2008-2023 Ryo Suzuki
// Copyright (c) 2016-2023 OpenSiv3D Project
//
// Licensed under the MIT License.
//
//-----------------------------------------------
# include <Siv3D/Timer.hpp>
# include <Siv3D/ISteadyClock.hpp>
# include <Siv3D/FormatData.hpp>
namespace s3d
{
Timer::Timer(const Duration& duration, const StartImmediately startImmediately, ISteadyClock* pSteadyClock)
: m_durationMicrosec{ static_cast<int64>(duration.count() * 1'000'000) }
, m_remainingMicrosec{ m_durationMicrosec }
, m_pSteadyClock{ pSteadyClock }
{
if (startImmediately)
{
start();
}
}
bool Timer::isStarted() const noexcept
{
return m_isStarted;
}
bool Timer::isPaused() const noexcept
{
return (m_isStarted && m_pausing);
}
bool Timer::isRunning() const noexcept
{
return (m_isStarted && (not m_pausing) && (not reachedZero()));
}
bool Timer::reachedZero() const
{
return (us() == 0);
}
void Timer::start()
{
if (m_isStarted && (not m_pausing))
{
return;
}
m_isStarted = true;
m_pausing = false;
m_startTimeMicrosec = ISteadyClock::GetMicrosec(m_pSteadyClock);
}
void Timer::pause()
{
m_remainingMicrosec = us();
m_pausing = true;
}
void Timer::resume()
{
if (not isPaused())
{
return;
}
start();
}
void Timer::reset() noexcept
{
m_remainingMicrosec = m_durationMicrosec;
m_isStarted = false;
m_pausing = false;
}
void Timer::restart()
{
setRemaining(MicrosecondsF(m_durationMicrosec));
start();
}
void Timer::restart(const Duration& duration)
{
reset();
set(duration);
start();
}
void Timer::set(const Duration& duration)
{
m_durationMicrosec = static_cast<int64>(duration.count() * 1'000'000);
m_remainingMicrosec = m_durationMicrosec;
m_startTimeMicrosec = ISteadyClock::GetMicrosec(m_pSteadyClock);
}
void Timer::setRemaining(const Duration& remaining)
{
m_remainingMicrosec = static_cast<int64>(remaining.count() * (1000LL * 1000LL));
m_startTimeMicrosec = ISteadyClock::GetMicrosec(m_pSteadyClock);
}
int32 Timer::d() const
{
return static_cast<int32>(d64());
}
int64 Timer::d64() const
{
return (us() / (1000LL * 1000LL * 60LL * 60LL * 24LL));
}
double Timer::dF() const
{
return static_cast<double>(us() / static_cast<double>(1000LL * 1000LL * 60LL * 60LL * 24LL));
}
int32 Timer::h() const
{
return static_cast<int32>(h64());
}
int64 Timer::h64() const
{
return (us() / (1000LL * 1000LL * 60LL * 60LL));
}
double Timer::hF() const
{
return static_cast<double>(us() / static_cast<double>(1000LL * 1000LL * 60LL * 60LL));
}
int32 Timer::min() const
{
return static_cast<int32>(min64());
}
int64 Timer::min64() const
{
return (us() / (1000LL * 1000LL * 60LL));
}
double Timer::minF() const
{
return static_cast<double>(us() / static_cast<double>(1000LL * 1000LL * 60LL));
}
int32 Timer::s() const
{
return static_cast<int32>(s64());
}
int64 Timer::s64() const
{
return (us() / (1000LL * 1000LL));
}
double Timer::sF() const
{
return static_cast<double>(us() / static_cast<double>(1000LL * 1000LL));
}
int32 Timer::ms() const
{
return static_cast<int32>(ms64());
}
int64 Timer::ms64() const
{
return (us() / (1000LL));
}
double Timer::msF() const
{
return static_cast<double>(us() / static_cast<double>(1000LL));
}
int64 Timer::us() const
{
const int64 t = ISteadyClock::GetMicrosec(m_pSteadyClock);
if (not m_isStarted)
{
return m_durationMicrosec;
}
if (m_pausing)
{
return Max<int64>(m_remainingMicrosec, 0);
}
return Max<int64>(m_remainingMicrosec - (t - m_startTimeMicrosec), 0);
}
int64 Timer::us64() const
{
return us();
}
double Timer::usF() const
{
return static_cast<double>(us());
}
Duration Timer::duration() const
{
return SecondsF{ m_durationMicrosec / static_cast<double>(1000LL * 1000LL) };
}
Duration Timer::remaining() const
{
return SecondsF{ sF() };
}
double Timer::progress1_0() const
{
if (m_durationMicrosec == 0)
{
return 0.0;
}
else
{
return (static_cast<double>(us()) / m_durationMicrosec);
}
}
double Timer::progress0_1() const
{
return (1.0 - progress1_0());
}
String Timer::format(const StringView format) const
{
return FormatTime(remaining(), format);
}
void Formatter(FormatData& formatData, const Timer& value)
{
formatData.string.append(value.format());
}
}