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
42constexpr Time Time::FromPosix(Sint64 time)
43{
44 return Time::FromNS(SDL_SECONDS_TO_NS(time));
45}
46
58constexpr Sint64 Time::ToPosix() const
59{
60 return SDL_NS_TO_SECONDS(m_value.count());
61}
62
73inline std::chrono::nanoseconds GetTicks()
74{
75 return std::chrono::nanoseconds{SDL_GetTicksNS()};
76}
77
95inline Uint64 GetPerformanceCounter() { return SDL_GetPerformanceCounter(); }
96
109{
110 return SDL_GetPerformanceFrequency();
111}
112
128inline void Delay(std::chrono::nanoseconds duration)
129{
130 SDL_DelayNS(duration.count());
131}
132
148inline void DelayPrecise(std::chrono::nanoseconds duration)
149{
150 SDL_DelayPrecise(duration.count());
151}
157using TimerID = SDL_TimerID;
158
183using TimerCallback = SDL_NSTimerCallback;
184
210using TimerCB =
211 std::function<std::chrono::nanoseconds(TimerID, std::chrono::nanoseconds)>;
212
247inline TimerID AddTimer(std::chrono::nanoseconds interval,
248 TimerCallback callback,
249 void* userdata)
250{
251 return CheckError(SDL_AddTimerNS(interval.count(), callback, userdata));
252}
253
290inline TimerID AddTimer(std::chrono::nanoseconds interval, TimerCB callback)
291{
292 using Wrapper = CallbackWrapper<TimerCB>;
294
295 auto cb = Wrapper::Wrap(std::move(callback));
296
297 if (TimerID id = SDL_AddTimerNS(
298 interval.count(),
299 [](void* userdata, TimerID timerID, Uint64 interval) -> Uint64 {
300 auto& f = *static_cast<TimerCB*>(userdata);
301 auto next = f(timerID, std::chrono::nanoseconds(interval)).count();
302 // If ask to removal, then remove it
303 if (next == 0) delete Store::release(timerID);
304 return next;
305 },
306 cb)) {
307 Store::Wrap(id, std::move(cb));
308 return id;
309 }
310 delete cb;
311 throw Error{};
312}
313
326inline void RemoveTimer(TimerID id)
327{
329 CheckError(SDL_RemoveTimer(id));
330}
331
333
334} // namespace SDL
335
336#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:293
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition SDL3pp_stdinc.h:323
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:58
void Delay(std::chrono::nanoseconds duration)
Wait a specified duration before returning.
Definition SDL3pp_timer.h:128
TimerID AddTimer(std::chrono::nanoseconds interval, TimerCallback callback, void *userdata)
Call a callback function at a future time.
Definition SDL3pp_timer.h:247
void DelayPrecise(std::chrono::nanoseconds duration)
Wait a specified duration before returning.
Definition SDL3pp_timer.h:148
std::function< std::chrono::nanoseconds(TimerID, std::chrono::nanoseconds)> TimerCB
Function prototype for the nanosecond timer callback function.
Definition SDL3pp_timer.h:211
std::chrono::nanoseconds GetTicks()
Get the time elapsed since SDL library initialization.
Definition SDL3pp_timer.h:73
Uint64 GetPerformanceFrequency()
Get the count per second of the high resolution counter.
Definition SDL3pp_timer.h:108
SDL_TimerID TimerID
Definition of the timer ID type.
Definition SDL3pp_timer.h:157
Uint64 GetPerformanceCounter()
Get the current value of the high resolution counter.
Definition SDL3pp_timer.h:95
void RemoveTimer(TimerID id)
Remove a timer created with SDL_AddTimer().
Definition SDL3pp_timer.h:326
SDL_NSTimerCallback TimerCallback
Function prototype for the nanosecond timer callback function.
Definition SDL3pp_timer.h:183
static constexpr Time FromPosix(Sint64 time)
Convert seconds to nanoseconds.
Definition SDL3pp_timer.h:42
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