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 <chrono>
5#include <functional>
6#include <SDL3/SDL_timer.h>
7#include "SDL3pp_callbackWrapper.h"
8#include "SDL3pp_error.h"
9#include "SDL3pp_stdinc.h"
10
11namespace SDL {
12
43constexpr Time Time::FromPosix(Sint64 time)
44{
45 return Time::FromNS(SDL_SECONDS_TO_NS(time));
46}
47
59constexpr Sint64 Time::ToPosix() const
60{
61 return SDL_NS_TO_SECONDS(m_value.count());
62}
63
74inline std::chrono::nanoseconds GetTicks()
75{
76 return std::chrono::nanoseconds{SDL_GetTicksNS()};
77}
78
96inline Uint64 GetPerformanceCounter() { return SDL_GetPerformanceCounter(); }
97
110{
111 return SDL_GetPerformanceFrequency();
112}
113
126inline void Delay(std::chrono::nanoseconds duration)
127{
128 SDL_DelayNS(duration.count());
129}
130
147inline void DelayPrecise(std::chrono::nanoseconds duration)
148{
149 SDL_DelayPrecise(duration.count());
150}
156using TimerID = SDL_TimerID;
157
182using TimerCallback = SDL_NSTimerCallback;
183
209using TimerCB =
210 std::function<std::chrono::nanoseconds(TimerID, std::chrono::nanoseconds)>;
211
246inline TimerID AddTimer(std::chrono::nanoseconds interval,
247 TimerCallback callback,
248 void* userdata)
249{
250 return CheckError(SDL_AddTimerNS(interval.count(), callback, userdata));
251}
252
289inline TimerID AddTimer(std::chrono::nanoseconds interval, TimerCB callback)
290{
291 using Wrapper = CallbackWrapper<TimerCB>;
293
294 auto cb = Wrapper::Wrap(std::move(callback));
295
296 if (TimerID id = SDL_AddTimerNS(
297 interval.count(),
298 [](void* userdata, TimerID timerID, Uint64 interval) -> Uint64 {
299 auto& f = *static_cast<TimerCB*>(userdata);
300 auto next = f(timerID, std::chrono::nanoseconds(interval)).count();
301 // If ask to removal, then remove it
302 if (next == 0) delete Store::release(timerID);
303 return next;
304 },
305 cb)) {
306 Store::Wrap(id, std::move(cb));
307 return id;
308 }
309 delete cb;
310 throw Error{};
311}
312
325inline void RemoveTimer(TimerID id)
326{
328 CheckError(SDL_RemoveTimer(id));
329}
330
332
333} // namespace SDL
334
335#endif /* SDL3PP_TIMER_H_ */
An exception that returns GetError()
Definition SDL3pp_error.h:167
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition SDL3pp_stdinc.h:261
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition SDL3pp_stdinc.h:291
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
constexpr Sint64 ToPosix() const
Convert nanoseconds to seconds.
Definition SDL3pp_timer.h:59
void Delay(std::chrono::nanoseconds duration)
Wait a specified duration before returning.
Definition SDL3pp_timer.h:126
TimerID AddTimer(std::chrono::nanoseconds interval, TimerCallback callback, void *userdata)
Call a callback function at a future time.
Definition SDL3pp_timer.h:246
void DelayPrecise(std::chrono::nanoseconds duration)
Wait a specified duration before returning.
Definition SDL3pp_timer.h:147
std::function< std::chrono::nanoseconds(TimerID, std::chrono::nanoseconds)> TimerCB
Function prototype for the nanosecond timer callback function.
Definition SDL3pp_timer.h:210
std::chrono::nanoseconds GetTicks()
Get the time elapsed since SDL library initialization.
Definition SDL3pp_timer.h:74
Uint64 GetPerformanceFrequency()
Get the count per second of the high resolution counter.
Definition SDL3pp_timer.h:109
SDL_TimerID TimerID
Definition of the timer ID type.
Definition SDL3pp_timer.h:156
Uint64 GetPerformanceCounter()
Get the current value of the high resolution counter.
Definition SDL3pp_timer.h:96
void RemoveTimer(TimerID id)
Remove a timer created with AddTimer().
Definition SDL3pp_timer.h:325
SDL_NSTimerCallback TimerCallback
Function prototype for the nanosecond timer callback function.
Definition SDL3pp_timer.h:182
static constexpr Time FromPosix(Sint64 time)
Convert seconds to nanoseconds.
Definition SDL3pp_timer.h:43
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
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