SDL_timer.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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. * # CategoryTimer
  22. *
  23. * SDL time management routines.
  24. */
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_begin_code.h>
  28. /* Set up for C function definitions, even when using C++ */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* SDL time constants */
  33. #define SDL_MS_PER_SECOND 1000
  34. #define SDL_US_PER_SECOND 1000000
  35. #define SDL_NS_PER_SECOND 1000000000LL
  36. #define SDL_NS_PER_MS 1000000
  37. #define SDL_NS_PER_US 1000
  38. #define SDL_SECONDS_TO_NS(S) (((Uint64)(S)) * SDL_NS_PER_SECOND)
  39. #define SDL_NS_TO_SECONDS(NS) ((NS) / SDL_NS_PER_SECOND)
  40. #define SDL_MS_TO_NS(MS) (((Uint64)(MS)) * SDL_NS_PER_MS)
  41. #define SDL_NS_TO_MS(NS) ((NS) / SDL_NS_PER_MS)
  42. #define SDL_US_TO_NS(US) (((Uint64)(US)) * SDL_NS_PER_US)
  43. #define SDL_NS_TO_US(NS) ((NS) / SDL_NS_PER_US)
  44. /**
  45. * Get the number of milliseconds since SDL library initialization.
  46. *
  47. * \returns an unsigned 64-bit value representing the number of milliseconds
  48. * since the SDL library initialized.
  49. *
  50. * \since This function is available since SDL 3.0.0.
  51. */
  52. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetTicks(void);
  53. /**
  54. * Get the number of nanoseconds since SDL library initialization.
  55. *
  56. * \returns an unsigned 64-bit value representing the number of nanoseconds
  57. * since the SDL library initialized.
  58. *
  59. * \since This function is available since SDL 3.0.0.
  60. */
  61. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetTicksNS(void);
  62. /**
  63. * Get the current value of the high resolution counter.
  64. *
  65. * This function is typically used for profiling.
  66. *
  67. * The counter values are only meaningful relative to each other. Differences
  68. * between values can be converted to times by using
  69. * SDL_GetPerformanceFrequency().
  70. *
  71. * \returns the current counter value.
  72. *
  73. * \since This function is available since SDL 3.0.0.
  74. *
  75. * \sa SDL_GetPerformanceFrequency
  76. */
  77. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
  78. /**
  79. * Get the count per second of the high resolution counter.
  80. *
  81. * \returns a platform-specific count per second.
  82. *
  83. * \since This function is available since SDL 3.0.0.
  84. *
  85. * \sa SDL_GetPerformanceCounter
  86. */
  87. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void);
  88. /**
  89. * Wait a specified number of milliseconds before returning.
  90. *
  91. * This function waits a specified number of milliseconds before returning. It
  92. * waits at least the specified time, but possibly longer due to OS
  93. * scheduling.
  94. *
  95. * \param ms the number of milliseconds to delay.
  96. *
  97. * \since This function is available since SDL 3.0.0.
  98. */
  99. extern SDL_DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
  100. /**
  101. * Wait a specified number of nanoseconds before returning.
  102. *
  103. * This function waits a specified number of nanoseconds before returning. It
  104. * will attempt to wait as close to the requested time as possible, busy
  105. * waiting if necessary, but could return later due to OS scheduling.
  106. *
  107. * \param ns the number of nanoseconds to delay.
  108. *
  109. * \since This function is available since SDL 3.0.0.
  110. */
  111. extern SDL_DECLSPEC void SDLCALL SDL_DelayNS(Uint64 ns);
  112. /**
  113. * Definition of the timer ID type.
  114. *
  115. * \since This datatype is available since SDL 3.0.0.
  116. */
  117. typedef Uint32 SDL_TimerID;
  118. /**
  119. * Function prototype for the millisecond timer callback function.
  120. *
  121. * The callback function is passed the current timer interval and returns the
  122. * next timer interval, in milliseconds. If the returned value is the same as
  123. * the one passed in, the periodic alarm continues, otherwise a new alarm is
  124. * scheduled. If the callback returns 0, the periodic alarm is canceled and
  125. * will be removed.
  126. *
  127. * \param userdata an arbitrary pointer provided by the app through
  128. * SDL_AddTimer, for its own use.
  129. * \param timerID the current timer being processed.
  130. * \param interval the current callback time interval.
  131. * \returns the new callback time interval, or 0 to disable further runs of
  132. * the callback.
  133. *
  134. * \threadsafety SDL may call this callback at any time from a background
  135. * thread; the application is responsible for locking resources
  136. * the callback touches that need to be protected.
  137. *
  138. * \since This datatype is available since SDL 3.0.0.
  139. *
  140. * \sa SDL_AddTimer
  141. */
  142. typedef Uint32 (SDLCALL *SDL_TimerCallback)(void *userdata, SDL_TimerID timerID, Uint32 interval);
  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 and will be removed.
  152. *
  153. * The callback is run on a separate thread, and for short timeouts can
  154. * potentially be called before this function returns.
  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 userdata a pointer that is passed to `callback`.
  169. * \returns a timer ID or 0 on failure; call SDL_GetError() for more
  170. * information.
  171. *
  172. * \threadsafety It is safe to call this function from any thread.
  173. *
  174. * \since This function is available since SDL 3.0.0.
  175. *
  176. * \sa SDL_AddTimerNS
  177. * \sa SDL_RemoveTimer
  178. */
  179. extern SDL_DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *userdata);
  180. /**
  181. * Function prototype for the nanosecond timer callback function.
  182. *
  183. * The callback function is passed the current timer interval and returns the
  184. * next timer interval, in nanoseconds. If the returned value is the same as
  185. * the one passed in, the periodic alarm continues, otherwise a new alarm is
  186. * scheduled. If the callback returns 0, the periodic alarm is canceled and
  187. * will be removed.
  188. *
  189. * \param userdata an arbitrary pointer provided by the app through
  190. * SDL_AddTimer, for its own use.
  191. * \param timerID the current timer being processed.
  192. * \param interval the current callback time interval.
  193. * \returns the new callback time interval, or 0 to disable further runs of
  194. * the callback.
  195. *
  196. * \threadsafety SDL may call this callback at any time from a background
  197. * thread; the application is responsible for locking resources
  198. * the callback touches that need to be protected.
  199. *
  200. * \since This datatype is available since SDL 3.0.0.
  201. *
  202. * \sa SDL_AddTimerNS
  203. */
  204. typedef Uint64 (SDLCALL *SDL_NSTimerCallback)(void *userdata, SDL_TimerID timerID, Uint64 interval);
  205. /**
  206. * Call a callback function at a future time.
  207. *
  208. * If you use this function, you must pass `SDL_INIT_TIMER` to SDL_Init().
  209. *
  210. * The callback function is passed the current timer interval and the user
  211. * supplied parameter from the SDL_AddTimerNS() call and should return the
  212. * next timer interval. If the value returned from the callback is 0, the
  213. * timer is canceled and will be removed.
  214. *
  215. * The callback is run on a separate thread, and for short timeouts can
  216. * potentially be called before this function returns.
  217. *
  218. * Timers take into account the amount of time it took to execute the
  219. * callback. For example, if the callback took 250 ns to execute and returned
  220. * 1000 (ns), the timer would only wait another 750 ns before its next
  221. * iteration.
  222. *
  223. * Timing may be inexact due to OS scheduling. Be sure to note the current
  224. * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
  225. * callback needs to adjust for variances.
  226. *
  227. * \param interval the timer delay, in nanoseconds, passed to `callback`.
  228. * \param callback the SDL_TimerCallback function to call when the specified
  229. * `interval` elapses.
  230. * \param userdata a pointer that is passed to `callback`.
  231. * \returns a timer ID or 0 on failure; call SDL_GetError() for more
  232. * information.
  233. *
  234. * \threadsafety It is safe to call this function from any thread.
  235. *
  236. * \since This function is available since SDL 3.0.0.
  237. *
  238. * \sa SDL_AddTimer
  239. * \sa SDL_RemoveTimer
  240. */
  241. extern SDL_DECLSPEC SDL_TimerID SDLCALL SDL_AddTimerNS(Uint64 interval, SDL_NSTimerCallback callback, void *userdata);
  242. /**
  243. * Remove a timer created with SDL_AddTimer().
  244. *
  245. * \param id the ID of the timer to remove.
  246. * \returns SDL_TRUE on success or SDL_FALSE on failure; call
  247. * SDL_GetError() for more information.
  248. *
  249. * \since This function is available since SDL 3.0.0.
  250. *
  251. * \sa SDL_AddTimer
  252. */
  253. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID id);
  254. /* Ends C function definitions when using C++ */
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #include <SDL3/SDL_close_code.h>
  259. #endif /* SDL_timer_h_ */