Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Time.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 *
21 * Part of this file comes from SFML, with the same license:
22 * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23 */
24#ifndef GF_TIME_H
25#define GF_TIME_H
26
27#include <cstdint>
28#include <chrono>
29#include <ratio>
30
31#include "CoreApi.h"
32#include "SerializationFwd.h"
33
34namespace gf {
35#ifndef DOXYGEN_SHOULD_SKIP_THIS
36inline namespace v1 {
37#endif
38
65 class GF_CORE_API Time {
66 public:
72 constexpr Time()
73 : m_duration(std::chrono::steady_clock::duration::zero())
74 {
75
76 }
77
83 constexpr explicit Time(std::chrono::steady_clock::duration duration)
84 : m_duration(duration)
85 {
86
87 }
88
96 constexpr float asSeconds() const {
97 return std::chrono::duration_cast<std::chrono::duration<float>>(m_duration).count();
98 }
99
107 constexpr int32_t asMilliseconds() const {
108 return std::chrono::duration_cast<std::chrono::duration<int32_t, std::milli>>(m_duration).count();
109 }
110
118 constexpr int64_t asMicroseconds() const {
119 return std::chrono::duration_cast<std::chrono::duration<int64_t, std::micro>>(m_duration).count();
120 }
121
127 constexpr std::chrono::steady_clock::duration asDuration() const {
128 return m_duration;
129 }
130
137 Time& addTo(Time other) {
138 m_duration += other.m_duration;
139 return *this;
140 }
141
148 Time& subTo(Time other) {
149 m_duration -= other.m_duration;
150 return *this;
151 }
152
159 static const Time Zero;
160
167 static constexpr Time zero() {
168 return Time();
169 }
170
171 private:
172 std::chrono::steady_clock::duration m_duration;
173 };
174
185 constexpr
186 Time seconds(float amount) {
187 return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<float>(amount)));
188 }
189
200 constexpr
201 Time milliseconds(int32_t amount) {
202 return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<int32_t, std::milli>(amount)));
203 }
204
215 constexpr
216 Time microseconds(int64_t amount) {
217 return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<int64_t, std::micro>(amount)));
218 }
219
220 namespace literals {
221
232 constexpr gf::Time operator"" _seconds(long double amount) {
233 return gf::seconds(static_cast<float>(amount));
234 }
235
246 constexpr gf::Time operator"" _milliseconds(unsigned long long int amount) {
247 return gf::milliseconds(static_cast<int32_t>(amount));
248 }
249
260 constexpr gf::Time operator"" _microseconds(unsigned long long int amount) {
261 return gf::microseconds(static_cast<int64_t>(amount));
262 }
263
264 }
265
266
275 constexpr
276 bool operator==(Time rhs, Time lhs) {
277 return rhs.asDuration() == lhs.asDuration();
278 }
279
288 constexpr
289 bool operator!=(Time rhs, Time lhs) {
290 return rhs.asDuration() != lhs.asDuration();
291 }
292
301 constexpr
302 bool operator<(Time rhs, Time lhs) {
303 return rhs.asDuration() < lhs.asDuration();
304 }
305
314 constexpr
315 bool operator>(Time rhs, Time lhs) {
316 return rhs.asDuration() > lhs.asDuration();
317 }
318
327 constexpr
328 bool operator<=(Time rhs, Time lhs) {
329 return rhs.asDuration() <= lhs.asDuration();
330 }
331
340 constexpr
341 bool operator>=(Time rhs, Time lhs) {
342 return rhs.asDuration() >= lhs.asDuration();
343 }
344
353 constexpr
355 return Time(rhs.asDuration() + lhs.asDuration());
356 }
357
366 inline
368 return rhs.addTo(lhs);
369 }
370
379 constexpr
381 return Time(rhs.asDuration() - lhs.asDuration());
382 }
383
392 inline
394 return rhs.subTo(lhs);
395 }
396
401 GF_CORE_API Serializer& operator|(Serializer& ar, Time time);
402
407 GF_CORE_API Deserializer& operator|(Deserializer& ar, Time& time);
408
409#ifndef DOXYGEN_SHOULD_SKIP_THIS
410}
411#endif
412}
413
414#endif // GF_TIME_H
A deserializer from a binary file.
Definition: Serialization.h:151
GF_CORE_API Deserializer & operator|(Deserializer &ar, Time &time)
Deserialize a time.
A serializer to a binary file.
Definition: Serialization.h:43
GF_CORE_API Serializer & operator|(Serializer &ar, Time time)
Serialize a time.
Represents a time value.
Definition: Time.h:65
constexpr bool operator!=(Time rhs, Time lhs)
Inequality operator.
Definition: Time.h:289
static const Time Zero
A time of zero as a variable.
Definition: Time.h:159
Time operator+=(Time &rhs, Time lhs)
Addition and assignement.
Definition: Time.h:367
constexpr int32_t asMilliseconds() const
Return the time value as a number of milliseconds.
Definition: Time.h:107
constexpr Time()
Default constructor.
Definition: Time.h:72
Time & addTo(Time other)
Add another time.
Definition: Time.h:137
constexpr Time seconds(float amount)
Construct a time value from a number of seconds.
Definition: Time.h:186
constexpr bool operator<=(Time rhs, Time lhs)
Lesser or equal operator.
Definition: Time.h:328
constexpr Time operator+(Time rhs, Time lhs)
Addition of two times.
Definition: Time.h:354
constexpr float asSeconds() const
Return the time value as a number of seconds.
Definition: Time.h:96
constexpr bool operator>=(Time rhs, Time lhs)
Greater or equal than operator.
Definition: Time.h:341
constexpr std::chrono::steady_clock::duration asDuration() const
Return the time value as a duration.
Definition: Time.h:127
Time operator-=(Time &rhs, Time lhs)
Substraction and assignment.
Definition: Time.h:393
constexpr Time operator-(Time rhs, Time lhs)
Substraction of two times.
Definition: Time.h:380
constexpr bool operator==(Time rhs, Time lhs)
Equality operator.
Definition: Time.h:276
constexpr bool operator<(Time rhs, Time lhs)
Lesser than operator.
Definition: Time.h:302
constexpr int64_t asMicroseconds() const
Return the time value as a number of microseconds.
Definition: Time.h:118
constexpr Time microseconds(int64_t amount)
Construct a time value from a number of microseconds.
Definition: Time.h:216
constexpr Time(std::chrono::steady_clock::duration duration)
Constructor with a duration.
Definition: Time.h:83
constexpr Time milliseconds(int32_t amount)
Construct a time value from a number of milliseconds.
Definition: Time.h:201
static constexpr Time zero()
Get a time of zero.
Definition: Time.h:167
constexpr bool operator>(Time rhs, Time lhs)
Greater than operator.
Definition: Time.h:315
Time & subTo(Time other)
Substract another time.
Definition: Time.h:148
The namespace for gf classes.