SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_timer.h
1#ifndef SDL3PP_TIMER_H_
2#define SDL3PP_TIMER_H_
3
4#include <SDL3/SDL_timer.h>
5#include "SDL3pp_stdinc.h"
6
7namespace SDL {
8
28{
29 return Time::FromNS(SDL_SECONDS_TO_NS(time));
30}
31
32constexpr Sint64 Time::ToPosix() const
33{
34 return SDL_NS_TO_SECONDS(m_time.count());
35}
36
47inline std::chrono::nanoseconds GetTicks()
48{
49 return std::chrono::nanoseconds{SDL_GetTicksNS()};
50}
51
69inline Uint64 GetPerformanceCounter() { return SDL_GetPerformanceCounter(); }
70
83{
84 return SDL_GetPerformanceFrequency();
85}
86
102inline void Delay(std::chrono::nanoseconds duration)
103{
104 SDL_DelayNS(duration.count());
105}
106
122inline void DelayPrecise(std::chrono::nanoseconds duration)
123{
124 SDL_DelayPrecise(duration.count());
125}
126
132using TimerID = SDL_TimerID;
133
158using TimerCallback = SDL_NSTimerCallback;
159
185using TimerCB =
186 std::function<std::chrono::nanoseconds(TimerID, std::chrono::nanoseconds)>;
187
222inline TimerID AddTimer(std::chrono::nanoseconds interval,
223 TimerCallback callback,
224 void* userdata)
225{
226 return CheckError(SDL_AddTimerNS(interval.count(), callback, userdata));
227}
228
265inline TimerID AddTimer(std::chrono::nanoseconds interval, TimerCB callback)
266{
267 using Wrapper = CallbackWrapper<TimerCB>;
269
270 auto cb = Wrapper::Wrap(std::move(callback));
271
272 if (TimerID id = SDL_AddTimerNS(
273 interval.count(),
274 [](void* userdata, TimerID timerID, Uint64 interval) -> Uint64 {
275 auto& f = *static_cast<TimerCB*>(userdata);
276 auto next = f(timerID, std::chrono::nanoseconds(interval)).count();
277 // If ask to removal, then remove it
278 if (next == 0) delete Store::release(timerID);
279 return next;
280 },
281 cb)) {
282 Store::Wrap(id, std::move(cb));
283 return id;
284 }
285 delete cb;
286 throw Error{};
287}
288
301inline void RemoveTimer(TimerID id)
302{
304 CheckError(SDL_RemoveTimer(id));
305}
306
308
309} // namespace SDL
310
311#endif /* SDL3PP_TIMER_H_ */
An exception that returns GetError()
Definition: SDL3pp_error.h:165
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition: SDL3pp_stdinc.h:383
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition: SDL3pp_stdinc.h:429
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:340
::Sint64 Sint64
A signed 64-bit integer type.
Definition: SDL3pp_stdinc.h:325
constexpr Sint64 ToPosix() const
Convert nanoseconds to seconds.
Definition: SDL3pp_timer.h:32
void Delay(std::chrono::nanoseconds duration)
Wait a specified duration before returning.
Definition: SDL3pp_timer.h:102
TimerID AddTimer(std::chrono::nanoseconds interval, TimerCallback callback, void *userdata)
Call a callback function at a future time.
Definition: SDL3pp_timer.h:222
void DelayPrecise(std::chrono::nanoseconds duration)
Wait a specified duration before returning.
Definition: SDL3pp_timer.h:122
std::function< std::chrono::nanoseconds(TimerID, std::chrono::nanoseconds)> TimerCB
Function prototype for the nanosecond timer callback function.
Definition: SDL3pp_timer.h:186
std::chrono::nanoseconds GetTicks()
Get the time elapsed since SDL library initialization.
Definition: SDL3pp_timer.h:47
Uint64 GetPerformanceFrequency()
Get the count per second of the high resolution counter.
Definition: SDL3pp_timer.h:82
SDL_TimerID TimerID
Definition of the timer ID type.
Definition: SDL3pp_timer.h:132
Uint64 GetPerformanceCounter()
Get the current value of the high resolution counter.
Definition: SDL3pp_timer.h:69
void RemoveTimer(TimerID id)
Remove a timer created with SDL_AddTimer().
Definition: SDL3pp_timer.h:301
SDL_NSTimerCallback TimerCallback
Function prototype for the nanosecond timer callback function.
Definition: SDL3pp_timer.h:158
static constexpr Time FromPosix(Sint64 time)
Convert seconds to nanoseconds.
Definition: SDL3pp_timer.h:27
Main include header for the SDL3pp library.
Definition: SDL3pp_callbackWrapper.h:66
Wrapper key to value result callbacks.
Definition: SDL3pp_callbackWrapper.h:145
static ValueType release(KeyType key)
Return unwrapped value associated by key and remove association.
Definition: SDL3pp_callbackWrapper.h:183