SDL 3.0
SDL_timer.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#ifndef SDL_timer_h_
23#define SDL_timer_h_
24
25/**
26 * \file SDL_timer.h
27 *
28 * Header for the SDL time management routines.
29 */
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33
34#include <SDL3/SDL_begin_code.h>
35/* Set up for C function definitions, even when using C++ */
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/**
41 * SDL time constants
42 */
43#define SDL_MS_PER_SECOND 1000
44#define SDL_US_PER_SECOND 1000000
45#define SDL_NS_PER_SECOND 1000000000LL
46#define SDL_NS_PER_MS 1000000
47#define SDL_NS_PER_US 1000
48#define SDL_SECONDS_TO_NS(S) (((Uint64)(S)) * SDL_NS_PER_SECOND)
49#define SDL_NS_TO_SECONDS(NS) ((NS) / SDL_NS_PER_SECOND)
50#define SDL_MS_TO_NS(MS) (((Uint64)(MS)) * SDL_NS_PER_MS)
51#define SDL_NS_TO_MS(NS) ((NS) / SDL_NS_PER_MS)
52#define SDL_US_TO_NS(US) (((Uint64)(US)) * SDL_NS_PER_US)
53#define SDL_NS_TO_US(NS) ((NS) / SDL_NS_PER_US)
54
55/**
56 * Get the number of milliseconds since SDL library initialization.
57 *
58 * \returns an unsigned 64-bit value representing the number of milliseconds
59 * since the SDL library initialized.
60 *
61 * \since This function is available since SDL 3.0.0.
62 */
63extern DECLSPEC Uint64 SDLCALL SDL_GetTicks(void);
64
65/**
66 * Get the number of nanoseconds since SDL library initialization.
67 *
68 * \returns an unsigned 64-bit value representing the number of nanoseconds
69 * since the SDL library initialized.
70 *
71 * \since This function is available since SDL 3.0.0.
72 */
73extern DECLSPEC Uint64 SDLCALL SDL_GetTicksNS(void);
74
75/**
76 * Get the current value of the high resolution counter.
77 *
78 * This function is typically used for profiling.
79 *
80 * The counter values are only meaningful relative to each other. Differences
81 * between values can be converted to times by using
82 * SDL_GetPerformanceFrequency().
83 *
84 * \returns the current counter value.
85 *
86 * \since This function is available since SDL 3.0.0.
87 *
88 * \sa SDL_GetPerformanceFrequency
89 */
90extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
91
92/**
93 * Get the count per second of the high resolution counter.
94 *
95 * \returns a platform-specific count per second.
96 *
97 * \since This function is available since SDL 3.0.0.
98 *
99 * \sa SDL_GetPerformanceCounter
100 */
101extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void);
102
103/**
104 * Wait a specified number of milliseconds before returning.
105 *
106 * This function waits a specified number of milliseconds before returning. It
107 * waits at least the specified time, but possibly longer due to OS
108 * scheduling.
109 *
110 * \param ms the number of milliseconds to delay
111 *
112 * \since This function is available since SDL 3.0.0.
113 */
114extern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
115
116/**
117 * Wait a specified number of nanoseconds before returning.
118 *
119 * This function waits a specified number of nanoseconds before returning. It
120 * waits at least the specified time, but possibly longer due to OS
121 * scheduling.
122 *
123 * \param ns the number of nanoseconds to delay
124 *
125 * \since This function is available since SDL 3.0.0.
126 */
127extern DECLSPEC void SDLCALL SDL_DelayNS(Uint64 ns);
128
129/**
130 * Function prototype for the timer callback function.
131 *
132 * The callback function is passed the current timer interval and returns
133 * the next timer interval, in milliseconds. If the returned value is the same as the one
134 * passed in, the periodic alarm continues, otherwise a new alarm is
135 * scheduled. If the callback returns 0, the periodic alarm is cancelled.
136 */
137typedef Uint32 (SDLCALL *SDL_TimerCallback)(Uint32 interval, void *param);
138
139/**
140 * Definition of the timer ID type.
141 */
143
144/**
145 * Call a callback function at a future time.
146 *
147 * If you use this function, you must pass `SDL_INIT_TIMER` to SDL_Init().
148 *
149 * The callback function is passed the current timer interval and the user
150 * supplied parameter from the SDL_AddTimer() call and should return the next
151 * timer interval. If the value returned from the callback is 0, the timer is
152 * canceled.
153 *
154 * The callback is run on a separate thread.
155 *
156 * Timers take into account the amount of time it took to execute the
157 * callback. For example, if the callback took 250 ms to execute and returned
158 * 1000 (ms), the timer would only wait another 750 ms before its next
159 * iteration.
160 *
161 * Timing may be inexact due to OS scheduling. Be sure to note the current
162 * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
163 * callback needs to adjust for variances.
164 *
165 * \param interval the timer delay, in milliseconds, passed to `callback`
166 * \param callback the SDL_TimerCallback function to call when the specified
167 * `interval` elapses
168 * \param param a pointer that is passed to `callback`
169 * \returns a timer ID or 0 if an error occurs; call SDL_GetError() for more
170 * information.
171 *
172 * \since This function is available since SDL 3.0.0.
173 *
174 * \sa SDL_RemoveTimer
175 */
176extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval,
177 SDL_TimerCallback callback,
178 void *param);
179
180/**
181 * Remove a timer created with SDL_AddTimer().
182 *
183 * \param id the ID of the timer to remove
184 * \returns SDL_TRUE if the timer is removed or SDL_FALSE if the timer wasn't
185 * found.
186 *
187 * \since This function is available since SDL 3.0.0.
188 *
189 * \sa SDL_AddTimer
190 */
191extern DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID id);
192
193
194/* Ends C function definitions when using C++ */
195#ifdef __cplusplus
196}
197#endif
198#include <SDL3/SDL_close_code.h>
199
200#endif /* SDL_timer_h_ */
int SDL_bool
Definition SDL_stdinc.h:137
uint64_t Uint64
Definition SDL_stdinc.h:187
uint32_t Uint32
Definition SDL_stdinc.h:174
Uint32 SDL_TimerID
Definition SDL_timer.h:142
Uint32(* SDL_TimerCallback)(Uint32 interval, void *param)
Definition SDL_timer.h:137
SDL_bool SDL_RemoveTimer(SDL_TimerID id)
Uint64 SDL_GetPerformanceFrequency(void)
Uint64 SDL_GetPerformanceCounter(void)
SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
void SDL_Delay(Uint32 ms)
void SDL_DelayNS(Uint64 ns)
Uint64 SDL_GetTicksNS(void)
Uint64 SDL_GetTicks(void)