Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Clock.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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_CLOCK_H
25 #define GF_CLOCK_H
26 
27 #include <chrono>
28 
29 #include "Portability.h"
30 #include "Time.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
37  /**
38  * @ingroup core
39  * @brief Utility class that measures the elapsed time
40  *
41  * gf::Clock is a lightweight class for measuring time.
42  * It is a thin wrapper around C++11
43  * [std::chrono::steady_clock](http://en.cppreference.com/w/cpp/chrono/steady_clock).
44  *
45  * Its provides the most precise time that the underlying
46  * OS can achieve (generally microseconds or nanoseconds).
47  * It also ensures monotonicity, which means that the returned
48  * time can never go backward, even if the system time is
49  * changed.
50  *
51  * Usage example:
52  *
53  * ~~~{.cc}
54  * gf::Clock clock;
55  * ...
56  * gf::Time time1 = clock.getElapsedTime();
57  * ...
58  * gf::Time time2 = clock.restart();
59  * ~~~
60  *
61  * The gf::Time value returned by the clock can then be
62  * converted to a number of seconds, milliseconds or even
63  * microseconds.
64  *
65  * @sa gf::Time
66  */
67  class GF_API Clock {
68  public:
69  /**
70  * @brief Default constructor
71  *
72  * The clock starts automatically after being constructed.
73  */
74  Clock();
75 
76  /**
77  * @brief Get the elapsed time
78  *
79  * This function returns the time elapsed since the last call
80  * to `restart()` (or the construction of the instance if `restart()`
81  * has not been called).
82  *
83  * @return Time elapsed
84  */
85  Time getElapsedTime() const;
86 
87  /**
88  * @brief Restart the clock
89  *
90  * This function puts the time counter back to zero.
91  * It also returns the time elapsed since the clock was started.
92  *
93  * @return Time elapsed
94  */
95  Time restart();
96 
97  private:
98  std::chrono::steady_clock::time_point m_start;
99  };
100 
101 #ifndef DOXYGEN_SHOULD_SKIP_THIS
102 }
103 #endif
104 }
105 
106 #endif // GF_CLOCK_H
Clock()
Default constructor.
Time getElapsedTime() const
Get the elapsed time.
Time restart()
Restart the clock.
Represents a time value.
Definition: Time.h:73
Definition: Action.h:34
#define GF_API
Definition: Portability.h:35
Utility class that measures the elapsed time.
Definition: Clock.h:67