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
27{
28 return Time::FromNS(SDL_SECONDS_TO_NS(time));
29}
30
31constexpr Sint64 Time::ToPosix() const
32{
33 return SDL_NS_TO_SECONDS(m_time.count());
34}
35
49inline std::chrono::nanoseconds GetTicks()
50{
51 return std::chrono::nanoseconds(SDL_GetTicksNS());
52}
53
68inline Uint64 GetTicksMS() { return SDL_GetTicks(); }
69
80inline Uint64 GetTicksNS() { return SDL_GetTicksNS(); }
81
98inline Uint64 GetPerformanceCounter() { return SDL_GetPerformanceCounter(); }
99
112{
113 return SDL_GetPerformanceFrequency();
114}
115
132inline void Delay(Uint32 ms) { SDL_Delay(ms); }
133
149inline void Delay(std::chrono::nanoseconds duration)
150{
151 SDL_DelayNS(duration.count());
152}
153
169inline void DelayNS(Uint64 ns) { SDL_DelayNS(ns); }
170
188inline void DelayPrecise(Uint64 ns) { SDL_DelayPrecise(ns); }
189
208inline void DelayPrecise(std::chrono::nanoseconds duration)
209{
210 SDL_DelayPrecise(duration.count());
211}
212
218using TimerID = SDL_TimerID;
219
244using MSTimerCallback = Uint32(SDLCALL*)(void* userdata,
245 TimerID timerID,
246 Uint32 interval);
247
272using NSTimerCallback = Uint64(SDLCALL*)(void* userdata,
273 TimerID timerID,
274 Uint64 interval);
275
302struct TimerCB : LightweightCallbackT<TimerCB, Uint64, TimerID, Uint64>
303{
305 template<std::invocable<TimerID, std::chrono::nanoseconds> F>
306 TimerCB(const F& func)
308 {
309 }
310
312 template<std::invocable<TimerID, std::chrono::nanoseconds> F>
313 static Uint64 doCall(F& func, TimerID timerID, Uint64 interval)
314 {
315 return func(timerID, std::chrono::nanoseconds(interval)).count();
316 }
317};
318
353inline TimerID AddTimer(std::chrono::milliseconds interval,
354 MSTimerCallback callback,
355 void* userdata)
356{
357 return SDL_AddTimer(Uint32(interval.count()), callback, userdata);
358}
359
395inline TimerID AddTimer(std::chrono::nanoseconds interval,
396 NSTimerCallback callback,
397 void* userdata)
398{
399 return CheckError(SDL_AddTimerNS(interval.count(), callback, userdata));
400}
401
439inline TimerID AddTimer(std::chrono::nanoseconds interval, TimerCB callback)
440{
441 return SDL_AddTimerNS(interval.count(), callback.wrapper, callback.data);
442}
443
456inline void RemoveTimer(TimerID id) { CheckError(SDL_RemoveTimer(id)); }
457
459
460} // namespace SDL
461
462#endif /* SDL3PP_TIMER_H_ */
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition: SDL3pp_stdinc.h:414
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition: SDL3pp_stdinc.h:460
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:371
::Sint64 Sint64
A signed 64-bit integer type.
Definition: SDL3pp_stdinc.h:356
constexpr Sint64 ToPosix() const
Convert nanoseconds to seconds.
Definition: SDL3pp_timer.h:31
Uint64(SDLCALL *)(void *userdata, TimerID timerID, Uint64 interval) NSTimerCallback
Function prototype for the nanosecond timer callback function.
Definition: SDL3pp_timer.h:274
std::chrono::nanoseconds GetTicks()
Get the time elapsed since SDL library initialization.
Definition: SDL3pp_timer.h:49
Uint64 GetPerformanceFrequency()
Get the count per second of the high resolution counter.
Definition: SDL3pp_timer.h:111
SDL_TimerID TimerID
Definition of the timer ID type.
Definition: SDL3pp_timer.h:218
Uint64 GetPerformanceCounter()
Get the current value of the high resolution counter.
Definition: SDL3pp_timer.h:98
void RemoveTimer(TimerID id)
Remove a timer created with AddTimer().
Definition: SDL3pp_timer.h:456
void DelayNS(Uint64 ns)
Wait a specified number of nanoseconds before returning.
Definition: SDL3pp_timer.h:169
Uint64 GetTicksMS()
Get the number of milliseconds that have elapsed since the SDL library initialization.
Definition: SDL3pp_timer.h:68
void DelayPrecise(Uint64 ns)
Wait a specified number of nanoseconds before returning.
Definition: SDL3pp_timer.h:188
Uint32(SDLCALL *)(void *userdata, TimerID timerID, Uint32 interval) MSTimerCallback
Function prototype for the millisecond timer callback function.
Definition: SDL3pp_timer.h:246
Uint64 GetTicksNS()
Get the number of nanoseconds since SDL library initialization.
Definition: SDL3pp_timer.h:80
void Delay(Uint32 ms)
Wait a specified number of milliseconds before returning.
Definition: SDL3pp_timer.h:132
static constexpr Time FromPosix(Sint64 time)
Convert seconds to nanoseconds.
Definition: SDL3pp_timer.h:26
TimerID AddTimer(std::chrono::milliseconds interval, MSTimerCallback callback, void *userdata)
Call a callback function at a future time.
Definition: SDL3pp_timer.h:353
Main include header for the SDL3pp library.
Lightweight wrapper.
Definition: SDL3pp_callbackWrapper.h:109
void * data
The wrapped data.
Definition: SDL3pp_callbackWrapper.h:114
R(* wrapper)(void *, PARAMS...)
The wrapper function.
Definition: SDL3pp_callbackWrapper.h:111
Function prototype for the nanosecond timer callback function.
Definition: SDL3pp_timer.h:303
TimerCB(const F &func)
ctor
Definition: SDL3pp_timer.h:306