galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Timer.cpp
Go to the documentation of this file.
1
7
8#include <BS_thread_pool.hpp>
9#include <entt/locator/locator.hpp>
10
11// #include "galaxy/core/Settings.hpp"
12
13using namespace std::chrono_literals;
14
15#include "Timer.hpp"
16
17namespace galaxy
18{
19 namespace async
20 {
21 Timer::Timer() noexcept
22 : m_stopped {true}
23 , m_paused {false}
24 , m_repeat {false}
25 , m_delay {1000}
26 , m_time_passed {0.0}
27 , m_handle {}
28 , m_callback {nullptr}
29 {
30 }
31
32 Timer::Timer(Timer::Function&& func, const std::uint32_t delay) noexcept
33 : m_stopped {true}
34 , m_paused {false}
35 , m_repeat {false}
36 , m_delay {1000}
37 , m_time_passed {0.0}
38 , m_handle {}
39 , m_callback {nullptr}
40 {
41 set(std::move(func), delay);
42 }
43
44 Timer::~Timer() noexcept
45 {
46 stop();
47 }
48
49 void Timer::set(Timer::Function&& func, const std::uint32_t delay) noexcept
50 {
51 m_callback = std::move(func);
52 m_delay = delay;
53 }
54
55 void Timer::repeat(const bool repeat) noexcept
56 {
57 m_repeat = repeat;
58 }
59
61 {
62 if (m_stopped && !m_paused)
63 {
64 m_stopped = false;
65 m_paused = false;
66
67 auto& tp = entt::locator<BS::light_thread_pool>::value();
68 m_handle = tp.submit_task([&]() {
69 do
70 {
71 if (!m_paused)
72 {
73 std::this_thread::sleep_for(std::chrono::milliseconds(m_delay));
74 m_callback();
75 }
76 else
77 {
78 // Prevent excess CPU usage.
79 std::this_thread::sleep_for(0.1ms);
80 }
81 } while (m_repeat && !m_stopped);
82 });
83 }
84 }
85
86 void Timer::stop() noexcept
87 {
88 m_stopped = true;
89 m_repeat = false;
90 m_paused = false;
91 m_time_passed = 0.0;
92
93 m_handle.get();
94 }
95
96 void Timer::pause(const bool pause) noexcept
97 {
98 m_paused = pause;
99 }
100
101 bool Timer::stopped() const noexcept
102 {
103 return m_stopped;
104 }
105
106 bool Timer::paused() const noexcept
107 {
108 return m_paused;
109 }
110 } // namespace async
111} // namespace galaxy
void set(Timer::Function &&func, const std::uint32_t delay) noexcept
Run a function on a precision timer.
Definition Timer.cpp:49
std::uint32_t m_delay
Current delay on timer.
Definition Timer.hpp:125
void stop() noexcept
Stop timer.
Definition Timer.cpp:86
void repeat(const bool repeat) noexcept
Make function repeat itself instead of running once.
Definition Timer.cpp:55
bool paused() const noexcept
Is the timer paused?
Definition Timer.cpp:106
Timer() noexcept
Constructor.
Definition Timer.cpp:21
double m_time_passed
Time passed.
Definition Timer.hpp:130
std::atomic_bool m_stopped
Is timer stopped.
Definition Timer.hpp:110
void start()
Start timer.
Definition Timer.cpp:60
Timer::Function m_callback
Callback function.
Definition Timer.hpp:140
std::atomic_bool m_paused
Is timer paused.
Definition Timer.hpp:115
std::future< void > m_handle
Thread running task.
Definition Timer.hpp:135
void pause(const bool pause) noexcept
Pause the timer.
Definition Timer.cpp:96
~Timer() noexcept
Destructor.
Definition Timer.cpp:44
std::move_only_function< void(void)> Function
Timer callback type.
Definition Timer.hpp:27
std::atomic_bool m_repeat
Is function repeating?
Definition Timer.hpp:120
bool stopped() const noexcept
Is the timer finished?
Definition Timer.cpp:101
Timer.hpp galaxy.
Definition Async.hpp:17