diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/include/SDL3/SDL_timer.h | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/include/SDL3/SDL_timer.h')
| -rw-r--r-- | contrib/SDL-3.2.8/include/SDL3/SDL_timer.h | 450 |
1 files changed, 450 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/include/SDL3/SDL_timer.h b/contrib/SDL-3.2.8/include/SDL3/SDL_timer.h new file mode 100644 index 0000000..cf94881 --- /dev/null +++ b/contrib/SDL-3.2.8/include/SDL3/SDL_timer.h | |||
| @@ -0,0 +1,450 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 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 | * # CategoryTimer | ||
| 27 | * | ||
| 28 | * SDL provides time management functionality. It is useful for dealing with | ||
| 29 | * (usually) small durations of time. | ||
| 30 | * | ||
| 31 | * This is not to be confused with _calendar time_ management, which is | ||
| 32 | * provided by [CategoryTime](CategoryTime). | ||
| 33 | * | ||
| 34 | * This category covers measuring time elapsed (SDL_GetTicks(), | ||
| 35 | * SDL_GetPerformanceCounter()), putting a thread to sleep for a certain | ||
| 36 | * amount of time (SDL_Delay(), SDL_DelayNS(), SDL_DelayPrecise()), and firing | ||
| 37 | * a callback function after a certain amount of time has elasped | ||
| 38 | * (SDL_AddTimer(), etc). | ||
| 39 | * | ||
| 40 | * There are also useful macros to convert between time units, like | ||
| 41 | * SDL_SECONDS_TO_NS() and such. | ||
| 42 | */ | ||
| 43 | |||
| 44 | #include <SDL3/SDL_stdinc.h> | ||
| 45 | #include <SDL3/SDL_error.h> | ||
| 46 | |||
| 47 | #include <SDL3/SDL_begin_code.h> | ||
| 48 | /* Set up for C function definitions, even when using C++ */ | ||
| 49 | #ifdef __cplusplus | ||
| 50 | extern "C" { | ||
| 51 | #endif | ||
| 52 | |||
| 53 | /* SDL time constants */ | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Number of milliseconds in a second. | ||
| 57 | * | ||
| 58 | * This is always 1000. | ||
| 59 | * | ||
| 60 | * \since This macro is available since SDL 3.2.0. | ||
| 61 | */ | ||
| 62 | #define SDL_MS_PER_SECOND 1000 | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Number of microseconds in a second. | ||
| 66 | * | ||
| 67 | * This is always 1000000. | ||
| 68 | * | ||
| 69 | * \since This macro is available since SDL 3.2.0. | ||
| 70 | */ | ||
| 71 | #define SDL_US_PER_SECOND 1000000 | ||
| 72 | |||
| 73 | /** | ||
| 74 | * Number of nanoseconds in a second. | ||
| 75 | * | ||
| 76 | * This is always 1000000000. | ||
| 77 | * | ||
| 78 | * \since This macro is available since SDL 3.2.0. | ||
| 79 | */ | ||
| 80 | #define SDL_NS_PER_SECOND 1000000000LL | ||
| 81 | |||
| 82 | /** | ||
| 83 | * Number of nanoseconds in a millisecond. | ||
| 84 | * | ||
| 85 | * This is always 1000000. | ||
| 86 | * | ||
| 87 | * \since This macro is available since SDL 3.2.0. | ||
| 88 | */ | ||
| 89 | #define SDL_NS_PER_MS 1000000 | ||
| 90 | |||
| 91 | /** | ||
| 92 | * Number of nanoseconds in a microsecond. | ||
| 93 | * | ||
| 94 | * This is always 1000. | ||
| 95 | * | ||
| 96 | * \since This macro is available since SDL 3.2.0. | ||
| 97 | */ | ||
| 98 | #define SDL_NS_PER_US 1000 | ||
| 99 | |||
| 100 | /** | ||
| 101 | * Convert seconds to nanoseconds. | ||
| 102 | * | ||
| 103 | * This only converts whole numbers, not fractional seconds. | ||
| 104 | * | ||
| 105 | * \param S the number of seconds to convert. | ||
| 106 | * \returns S, expressed in nanoseconds. | ||
| 107 | * | ||
| 108 | * \threadsafety It is safe to call this macro from any thread. | ||
| 109 | * | ||
| 110 | * \since This macro is available since SDL 3.2.0. | ||
| 111 | */ | ||
| 112 | #define SDL_SECONDS_TO_NS(S) (((Uint64)(S)) * SDL_NS_PER_SECOND) | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Convert nanoseconds to seconds. | ||
| 116 | * | ||
| 117 | * This performs a division, so the results can be dramatically different if | ||
| 118 | * `NS` is an integer or floating point value. | ||
| 119 | * | ||
| 120 | * \param NS the number of nanoseconds to convert. | ||
| 121 | * \returns NS, expressed in seconds. | ||
| 122 | * | ||
| 123 | * \threadsafety It is safe to call this macro from any thread. | ||
| 124 | * | ||
| 125 | * \since This macro is available since SDL 3.2.0. | ||
| 126 | */ | ||
| 127 | #define SDL_NS_TO_SECONDS(NS) ((NS) / SDL_NS_PER_SECOND) | ||
| 128 | |||
| 129 | /** | ||
| 130 | * Convert milliseconds to nanoseconds. | ||
| 131 | * | ||
| 132 | * This only converts whole numbers, not fractional milliseconds. | ||
| 133 | * | ||
| 134 | * \param MS the number of milliseconds to convert. | ||
| 135 | * \returns MS, expressed in nanoseconds. | ||
| 136 | * | ||
| 137 | * \threadsafety It is safe to call this macro from any thread. | ||
| 138 | * | ||
| 139 | * \since This macro is available since SDL 3.2.0. | ||
| 140 | */ | ||
| 141 | #define SDL_MS_TO_NS(MS) (((Uint64)(MS)) * SDL_NS_PER_MS) | ||
| 142 | |||
| 143 | /** | ||
| 144 | * Convert nanoseconds to milliseconds. | ||
| 145 | * | ||
| 146 | * This performs a division, so the results can be dramatically different if | ||
| 147 | * `NS` is an integer or floating point value. | ||
| 148 | * | ||
| 149 | * \param NS the number of nanoseconds to convert. | ||
| 150 | * \returns NS, expressed in milliseconds. | ||
| 151 | * | ||
| 152 | * \threadsafety It is safe to call this macro from any thread. | ||
| 153 | * | ||
| 154 | * \since This macro is available since SDL 3.2.0. | ||
| 155 | */ | ||
| 156 | #define SDL_NS_TO_MS(NS) ((NS) / SDL_NS_PER_MS) | ||
| 157 | |||
| 158 | /** | ||
| 159 | * Convert microseconds to nanoseconds. | ||
| 160 | * | ||
| 161 | * This only converts whole numbers, not fractional microseconds. | ||
| 162 | * | ||
| 163 | * \param US the number of microseconds to convert. | ||
| 164 | * \returns US, expressed in nanoseconds. | ||
| 165 | * | ||
| 166 | * \threadsafety It is safe to call this macro from any thread. | ||
| 167 | * | ||
| 168 | * \since This macro is available since SDL 3.2.0. | ||
| 169 | */ | ||
| 170 | #define SDL_US_TO_NS(US) (((Uint64)(US)) * SDL_NS_PER_US) | ||
| 171 | |||
| 172 | /** | ||
| 173 | * Convert nanoseconds to microseconds. | ||
| 174 | * | ||
| 175 | * This performs a division, so the results can be dramatically different if | ||
| 176 | * `NS` is an integer or floating point value. | ||
| 177 | * | ||
| 178 | * \param NS the number of nanoseconds to convert. | ||
| 179 | * \returns NS, expressed in microseconds. | ||
| 180 | * | ||
| 181 | * \threadsafety It is safe to call this macro from any thread. | ||
| 182 | * | ||
| 183 | * \since This macro is available since SDL 3.2.0. | ||
| 184 | */ | ||
| 185 | #define SDL_NS_TO_US(NS) ((NS) / SDL_NS_PER_US) | ||
| 186 | |||
| 187 | /** | ||
| 188 | * Get the number of milliseconds since SDL library initialization. | ||
| 189 | * | ||
| 190 | * \returns an unsigned 64-bit value representing the number of milliseconds | ||
| 191 | * since the SDL library initialized. | ||
| 192 | * | ||
| 193 | * \threadsafety It is safe to call this function from any thread. | ||
| 194 | * | ||
| 195 | * \since This function is available since SDL 3.2.0. | ||
| 196 | */ | ||
| 197 | extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetTicks(void); | ||
| 198 | |||
| 199 | /** | ||
| 200 | * Get the number of nanoseconds since SDL library initialization. | ||
| 201 | * | ||
| 202 | * \returns an unsigned 64-bit value representing the number of nanoseconds | ||
| 203 | * since the SDL library initialized. | ||
| 204 | * | ||
| 205 | * \threadsafety It is safe to call this function from any thread. | ||
| 206 | * | ||
| 207 | * \since This function is available since SDL 3.2.0. | ||
| 208 | */ | ||
| 209 | extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetTicksNS(void); | ||
| 210 | |||
| 211 | /** | ||
| 212 | * Get the current value of the high resolution counter. | ||
| 213 | * | ||
| 214 | * This function is typically used for profiling. | ||
| 215 | * | ||
| 216 | * The counter values are only meaningful relative to each other. Differences | ||
| 217 | * between values can be converted to times by using | ||
| 218 | * SDL_GetPerformanceFrequency(). | ||
| 219 | * | ||
| 220 | * \returns the current counter value. | ||
| 221 | * | ||
| 222 | * \threadsafety It is safe to call this function from any thread. | ||
| 223 | * | ||
| 224 | * \since This function is available since SDL 3.2.0. | ||
| 225 | * | ||
| 226 | * \sa SDL_GetPerformanceFrequency | ||
| 227 | */ | ||
| 228 | extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void); | ||
| 229 | |||
| 230 | /** | ||
| 231 | * Get the count per second of the high resolution counter. | ||
| 232 | * | ||
| 233 | * \returns a platform-specific count per second. | ||
| 234 | * | ||
| 235 | * \threadsafety It is safe to call this function from any thread. | ||
| 236 | * | ||
| 237 | * \since This function is available since SDL 3.2.0. | ||
| 238 | * | ||
| 239 | * \sa SDL_GetPerformanceCounter | ||
| 240 | */ | ||
| 241 | extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void); | ||
| 242 | |||
| 243 | /** | ||
| 244 | * Wait a specified number of milliseconds before returning. | ||
| 245 | * | ||
| 246 | * This function waits a specified number of milliseconds before returning. It | ||
| 247 | * waits at least the specified time, but possibly longer due to OS | ||
| 248 | * scheduling. | ||
| 249 | * | ||
| 250 | * \param ms the number of milliseconds to delay. | ||
| 251 | * | ||
| 252 | * \threadsafety It is safe to call this function from any thread. | ||
| 253 | * | ||
| 254 | * \since This function is available since SDL 3.2.0. | ||
| 255 | * | ||
| 256 | * \sa SDL_DelayNS | ||
| 257 | * \sa SDL_DelayPrecise | ||
| 258 | */ | ||
| 259 | extern SDL_DECLSPEC void SDLCALL SDL_Delay(Uint32 ms); | ||
| 260 | |||
| 261 | /** | ||
| 262 | * Wait a specified number of nanoseconds before returning. | ||
| 263 | * | ||
| 264 | * This function waits a specified number of nanoseconds before returning. It | ||
| 265 | * waits at least the specified time, but possibly longer due to OS | ||
| 266 | * scheduling. | ||
| 267 | * | ||
| 268 | * \param ns the number of nanoseconds to delay. | ||
| 269 | * | ||
| 270 | * \threadsafety It is safe to call this function from any thread. | ||
| 271 | * | ||
| 272 | * \since This function is available since SDL 3.2.0. | ||
| 273 | * | ||
| 274 | * \sa SDL_Delay | ||
| 275 | * \sa SDL_DelayPrecise | ||
| 276 | */ | ||
| 277 | extern SDL_DECLSPEC void SDLCALL SDL_DelayNS(Uint64 ns); | ||
| 278 | |||
| 279 | /** | ||
| 280 | * Wait a specified number of nanoseconds before returning. | ||
| 281 | * | ||
| 282 | * This function waits a specified number of nanoseconds before returning. It | ||
| 283 | * will attempt to wait as close to the requested time as possible, busy | ||
| 284 | * waiting if necessary, but could return later due to OS scheduling. | ||
| 285 | * | ||
| 286 | * \param ns the number of nanoseconds to delay. | ||
| 287 | * | ||
| 288 | * \threadsafety It is safe to call this function from any thread. | ||
| 289 | * | ||
| 290 | * \since This function is available since SDL 3.2.0. | ||
| 291 | * | ||
| 292 | * \sa SDL_Delay | ||
| 293 | * \sa SDL_DelayNS | ||
| 294 | */ | ||
| 295 | extern SDL_DECLSPEC void SDLCALL SDL_DelayPrecise(Uint64 ns); | ||
| 296 | |||
| 297 | /** | ||
| 298 | * Definition of the timer ID type. | ||
| 299 | * | ||
| 300 | * \since This datatype is available since SDL 3.2.0. | ||
| 301 | */ | ||
| 302 | typedef Uint32 SDL_TimerID; | ||
| 303 | |||
| 304 | /** | ||
| 305 | * Function prototype for the millisecond timer callback function. | ||
| 306 | * | ||
| 307 | * The callback function is passed the current timer interval and returns the | ||
| 308 | * next timer interval, in milliseconds. If the returned value is the same as | ||
| 309 | * the one passed in, the periodic alarm continues, otherwise a new alarm is | ||
| 310 | * scheduled. If the callback returns 0, the periodic alarm is canceled and | ||
| 311 | * will be removed. | ||
| 312 | * | ||
| 313 | * \param userdata an arbitrary pointer provided by the app through | ||
| 314 | * SDL_AddTimer, for its own use. | ||
| 315 | * \param timerID the current timer being processed. | ||
| 316 | * \param interval the current callback time interval. | ||
| 317 | * \returns the new callback time interval, or 0 to disable further runs of | ||
| 318 | * the callback. | ||
| 319 | * | ||
| 320 | * \threadsafety SDL may call this callback at any time from a background | ||
| 321 | * thread; the application is responsible for locking resources | ||
| 322 | * the callback touches that need to be protected. | ||
| 323 | * | ||
| 324 | * \since This datatype is available since SDL 3.2.0. | ||
| 325 | * | ||
| 326 | * \sa SDL_AddTimer | ||
| 327 | */ | ||
| 328 | typedef Uint32 (SDLCALL *SDL_TimerCallback)(void *userdata, SDL_TimerID timerID, Uint32 interval); | ||
| 329 | |||
| 330 | /** | ||
| 331 | * Call a callback function at a future time. | ||
| 332 | * | ||
| 333 | * The callback function is passed the current timer interval and the user | ||
| 334 | * supplied parameter from the SDL_AddTimer() call and should return the next | ||
| 335 | * timer interval. If the value returned from the callback is 0, the timer is | ||
| 336 | * canceled and will be removed. | ||
| 337 | * | ||
| 338 | * The callback is run on a separate thread, and for short timeouts can | ||
| 339 | * potentially be called before this function returns. | ||
| 340 | * | ||
| 341 | * Timers take into account the amount of time it took to execute the | ||
| 342 | * callback. For example, if the callback took 250 ms to execute and returned | ||
| 343 | * 1000 (ms), the timer would only wait another 750 ms before its next | ||
| 344 | * iteration. | ||
| 345 | * | ||
| 346 | * Timing may be inexact due to OS scheduling. Be sure to note the current | ||
| 347 | * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your | ||
| 348 | * callback needs to adjust for variances. | ||
| 349 | * | ||
| 350 | * \param interval the timer delay, in milliseconds, passed to `callback`. | ||
| 351 | * \param callback the SDL_TimerCallback function to call when the specified | ||
| 352 | * `interval` elapses. | ||
| 353 | * \param userdata a pointer that is passed to `callback`. | ||
| 354 | * \returns a timer ID or 0 on failure; call SDL_GetError() for more | ||
| 355 | * information. | ||
| 356 | * | ||
| 357 | * \threadsafety It is safe to call this function from any thread. | ||
| 358 | * | ||
| 359 | * \since This function is available since SDL 3.2.0. | ||
| 360 | * | ||
| 361 | * \sa SDL_AddTimerNS | ||
| 362 | * \sa SDL_RemoveTimer | ||
| 363 | */ | ||
| 364 | extern SDL_DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *userdata); | ||
| 365 | |||
| 366 | /** | ||
| 367 | * Function prototype for the nanosecond timer callback function. | ||
| 368 | * | ||
| 369 | * The callback function is passed the current timer interval and returns the | ||
| 370 | * next timer interval, in nanoseconds. If the returned value is the same as | ||
| 371 | * the one passed in, the periodic alarm continues, otherwise a new alarm is | ||
| 372 | * scheduled. If the callback returns 0, the periodic alarm is canceled and | ||
| 373 | * will be removed. | ||
| 374 | * | ||
| 375 | * \param userdata an arbitrary pointer provided by the app through | ||
| 376 | * SDL_AddTimer, for its own use. | ||
| 377 | * \param timerID the current timer being processed. | ||
| 378 | * \param interval the current callback time interval. | ||
| 379 | * \returns the new callback time interval, or 0 to disable further runs of | ||
| 380 | * the callback. | ||
| 381 | * | ||
| 382 | * \threadsafety SDL may call this callback at any time from a background | ||
| 383 | * thread; the application is responsible for locking resources | ||
| 384 | * the callback touches that need to be protected. | ||
| 385 | * | ||
| 386 | * \since This datatype is available since SDL 3.2.0. | ||
| 387 | * | ||
| 388 | * \sa SDL_AddTimerNS | ||
| 389 | */ | ||
| 390 | typedef Uint64 (SDLCALL *SDL_NSTimerCallback)(void *userdata, SDL_TimerID timerID, Uint64 interval); | ||
| 391 | |||
| 392 | /** | ||
| 393 | * Call a callback function at a future time. | ||
| 394 | * | ||
| 395 | * The callback function is passed the current timer interval and the user | ||
| 396 | * supplied parameter from the SDL_AddTimerNS() call and should return the | ||
| 397 | * next timer interval. If the value returned from the callback is 0, the | ||
| 398 | * timer is canceled and will be removed. | ||
| 399 | * | ||
| 400 | * The callback is run on a separate thread, and for short timeouts can | ||
| 401 | * potentially be called before this function returns. | ||
| 402 | * | ||
| 403 | * Timers take into account the amount of time it took to execute the | ||
| 404 | * callback. For example, if the callback took 250 ns to execute and returned | ||
| 405 | * 1000 (ns), the timer would only wait another 750 ns before its next | ||
| 406 | * iteration. | ||
| 407 | * | ||
| 408 | * Timing may be inexact due to OS scheduling. Be sure to note the current | ||
| 409 | * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your | ||
| 410 | * callback needs to adjust for variances. | ||
| 411 | * | ||
| 412 | * \param interval the timer delay, in nanoseconds, passed to `callback`. | ||
| 413 | * \param callback the SDL_TimerCallback function to call when the specified | ||
| 414 | * `interval` elapses. | ||
| 415 | * \param userdata a pointer that is passed to `callback`. | ||
| 416 | * \returns a timer ID or 0 on failure; call SDL_GetError() for more | ||
| 417 | * information. | ||
| 418 | * | ||
| 419 | * \threadsafety It is safe to call this function from any thread. | ||
| 420 | * | ||
| 421 | * \since This function is available since SDL 3.2.0. | ||
| 422 | * | ||
| 423 | * \sa SDL_AddTimer | ||
| 424 | * \sa SDL_RemoveTimer | ||
| 425 | */ | ||
| 426 | extern SDL_DECLSPEC SDL_TimerID SDLCALL SDL_AddTimerNS(Uint64 interval, SDL_NSTimerCallback callback, void *userdata); | ||
| 427 | |||
| 428 | /** | ||
| 429 | * Remove a timer created with SDL_AddTimer(). | ||
| 430 | * | ||
| 431 | * \param id the ID of the timer to remove. | ||
| 432 | * \returns true on success or false on failure; call SDL_GetError() for more | ||
| 433 | * information. | ||
| 434 | * | ||
| 435 | * \threadsafety It is safe to call this function from any thread. | ||
| 436 | * | ||
| 437 | * \since This function is available since SDL 3.2.0. | ||
| 438 | * | ||
| 439 | * \sa SDL_AddTimer | ||
| 440 | */ | ||
| 441 | extern SDL_DECLSPEC bool SDLCALL SDL_RemoveTimer(SDL_TimerID id); | ||
| 442 | |||
| 443 | |||
| 444 | /* Ends C function definitions when using C++ */ | ||
| 445 | #ifdef __cplusplus | ||
| 446 | } | ||
| 447 | #endif | ||
| 448 | #include <SDL3/SDL_close_code.h> | ||
| 449 | |||
| 450 | #endif /* SDL_timer_h_ */ | ||
