SDL provides time management functionality.
It is useful for dealing with (usually) small durations of time.
This is not to be confused with calendar time management, which is provided by CategoryTime.
This category covers measuring time elapsed (GetTicks(), GetPerformanceCounter()), putting a thread to sleep for a certain amount of time (Delay(), DelayNS(), DelayPrecise()), and firing a callback function after a certain amount of time has elapsed (AddTimer(), etc).
Initial value:Uint32(SDLCALL*)(
void* userdata,
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:290
SDL_TimerID TimerID
Definition of the timer ID type.
Definition SDL3pp_timer.h:218
Function prototype for the millisecond timer callback function.
The callback function is passed the current timer interval and returns the next timer interval, in milliseconds. If the returned value is the same as the one passed in, the periodic alarm continues, otherwise a new alarm is scheduled. If the callback returns 0, the periodic alarm is canceled and will be removed.
- Parameters
-
| userdata | an arbitrary pointer provided by the app through AddTimer, for its own use. |
| timerID | the current timer being processed. |
| interval | the current callback time interval. |
- Returns
- the new callback time interval, or 0 to disable further runs of the callback.
- Thread safety:
- SDL may call this callback at any time from a background thread; the application is responsible for locking resources the callback touches that need to be protected.
- Since
- This datatype is available since SDL 3.2.0.
- See also
- AddTimer
Initial value:Uint64(SDLCALL*)(
void* userdata,
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:320
Function prototype for the nanosecond timer callback function.
The callback function is passed the current timer interval and returns the next timer interval, in nanoseconds. If the returned value is the same as the one passed in, the periodic alarm continues, otherwise a new alarm is scheduled. If the callback returns 0, the periodic alarm is canceled and will be removed.
- Parameters
-
| userdata | an arbitrary pointer provided by the app through AddTimer, for its own use. |
| timerID | the current timer being processed. |
| interval | the current callback time interval. |
- Returns
- the new callback time interval, or 0 to disable further runs of the callback.
- Thread safety:
- SDL may call this callback at any time from a background thread; the application is responsible for locking resources the callback touches that need to be protected.
- Since
- This datatype is available since SDL 3.2.0.
- See also
- AddTimer
Call a callback function at a future time.
The callback function is passed the current timer interval and the user supplied parameter from the AddTimer() call and should return the next timer interval. If the value returned from the callback is 0, the timer is canceled and will be removed.
The callback is run on a separate thread, and for short timeouts can potentially be called before this function returns.
Timers take into account the amount of time it took to execute the callback. For example, if the callback took 250 ms to execute and returned 1000 (ms), the timer would only wait another 750 ms before its next iteration.
Timing may be inexact due to OS scheduling. Be sure to note the current time with GetTicksNS() or GetPerformanceCounter() in case your callback needs to adjust for variances.
- Parameters
-
| interval | the timer delay, in std::chrono::nanoseconds, passed to callback. |
| callback | the NSTimerCallback function to call when the specified interval elapses. |
| userdata | a pointer that is passed to callback. |
- Returns
- a timer ID or 0 on failure; call GetError() for more information.
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- AddTimer(std::chrono::nanoseconds, NSTimerCallback, void*)
-
AddTimer(std::chrono::nanoseconds, TimerCB)
-
RemoveTimer
Call a callback function at a future time.
The callback function is passed the current timer interval and the user supplied parameter from the AddTimer() call and should return the next timer interval. If the value returned from the callback is 0, the timer is canceled and will be removed.
The callback is run on a separate thread, and for short timeouts can potentially be called before this function returns.
Timers take into account the amount of time it took to execute the callback. For example, if the callback took 250 ns to execute and returned 1000 (ns), the timer would only wait another 750 ns before its next iteration.
Timing may be inexact due to OS scheduling. Be sure to note the current time with GetTicksNS() or GetPerformanceCounter() in case your callback needs to adjust for variances.
- Parameters
-
| interval | the timer delay, in std::chrono::nanoseconds, passed to callback. |
| callback | the NSTimerCallback function to call when the specified interval elapses. |
| userdata | a pointer that is passed to callback. |
- Returns
- a timer ID.
- Exceptions
-
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- AddTimer(std::chrono::milliseconds, MSTimerCallback, void*)
-
AddTimer(std::chrono::nanoseconds, TimerCB)
-
RemoveTimer
| TimerID SDL::AddTimer |
( |
std::chrono::nanoseconds | interval, |
|
|
TimerCB | callback ) |
|
inline |
Call a callback function at a future time.
The callback function is passed the current timer interval and the user supplied parameter from the AddTimer() call and should return the next timer interval. If the value returned from the callback is 0, the timer is canceled and will be removed.
The callback is run on a separate thread, and for short timeouts can potentially be called before this function returns.
Timers take into account the amount of time it took to execute the callback. For example, if the callback took 250 ns to execute and returned 1000 (ns), the timer would only wait another 750 ns before its next iteration.
Timing may be inexact due to OS scheduling. Be sure to note the current time with GetTicksNS() or GetPerformanceCounter() in case your callback needs to adjust for variances.
- Parameters
-
| interval | the timer delay, in std::chrono::nanoseconds, passed to callback. |
| callback | the TimerCB function to call when the specified interval elapses. |
- Returns
- a timer ID.
- Exceptions
-
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- Category:
- Listener callback
- See also
- listener-callback
-
AddTimer(std::chrono::milliseconds, MSTimerCallback, void*)
-
AddTimer(std::chrono::nanoseconds, NSTimerCallback, void*)
-
RemoveTimer()