SDL_timer.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. * \deprecated This function is deprecated as of SDL 2.0.18; use
  38. * SDL_GetTicks64() instead, where the value doesn't wrap
  39. * every ~49 days.
  40. *
  41. * \returns an unsigned 32-bit value representing the number of milliseconds
  42. * since the SDL library initialized.
  43. *
  44. * \since This function is available since SDL 2.0.0.
  45. *
  46. * \sa SDL_TICKS_PASSED
  47. */
  48. extern SDL_DEPRECATED DECLSPEC Uint32 SDLCALL SDL_GetTicks(void);
  49. /**
  50. * Get the number of milliseconds since SDL library initialization.
  51. *
  52. * Note that you should not use the SDL_TICKS_PASSED macro with values
  53. * returned by this function, as that macro does clever math to compensate
  54. * for the 32-bit overflow every ~49 days. 64-bit values can just be safely
  55. * compared directly.
  56. *
  57. * For example, if you want to wait 100 ms, you could do this:
  58. *
  59. * ```c
  60. * const Uint64 timeout = SDL_GetTicks64() + 100;
  61. * while (SDL_GetTicks64() < timeout) {
  62. * // ... do work until timeout has elapsed
  63. * }
  64. * ```
  65. *
  66. * \returns an unsigned 64-bit value representing the number of milliseconds
  67. * since the SDL library initialized.
  68. */
  69. extern DECLSPEC Uint64 SDLCALL SDL_GetTicks64(void);
  70. /**
  71. * Compare 32-bit SDL ticks values, and return true if `A` has passed `B`.
  72. *
  73. * This should be used with results from SDL_GetTicks(), as this macro
  74. * attempts to deal with the 32-bit counter wrapping back to zero every ~49
  75. * days, but should _not_ be used with SDL_GetTicks64(), which does not have
  76. * that problem.
  77. *
  78. * For example, if you want to wait 100 ms, you could do this:
  79. *
  80. * ```c
  81. * const Uint32 timeout = SDL_GetTicks() + 100;
  82. * while (!SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
  83. * // ... do work until timeout has elapsed
  84. * }
  85. * ```
  86. *
  87. * Note that this does not handle tick differences greater
  88. * than 2^31 so take care when using the above kind of code
  89. * with large timeout delays (tens of days).
  90. */
  91. #define SDL_TICKS_PASSED(A, B) ((Sint32)((B) - (A)) <= 0)
  92. /**
  93. * Get the current value of the high resolution counter.
  94. *
  95. * This function is typically used for profiling.
  96. *
  97. * The counter values are only meaningful relative to each other. Differences
  98. * between values can be converted to times by using
  99. * SDL_GetPerformanceFrequency().
  100. *
  101. * \returns the current counter value.
  102. *
  103. * \since This function is available since SDL 2.0.0.
  104. *
  105. * \sa SDL_GetPerformanceFrequency
  106. */
  107. extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
  108. /**
  109. * Get the count per second of the high resolution counter.
  110. *
  111. * \returns a platform-specific count per second.
  112. *
  113. * \since This function is available since SDL 2.0.0.
  114. *
  115. * \sa SDL_GetPerformanceCounter
  116. */
  117. extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void);
  118. /**
  119. * Wait a specified number of milliseconds before returning.
  120. *
  121. * This function waits a specified number of milliseconds before returning. It
  122. * waits at least the specified time, but possibly longer due to OS
  123. * scheduling.
  124. *
  125. * \param ms the number of milliseconds to delay
  126. *
  127. * \since This function is available since SDL 2.0.0.
  128. */
  129. extern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
  130. /**
  131. * Function prototype for the timer callback function.
  132. *
  133. * The callback function is passed the current timer interval and returns
  134. * the next timer interval. If the returned value is the same as the one
  135. * passed in, the periodic alarm continues, otherwise a new alarm is
  136. * scheduled. If the callback returns 0, the periodic alarm is cancelled.
  137. */
  138. typedef Uint32 (SDLCALL * SDL_TimerCallback) (Uint32 interval, void *param);
  139. /**
  140. * Definition of the timer ID type.
  141. */
  142. typedef int SDL_TimerID;
  143. /**
  144. * Call a callback function at a future time.
  145. *
  146. * If you use this function, you must pass `SDL_INIT_TIMER` to SDL_Init().
  147. *
  148. * The callback function is passed the current timer interval and the user
  149. * supplied parameter from the SDL_AddTimer() call and should return the next
  150. * timer interval. If the value returned from the callback is 0, the timer is
  151. * canceled.
  152. *
  153. * The callback is run on a separate thread.
  154. *
  155. * Timers take into account the amount of time it took to execute the
  156. * callback. For example, if the callback took 250 ms to execute and returned
  157. * 1000 (ms), the timer would only wait another 750 ms before its next
  158. * iteration.
  159. *
  160. * Timing may be inexact due to OS scheduling. Be sure to note the current
  161. * time with SDL_GetTicks() or SDL_GetPerformanceCounter() in case your
  162. * callback needs to adjust for variances.
  163. *
  164. * \param interval the timer delay, in milliseconds, passed to `callback`
  165. * \param callback the SDL_TimerCallback function to call when the specified
  166. * `interval` elapses
  167. * \param param a pointer that is passed to `callback`
  168. * \returns a timer ID or 0 if an error occurs; call SDL_GetError() for more
  169. * information.
  170. *
  171. * \since This function is available since SDL 2.0.0.
  172. *
  173. * \sa SDL_RemoveTimer
  174. */
  175. extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval,
  176. SDL_TimerCallback callback,
  177. void *param);
  178. /**
  179. * Remove a timer created with SDL_AddTimer().
  180. *
  181. * \param id the ID of the timer to remove
  182. * \returns SDL_TRUE if the timer is removed or SDL_FALSE if the timer wasn't
  183. * found.
  184. *
  185. * \since This function is available since SDL 2.0.0.
  186. *
  187. * \sa SDL_AddTimer
  188. */
  189. extern DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID id);
  190. /* Ends C function definitions when using C++ */
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #include "close_code.h"
  195. #endif /* SDL_timer_h_ */
  196. /* vi: set ts=4 sw=4 expandtab: */