SDL_timer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef SDL_timer_h_
  19. #define SDL_timer_h_
  20. /**
  21. * \file SDL_timer.h
  22. *
  23. * Header for the SDL time management routines.
  24. */
  25. #include "SDL_stdinc.h"
  26. #include "SDL_error.h"
  27. #include "begin_code.h"
  28. /* Set up for C function definitions, even when using C++ */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /**
  33. * Get the number of milliseconds since SDL library initialization.
  34. *
  35. * This value wraps if the program runs for more than ~49 days.
  36. *
  37. * This function is not recommended as of SDL 2.0.18; use SDL_GetTicks64()
  38. * instead, where the value doesn't wrap every ~49 days. There are places in
  39. * SDL where we provide a 32-bit timestamp that can not change without
  40. * breaking binary compatibility, though, so this function isn't officially
  41. * deprecated.
  42. *
  43. * \returns an unsigned 32-bit value representing the number of milliseconds
  44. * since the SDL library initialized.
  45. *
  46. * \since This function is available since SDL 2.0.0.
  47. *
  48. * \sa SDL_TICKS_PASSED
  49. */
  50. extern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void);
  51. /**
  52. * Get the number of milliseconds since SDL library initialization.
  53. *
  54. * Note that you should not use the SDL_TICKS_PASSED macro with values
  55. * returned by this function, as that macro does clever math to compensate for
  56. * the 32-bit overflow every ~49 days that SDL_GetTicks() suffers from. 64-bit
  57. * values from this function can be safely compared directly.
  58. *
  59. * For example, if you want to wait 100 ms, you could do this:
  60. *
  61. * ```c
  62. * const Uint64 timeout = SDL_GetTicks64() + 100;
  63. * while (SDL_GetTicks64() < timeout) {
  64. * // ... do work until timeout has elapsed
  65. * }
  66. * ```
  67. *
  68. * \returns an unsigned 64-bit value representing the number of milliseconds
  69. * since the SDL library initialized.
  70. *
  71. * \since This function is available since SDL 2.0.18.
  72. */
  73. extern DECLSPEC Uint64 SDLCALL SDL_GetTicks64(void);
  74. /**
  75. * Compare 32-bit SDL ticks values, and return true if `A` has passed `B`.
  76. *
  77. * This should be used with results from SDL_GetTicks(), as this macro
  78. * attempts to deal with the 32-bit counter wrapping back to zero every ~49
  79. * days, but should _not_ be used with SDL_GetTicks64(), which does not have
  80. * that problem.
  81. *
  82. * For example, with SDL_GetTicks(), if you want to wait 100 ms, you could
  83. * do this:
  84. *
  85. * ```c
  86. * const Uint32 timeout = SDL_GetTicks() + 100;
  87. * while (!SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
  88. * // ... do work until timeout has elapsed
  89. * }
  90. * ```
  91. *
  92. * Note that this does not handle tick differences greater
  93. * than 2^31 so take care when using the above kind of code
  94. * with large timeout delays (tens of days).
  95. */
  96. #define SDL_TICKS_PASSED(A, B) ((Sint32)((B) - (A)) <= 0)
  97. /**
  98. * Get the current value of the high resolution counter.
  99. *
  100. * This function is typically used for profiling.
  101. *
  102. * The counter values are only meaningful relative to each other. Differences
  103. * between values can be converted to times by using
  104. * SDL_GetPerformanceFrequency().
  105. *
  106. * \returns the current counter value.
  107. *
  108. * \since This function is available since SDL 2.0.0.
  109. *
  110. * \sa SDL_GetPerformanceFrequency
  111. */
  112. extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
  113. /**
  114. * Get the count per second of the high resolution counter.
  115. *
  116. * \returns a platform-specific count per second.
  117. *
  118. * \since This function is available since SDL 2.0.0.
  119. *
  120. * \sa SDL_GetPerformanceCounter
  121. */
  122. extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void);
  123. /**
  124. * Wait a specified number of milliseconds before returning.
  125. *
  126. * This function waits a specified number of milliseconds before returning. It
  127. * waits at least the specified time, but possibly longer due to OS
  128. * scheduling.
  129. *
  130. * \param ms the number of milliseconds to delay
  131. *
  132. * \since This function is available since SDL 2.0.0.
  133. */
  134. extern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
  135. /**
  136. * Function prototype for the timer callback function.
  137. *
  138. * The callback function is passed the current timer interval and returns
  139. * the next timer interval. If the returned value is the same as the one
  140. * passed in, the periodic alarm continues, otherwise a new alarm is
  141. * scheduled. If the callback returns 0, the periodic alarm is cancelled.
  142. */
  143. typedef Uint32 (SDLCALL * SDL_TimerCallback) (Uint32 interval, void *param);
  144. /**
  145. * Definition of the timer ID type.
  146. */
  147. typedef int SDL_TimerID;
  148. /**
  149. * Call a callback function at a future time.
  150. *
  151. * If you use this function, you must pass `SDL_INIT_TIMER` to SDL_Init().
  152. *
  153. * The callback function is passed the current timer interval and the user
  154. * supplied parameter from the SDL_AddTimer() call and should return the next
  155. * timer interval. If the value returned from the callback is 0, the timer is
  156. * canceled.
  157. *
  158. * The callback is run on a separate thread.
  159. *
  160. * Timers take into account the amount of time it took to execute the
  161. * callback. For example, if the callback took 250 ms to execute and returned
  162. * 1000 (ms), the timer would only wait another 750 ms before its next
  163. * iteration.
  164. *
  165. * Timing may be inexact due to OS scheduling. Be sure to note the current
  166. * time with SDL_GetTicks() or SDL_GetPerformanceCounter() in case your
  167. * callback needs to adjust for variances.
  168. *
  169. * \param interval the timer delay, in milliseconds, passed to `callback`
  170. * \param callback the SDL_TimerCallback function to call when the specified
  171. * `interval` elapses
  172. * \param param a pointer that is passed to `callback`
  173. * \returns a timer ID or 0 if an error occurs; call SDL_GetError() for more
  174. * information.
  175. *
  176. * \since This function is available since SDL 2.0.0.
  177. *
  178. * \sa SDL_RemoveTimer
  179. */
  180. extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval,
  181. SDL_TimerCallback callback,
  182. void *param);
  183. /**
  184. * Remove a timer created with SDL_AddTimer().
  185. *
  186. * \param id the ID of the timer to remove
  187. * \returns SDL_TRUE if the timer is removed or SDL_FALSE if the timer wasn't
  188. * found.
  189. *
  190. * \since This function is available since SDL 2.0.0.
  191. *
  192. * \sa SDL_AddTimer
  193. */
  194. extern DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID id);
  195. /* Ends C function definitions when using C++ */
  196. #ifdef __cplusplus
  197. }
  198. #endif
  199. #include "close_code.h"
  200. #endif /* SDL_timer_h_ */
  201. /* vi: set ts=4 sw=4 expandtab: */