Etterna 0.74.4
Loading...
Searching...
No Matches
RageTimer.h
1/* RageTimer - Timer services. */
2
3#ifndef RAGE_TIMER_H
4#define RAGE_TIMER_H
5
6#include <chrono>
7
9{
10 public:
11 RageTimer() { Touch(); }
12 RageTimer(std::chrono::steady_clock::time_point tm) : tm(tm) {};
13 RageTimer(unsigned microseconds) : tm()
14 {
15 tm += std::chrono::microseconds(microseconds);
16 }
17 RageTimer(unsigned secs, unsigned microseconds) : tm()
18 {
19 auto seconds = std::chrono::seconds(secs);
20 auto microsecs = std::chrono::microseconds(microseconds);
21 tm += seconds + microsecs;
22 }
23
24 /* Time ago this RageTimer represents. */
25 [[nodiscard]] auto Ago() const -> float;
26 void Touch() { tm = std::chrono::steady_clock::now(); }
27 [[nodiscard]] auto IsZero() const -> bool
28 {
29 return tm == std::chrono::steady_clock::time_point();
30 }
31 void SetZero() { tm = std::chrono::steady_clock::time_point(); }
32
33 /* Time between last call to GetDeltaTime() (Ago() + Touch()): */
34 auto GetDeltaTime() -> float;
35 /* Alias for Ago */
36 [[nodiscard]] auto PeekDeltaTime() const -> float { return Ago(); }
37
38 static auto GetTimeSinceStart()
39 -> float; // seconds since the program was started
40
41 /* Add (or subtract) a duration from a timestamp. The result is another
42 * timestamp. */
43 auto operator+(float tm) const -> RageTimer;
44 auto operator-(float tm) const -> RageTimer { return *this + -tm; }
45 void operator+=(float tm) { *this = *this + tm; }
46 void operator-=(float tm) { *this = *this + -tm; }
47
48 /* Find the amount of time between two timestamps. The result is a
49 * duration. */
50 auto operator-(const RageTimer& rhs) const -> float;
51
52 auto operator<(const RageTimer& rhs) const -> bool;
53 std::chrono::steady_clock::time_point tm;
54
55 private:
56 static auto Sum(const RageTimer& lhs, float tm) -> RageTimer;
57 static auto Difference(const RageTimer& lhs, const RageTimer& rhs) -> float;
58};
59
60extern const RageTimer RageZeroTimer;
61
62#endif
Definition RageTimer.h:9