TermOx
animation_engine.hpp
1 #ifndef TERMOX_SYSTEM_ANIMATION_ENGINE_HPP
2 #define TERMOX_SYSTEM_ANIMATION_ENGINE_HPP
3 #include <map>
4 #include <mutex>
5 
6 #include <termox/common/lockable.hpp>
7 #include <termox/common/timer.hpp>
8 #include <termox/system/event_loop.hpp>
9 #include <termox/system/event_queue.hpp>
10 
11 namespace ox {
12 class Widget;
13 
15 class Animation_engine : private Lockable<std::recursive_mutex> {
16  public:
17  using Clock_t = Timer::Clock_t;
18  using Duration_t = Timer::Duration_t;
19  using Time_point = Timer::Time_point;
20 
21  struct Registered_data {
22  Duration_t interval;
23  Time_point last_event_time;
24  };
25 
26  static auto constexpr default_interval = Duration_t{100};
27 
28  public:
30  void register_widget(Widget& w, Duration_t interval);
31 
33  void register_widget(Widget& w, FPS fps);
34 
36  void unregister_widget(Widget& w);
37 
39  [[nodiscard]] auto is_empty() const -> bool;
40 
42  void start();
43 
45  void stop();
46 
48  [[nodiscard]] auto is_running() const -> bool;
49 
50  private:
51  std::map<Widget*, Registered_data> subjects_;
52  Event_loop loop_;
53  Timer timer_ = Timer{default_interval};
54 
55  private:
57  auto get_timer_events() -> std::vector<Timer_event>&;
58 
60  void loop_function(Event_queue& queue);
61 };
62 
63 } // namespace ox
64 #endif // TERMOX_SYSTEM_ANIMATION_ENGINE_HPP
Registers Widgets with intervals to send timer events.
Definition: animation_engine.hpp:15
auto is_running() const -> bool
Return true if start() has been called, and hasn't been exited.
Definition: animation_engine.cpp:54
void register_widget(Widget &w, Duration_t interval)
Register to start sending Timer_events to w every interval.
Definition: animation_engine.cpp:24
auto is_empty() const -> bool
Return true if there are no registered widgets.
Definition: animation_engine.cpp:41
void start()
Start another thread that waits on intervals and sents timer events.
Definition: animation_engine.cpp:43
void stop()
Sends exit signal and waits for animation thread to exit.
Definition: animation_engine.cpp:48
void unregister_widget(Widget &w)
Stop the given Widget from being sent Timer_events.
Definition: animation_engine.cpp:35
Calls on loop_function(), and then processes the Event_queue.
Definition: event_loop.hpp:17
Definition: lockable.hpp:8
Timer class where begin() and wait() are used to block for a given interval.
Definition: timer.hpp:10
Definition: widget.hpp:31
Definition: animation_engine.hpp:21
Frames Per Second.
Definition: fps.hpp:8