Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
Time.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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 "Portability.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
38  /**
39  * @ingroup core
40  * @brief Represents a time value
41  *
42  * gf::Time encapsulates a time value in a flexible way.
43  * It allows to define a time value either as a number of
44  * seconds, milliseconds or microseconds. It also works the
45  * other way round: you can read a time value as either
46  * a number of seconds, milliseconds or microseconds.
47  *
48  * By using such a flexible interface, the API doesn't
49  * impose any fixed type or resolution for time values,
50  * and let the user choose its own favorite representation.
51  *
52  * Since they represent a time span and not an absolute time
53  * value, times can also be negative.
54  *
55  * gf::Time is a thin wrapper around C++11
56  * [std::chrono::steady_clock](http://en.cppreference.com/w/cpp/chrono/steady_clock).
57  *
58  * Usage example:
59  *
60  * ~~~{.cc}
61  * gf::Time t1 = gf::seconds(0.1f);
62  * int32_t milli = t1.asMilliseconds(); // 100
63  *
64  * gf::Time t2 = gf::milliseconds(30);
65  * int64_t micro = t2.asMicroseconds(); // 30000
66  *
67  * gf::Time t3 = gf::microseconds(-800000);
68  * float sec = t3.asSeconds(); // -0.8
69  * ~~~
70  *
71  * @sa gf::Clock
72  */
73  class GF_API Time {
74  public:
75  /**
76  * @brief Default constructor
77  *
78  * Sets the time value to zero.
79  */
80  constexpr Time()
81  : m_duration(std::chrono::steady_clock::duration::zero())
82  {
83 
84  }
85 
86  /**
87  * @brief Constructor with a duration
88  *
89  * @param duration A duration expressed with a `std::chrono` type
90  */
91  explicit Time(std::chrono::steady_clock::duration duration)
92  : m_duration(duration)
93  {
94 
95  }
96 
97  /**
98  * @brief Return the time value as a number of seconds
99  *
100  * @return Time in seconds
101  *
102  * @sa asMilliseconds(), asMicroseconds()
103  */
104  float asSeconds() const;
105 
106  /**
107  * @brief Return the time value as a number of milliseconds
108  *
109  * @return Time in milliseconds
110  *
111  * @sa asSeconds(), asMicroseconds()
112  */
113  int32_t asMilliseconds() const;
114 
115  /**
116  * @brief Return the time value as a number of microseconds
117  *
118  * @return Time in microseconds
119  *
120  * @sa asSeconds(), asMilliseconds()
121  */
122  int64_t asMicroseconds() const;
123 
124  /**
125  * @brief Return the time value as a duration
126  *
127  * @return Time as a duration (`std::chrono` type)
128  */
129  std::chrono::steady_clock::duration asDuration() const {
130  return m_duration;
131  }
132 
133  static const Time Zero;
134 
135  private:
136  std::chrono::steady_clock::duration m_duration;
137  };
138 
139  /**
140  * @relates Time
141  * @brief Construct a time value from a number of seconds
142  *
143  * @param amount Number of seconds
144  *
145  * @return Time value constructed from the amount of seconds
146  *
147  * @sa milliseconds(), microseconds()
148  */
149  GF_API Time seconds(float amount);
150 
151  /**
152  * @relates Time
153  * @brief Construct a time value from a number of milliseconds
154  *
155  * @param amount Number of milliseconds
156  *
157  * @return Time value constructed from the amount of milliseconds
158  *
159  * @sa seconds(), microseconds()
160  */
161  GF_API Time milliseconds(int32_t amount);
162 
163  /**
164  * @relates Time
165  * @brief Construct a time value from a number of microseconds
166  *
167  * @param amount Number of microseconds
168  *
169  * @return Time value constructed from the amount of microseconds
170  *
171  * @sa seconds(), milliseconds()
172  */
173  GF_API Time microseconds(int64_t amount);
174 
175  /**
176  * @relates Time
177  * @brief Equality operator
178  *
179  * @param rhs First time
180  * @param lhs Second time
181  * @return True if the first time and the second time are the same
182  */
183  inline
184  bool operator==(Time rhs, Time lhs) {
185  return rhs.asDuration() == lhs.asDuration();
186  }
187 
188  /**
189  * @relates Time
190  * @brief Inequality operator
191  *
192  * @param rhs First time
193  * @param lhs Second time
194  * @return True if the first time and the second time are different
195  */
196  inline
197  bool operator!=(Time rhs, Time lhs) {
198  return rhs.asDuration() != lhs.asDuration();
199  }
200 
201  /**
202  * @relates Time
203  * @brief Lesser than operator
204  *
205  * @param rhs First time
206  * @param lhs Second time
207  * @return True if the first time is lesser than the second time
208  */
209  inline
210  bool operator<(Time rhs, Time lhs) {
211  return rhs.asDuration() < lhs.asDuration();
212  }
213 
214  /**
215  * @relates Time
216  * @brief Greater than operator
217  *
218  * @param rhs First time
219  * @param lhs Second time
220  * @return True if the first time is greater than the second time
221  */
222  inline
223  bool operator>(Time rhs, Time lhs) {
224  return rhs.asDuration() > lhs.asDuration();
225  }
226 
227  /**
228  * @relates Time
229  * @brief Lesser or equal operator
230  *
231  * @param rhs First time
232  * @param lhs Second time
233  * @return True if the first time is lesser or equal than the second time
234  */
235  inline
236  bool operator<=(Time rhs, Time lhs) {
237  return rhs.asDuration() <= lhs.asDuration();
238  }
239 
240  /**
241  * @relates Time
242  * @brief Greater or equal than operator
243  *
244  * @param rhs First time
245  * @param lhs Second time
246  * @return True if the first time is greater or equal than the second time
247  */
248  inline
249  bool operator>=(Time rhs, Time lhs) {
250  return rhs.asDuration() >= lhs.asDuration();
251  }
252 
253  /**
254  * @relates Time
255  * @brief Addition of two times
256  *
257  * @param rhs First time
258  * @param lhs Second time
259  * @return The sum of two times
260  */
261  inline
262  Time operator+(Time rhs, Time lhs) {
263  return Time(rhs.asDuration() + lhs.asDuration());
264  }
265 
266  /**
267  * @relates Time
268  * @brief Substraction of two times
269  *
270  * @param rhs First time
271  * @param lhs Second time
272  * @return The difference of two times
273  */
274  inline
275  Time operator-(Time rhs, Time lhs) {
276  return Time(rhs.asDuration() - lhs.asDuration());
277  }
278 
279 #ifndef DOXYGEN_SHOULD_SKIP_THIS
280 }
281 #endif
282 }
283 
284 #endif // GF_TIME_H
Time operator-(Time rhs, Time lhs)
Substraction of two times.
Definition: Time.h:275
int32_t asMilliseconds() const
Return the time value as a number of milliseconds.
bool operator<=(Time rhs, Time lhs)
Lesser or equal operator.
Definition: Time.h:236
std::chrono::steady_clock::duration asDuration() const
Return the time value as a duration.
Definition: Time.h:129
constexpr Time()
Default constructor.
Definition: Time.h:80
bool operator!=(Time rhs, Time lhs)
Inequality operator.
Definition: Time.h:197
bool operator<(Time rhs, Time lhs)
Lesser than operator.
Definition: Time.h:210
Represents a time value.
Definition: Time.h:73
Time(std::chrono::steady_clock::duration duration)
Constructor with a duration.
Definition: Time.h:91
float asSeconds() const
Return the time value as a number of seconds.
Time milliseconds(int32_t amount)
Construct a time value from a number of milliseconds.
bool operator>=(Time rhs, Time lhs)
Greater or equal than operator.
Definition: Time.h:249
The namespace for gf classes.
Definition: Action.h:34
Time microseconds(int64_t amount)
Construct a time value from a number of microseconds.
Time operator+(Time rhs, Time lhs)
Addition of two times.
Definition: Time.h:262
int64_t asMicroseconds() const
Return the time value as a number of microseconds.
bool operator==(Time rhs, Time lhs)
Equality operator.
Definition: Time.h:184
#define GF_API
Definition: Portability.h:35
static const Time Zero
Definition: Time.h:133
Time seconds(float amount)
Construct a time value from a number of seconds.
bool operator>(Time rhs, Time lhs)
Greater than operator.
Definition: Time.h:223