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