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
36constexpr Uint32 MS_PER_SECOND = SDL_MS_PER_SECOND;
37
45constexpr Uint32 US_PER_SECOND = SDL_US_PER_SECOND;
46
54constexpr Uint64 NS_PER_SECOND = SDL_NS_PER_SECOND;
55
63constexpr Uint32 NS_PER_MS = SDL_NS_PER_MS;
64
72constexpr Uint32 NS_PER_US = SDL_NS_PER_US;
73
86constexpr Uint64 SecondsToNs(Number auto S) { return SDL_SECONDS_TO_NS(S); }
87
100constexpr Time TimeFromPosix(Sint64 time)
101{
102 return Time::FromNS(SecondsToNs(time));
103}
104
105constexpr Time Time::FromPosix(Sint64 time)
106{
107 return Time::FromNS(SecondsToNs(time));
108}
109
123constexpr auto NsToSeconds(Number auto NS) { return SDL_NS_TO_SECONDS(NS); }
124
137constexpr Sint64 TimeToPosix(Time time) { return NsToSeconds(time.ToPosix()); }
138
139constexpr Sint64 Time::ToPosix() const { return NsToSeconds(m_time.count()); }
140
153constexpr Uint64 MsToNs(Number auto MS) { return SDL_MS_TO_NS(MS); }
154
168constexpr auto NsToMs(Number auto NS) { return SDL_NS_TO_MS(NS); }
169
182constexpr Uint64 UsToNs(Number auto US) { return SDL_US_TO_NS(US); }
183
197constexpr auto NsToUs(Number auto NS) { return SDL_NS_TO_US(NS); }
198
212inline Nanoseconds GetTicks() { return Nanoseconds(SDL_GetTicksNS()); }
213
228inline Uint64 GetTicksMS() { return SDL_GetTicks(); }
229
240inline Uint64 GetTicksNS() { return SDL_GetTicksNS(); }
241
258inline Uint64 GetPerformanceCounter() { return SDL_GetPerformanceCounter(); }
259
272{
273 return SDL_GetPerformanceFrequency();
274}
275
292inline void Delay(Uint32 ms) { SDL_Delay(ms); }
293
309inline void Delay(Nanoseconds duration) { SDL_DelayNS(duration.count()); }
310
326inline void DelayNS(Uint64 ns) { SDL_DelayNS(ns); }
327
345inline void DelayPrecise(Uint64 ns) { SDL_DelayPrecise(ns); }
346
365inline void DelayPrecise(Nanoseconds duration)
366{
367 SDL_DelayPrecise(duration.count());
368}
369
375using TimerID = SDL_TimerID;
376
401using MSTimerCallback = Uint32(SDLCALL*)(void* userdata,
402 TimerID timerID,
403 Uint32 interval);
404
429using NSTimerCallback = Uint64(SDLCALL*)(void* userdata,
430 TimerID timerID,
431 Uint64 interval);
432
459struct TimerCB : LightweightCallbackT<TimerCB, Uint64, TimerID, Uint64>
460{
462 template<std::invocable<TimerID, Nanoseconds> F>
463 TimerCB(const F& func)
465 {
466 }
467
469 template<std::invocable<TimerID, Nanoseconds> F>
470 static Uint64 doCall(F& func, TimerID timerID, Uint64 interval)
471 {
472 return func(timerID, std::chrono::nanoseconds(interval)).count();
473 }
474};
475
511 MSTimerCallback callback,
512 void* userdata)
513{
514 return SDL_AddTimer(Uint32(interval.count()), callback, userdata);
515}
516
553 NSTimerCallback callback,
554 void* userdata)
555{
556 return CheckError(SDL_AddTimerNS(interval.count(), callback, userdata));
557}
558
596inline TimerID AddTimer(Nanoseconds interval, TimerCB callback)
597{
598 return SDL_AddTimerNS(interval.count(), callback.wrapper, callback.data);
599}
600
613inline void RemoveTimer(TimerID id) { CheckError(SDL_RemoveTimer(id)); }
614
616
617} // namespace SDL
618
619#endif /* SDL3PP_TIMER_H_ */
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition SDL3pp_stdinc.h:369
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition SDL3pp_stdinc.h:417
Either integral or floating point.
Definition SDL3pp_stdinc.h:6283
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
std::chrono::nanoseconds Nanoseconds
Duration in Nanoseconds (Uint64).
Definition SDL3pp_stdinc.h:338
::Sint64 Sint64
A signed 64-bit integer type.
Definition SDL3pp_stdinc.h:311
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:296
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition SDL3pp_stdinc.h:341
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:326
constexpr Sint64 ToPosix() const
Convert nanoseconds to seconds.
Definition SDL3pp_timer.h:139
constexpr Uint64 UsToNs(Number auto US)
Convert microseconds to nanoseconds.
Definition SDL3pp_timer.h:182
Uint32(SDLCALL *)(void *userdata, TimerID timerID, Uint32 interval) MSTimerCallback
Function prototype for the millisecond timer callback function.
Definition SDL3pp_timer.h:401
constexpr auto NsToMs(Number auto NS)
Convert nanoseconds to milliseconds.
Definition SDL3pp_timer.h:168
Uint64 GetPerformanceFrequency()
Get the count per second of the high resolution counter.
Definition SDL3pp_timer.h:271
Uint64 GetPerformanceCounter()
Get the current value of the high resolution counter.
Definition SDL3pp_timer.h:258
constexpr Uint32 MS_PER_SECOND
Number of milliseconds in a second.
Definition SDL3pp_timer.h:36
constexpr Uint32 NS_PER_US
Number of nanoseconds in a microsecond.
Definition SDL3pp_timer.h:72
void RemoveTimer(TimerID id)
Remove a timer created with AddTimer().
Definition SDL3pp_timer.h:613
constexpr Uint64 NS_PER_SECOND
Number of nanoseconds in a second.
Definition SDL3pp_timer.h:54
SDL_TimerID TimerID
Definition of the timer ID type.
Definition SDL3pp_timer.h:375
void DelayNS(Uint64 ns)
Wait a specified number of nanoseconds before returning.
Definition SDL3pp_timer.h:326
constexpr auto NsToUs(Number auto NS)
Convert nanoseconds to microseconds.
Definition SDL3pp_timer.h:197
Uint64 GetTicksMS()
Get the number of milliseconds that have elapsed since the SDL library initialization.
Definition SDL3pp_timer.h:228
void DelayPrecise(Uint64 ns)
Wait a specified number of nanoseconds before returning.
Definition SDL3pp_timer.h:345
constexpr Time TimeFromPosix(Sint64 time)
Convert seconds to nanoseconds.
Definition SDL3pp_timer.h:100
TimerID AddTimer(Milliseconds interval, MSTimerCallback callback, void *userdata)
Call a callback function at a future time.
Definition SDL3pp_timer.h:510
constexpr auto NsToSeconds(Number auto NS)
Convert nanoseconds to seconds.
Definition SDL3pp_timer.h:123
constexpr Uint32 NS_PER_MS
Number of nanoseconds in a millisecond.
Definition SDL3pp_timer.h:63
constexpr Uint32 US_PER_SECOND
Number of microseconds in a second.
Definition SDL3pp_timer.h:45
Uint64 GetTicksNS()
Get the number of nanoseconds since SDL library initialization.
Definition SDL3pp_timer.h:240
void Delay(Uint32 ms)
Wait a specified number of milliseconds before returning.
Definition SDL3pp_timer.h:292
Uint64(SDLCALL *)(void *userdata, TimerID timerID, Uint64 interval) NSTimerCallback
Function prototype for the nanosecond timer callback function.
Definition SDL3pp_timer.h:429
static constexpr Time FromPosix(Sint64 time)
Convert seconds to nanoseconds.
Definition SDL3pp_timer.h:105
constexpr Sint64 TimeToPosix(Time time)
Convert nanoseconds to seconds.
Definition SDL3pp_timer.h:137
constexpr Uint64 SecondsToNs(Number auto S)
Convert seconds to nanoseconds.
Definition SDL3pp_timer.h:86
constexpr Uint64 MsToNs(Number auto MS)
Convert milliseconds to nanoseconds.
Definition SDL3pp_timer.h:153
Nanoseconds GetTicks()
Get the time elapsed since SDL library initialization.
Definition SDL3pp_timer.h:212
Main include header for the SDL3pp library.
void * data
The wrapped data.
Definition SDL3pp_callbackWrapper.h:114
LightweightCallbackT(const F &func)
Definition SDL3pp_callbackWrapper.h:118
R(* wrapper)(void *, PARAMS...)
The wrapper function.
Definition SDL3pp_callbackWrapper.h:111
Function prototype for the nanosecond timer callback function.
Definition SDL3pp_timer.h:460
TimerCB(const F &func)
ctor
Definition SDL3pp_timer.h:463