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.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_TIME_STOPWATCH_HPP_
9#define GALAXY_TIME_STOPWATCH_HPP_
10
11#include <functional>
12
13namespace galaxy
14{
18 class Stopwatch final
19 {
20 public:
24 using Function = std::move_only_function<void(void)>;
25
29 Stopwatch();
30
34 ~Stopwatch();
35
42 void set(Stopwatch::Function&& func, const std::uint64_t delay);
43
49 void repeat(const bool repeat);
50
54 void start();
55
59 void stop();
60
64 void pause();
65
69 void unpause();
70
77 void update();
78
84 [[nodiscard]]
85 bool stopped() const;
86
92 [[nodiscard]]
93 bool paused() const;
94
100 [[nodiscard]]
101 std::uint64_t get_time_passed() const noexcept;
102
103 private:
107 Stopwatch(const Stopwatch&) = delete;
108
112 Stopwatch& operator=(const Stopwatch&) = delete;
113
114 private:
119
124
129
134
139
143 std::uint64_t m_delay;
144
149 };
150} // namespace galaxy
151
152#endif
Synchronous stopwatch.
Definition Stopwatch.hpp:19
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
STL namespace.