Etterna 0.75.1
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 static auto GetTimeSinceStart64() -> double;
42
43 /* Add (or subtract) a duration from a timestamp. The result is another
44 * timestamp. */
45 auto operator+(float tm) const -> RageTimer;
46 auto operator-(float tm) const -> RageTimer { return *this + -tm; }
47 void operator+=(float tm) { *this = *this + tm; }
48 void operator-=(float tm) { *this = *this + -tm; }
49
50 /* Find the amount of time between two timestamps. The result is a
51 * duration. */
52 auto operator-(const RageTimer& rhs) const -> float;
53
54 auto operator<(const RageTimer& rhs) const -> bool;
55 std::chrono::steady_clock::time_point tm;
56
57 private:
58 static auto Sum(const RageTimer& lhs, float tm) -> RageTimer;
59 static auto Difference(const RageTimer& lhs, const RageTimer& rhs) -> float;
60};
61
62extern const RageTimer RageZeroTimer;
63
64#endif
Definition RageTimer.h:9