TermOx
timer.hpp
1 #ifndef TERMOX_COMMON_TIMER_HPP
2 #define TERMOX_COMMON_TIMER_HPP
3 #include <chrono>
4 
5 #include <termox/common/fps.hpp>
6 
7 namespace ox {
8 
10 class Timer {
11  public:
12  using Clock_t = std::chrono::steady_clock;
13  using Time_point = Clock_t::time_point;
14  using Duration_t = std::chrono::milliseconds;
15 
16  public:
18  explicit constexpr Timer(Duration_t interval) : interval_{interval} {}
19 
21  explicit constexpr Timer(FPS fps)
22  : interval_{fps_to_period<Duration_t>(fps)}
23  {}
24 
25  public:
27  void begin();
28 
30 
32  void wait();
33 
35  void set_interval(Duration_t interval);
36 
38  [[nodiscard]] auto get_interval() const -> Duration_t;
39 
40  private:
41  Duration_t interval_;
42  Time_point last_time_;
43 
44  private:
46 
47  [[nodiscard]] auto get_sleep_time() const -> Clock_t::duration;
48 };
49 
50 } // namespace ox
51 #endif // TERMOX_COMMON_TIMER_HPP
Timer class where begin() and wait() are used to block for a given interval.
Definition: timer.hpp:10
auto get_interval() const -> Duration_t
Return the currently set interval.
Definition: timer.cpp:15
constexpr Timer(FPS fps)
Construct a Timer with the given FPS interval.
Definition: timer.hpp:21
void begin()
Start the timer, returns immediately.
Definition: timer.cpp:9
void set_interval(Duration_t interval)
Set the amount of time to wait for from begin().
Definition: timer.cpp:13
void wait()
Sleep until the time interval is over, from last begin() call.
Definition: timer.cpp:11
constexpr Timer(Duration_t interval)
Construct a Timer with the given interval.
Definition: timer.hpp:18
Frames Per Second.
Definition: fps.hpp:8