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
Stopwatch.cpp
Go to the documentation of this file.
1
7
8#include <chrono>
9
10#include "galaxy/time/Time.hpp"
11
12#include "Stopwatch.hpp"
13
14namespace galaxy
15{
17 : m_stopped {true}
18 , m_paused {false}
19 , m_repeat {false}
20 , m_start_ticks {0}
21 , m_paused_ticks {0}
22 , m_delay {1000}
23 , m_callback {nullptr}
24 {
25 }
26
28 {
29 stop();
30 }
31
32 void Stopwatch::set(Stopwatch::Function&& func, const std::uint64_t delay)
33 {
34 m_callback = std::move(func);
35 m_delay = delay;
36 }
37
38 void Stopwatch::repeat(const bool repeat)
39 {
41 }
42
44 {
45 m_stopped = false;
46 m_paused = false;
47
50 }
51
53 {
54 m_stopped = true;
55 m_paused = false;
56
57 m_start_ticks = 0;
59 }
60
62 {
63 if (!m_stopped && !m_paused)
64 {
65 m_paused = true;
66
68 m_start_ticks = 0;
69 }
70 }
71
73 {
74 if (!m_stopped && m_paused)
75 {
76 m_paused = false;
77
80 }
81 }
82
84 {
85 if (!m_stopped && !m_paused)
86 {
87 if (get_time_passed() >= m_delay)
88 {
89 m_callback();
90
91 if (!m_repeat)
92 {
93 stop();
94 }
95 }
96 }
97 }
98
99 bool Stopwatch::stopped() const
100 {
101 return m_stopped;
102 }
103
104 bool Stopwatch::paused() const
105 {
106 return m_paused && !m_stopped;
107 }
108
109 std::uint64_t Stopwatch::get_time_passed() const noexcept
110 {
111 std::uint64_t time = 0;
112
113 if (!m_stopped)
114 {
115 if (m_paused)
116 {
118 }
119 else
120 {
122 }
123 }
124
125 // We need to convert because time_passed is in MS while
126 // timer is in NS.
127 return time / 1000000;
128 }
129} // namespace galaxy
void stop()
Stop Stopwatch.
Definition Stopwatch.cpp:52
void pause()
Pause the Stopwatch.
Definition Stopwatch.cpp:61
std::uint64_t m_start_ticks
The clock time when the timer started.
void unpause()
Resume the Stopwatch.
Definition Stopwatch.cpp:72
void start()
Start Stopwatch.
Definition Stopwatch.cpp:43
void set(Stopwatch::Function &&func, const std::uint64_t delay)
Run a function on a precision Stopwatch.
Definition Stopwatch.cpp:32
void repeat(const bool repeat)
Make function repeat itself instead of running once.
Definition Stopwatch.cpp:38
bool paused() const
Is the Stopwatch paused?
bool m_paused
Is Stopwatch paused.
bool m_stopped
Is Stopwatch stopped.
Stopwatch()
Constructor.
Definition Stopwatch.cpp:16
void update()
Call this if you want to trigger the callback after delay has passed.
Definition Stopwatch.cpp:83
~Stopwatch()
Destructor.
Definition Stopwatch.cpp:27
std::move_only_function< void(void)> Function
Stopwatch callback type.
Definition Stopwatch.hpp:24
std::uint64_t get_time_passed() const noexcept
Time passed in milliseconds.
std::uint64_t m_paused_ticks
The ticks stored when the timer was paused.
bool m_repeat
Is function repeating?
Stopwatch::Function m_callback
Callback function.
std::uint64_t m_delay
Duration before callback is invoked.
bool stopped() const
Is the Stopwatch finished?
Definition Stopwatch.cpp:99
std::uint64_t time_since_epoch() noexcept
Time since epoch.
Definition Time.cpp:21
Application.hpp galaxy.