SDL_timer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. * \file SDL_timer.h
  22. *
  23. * \brief Header for the 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. /**
  33. * SDL time constants
  34. */
  35. #define SDL_MS_PER_SECOND 1000
  36. #define SDL_US_PER_SECOND 1000000
  37. #define SDL_NS_PER_SECOND 1000000000LL
  38. #define SDL_NS_PER_MS 1000000
  39. #define SDL_NS_PER_US 1000
  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 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 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 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 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 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. * waits at least the specified time, but possibly longer due to OS
  105. * 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 DECLSPEC void SDLCALL SDL_DelayNS(Uint64 ns);
  112. /**
  113. * Function prototype for the timer callback function.
  114. *
  115. * The callback function is passed the current timer interval and returns
  116. * the next timer interval, in milliseconds. If the returned value is the same as the one
  117. * passed in, the periodic alarm continues, otherwise a new alarm is
  118. * scheduled. If the callback returns 0, the periodic alarm is cancelled.
  119. */
  120. typedef Uint32 (SDLCALL *SDL_TimerCallback)(Uint32 interval, void *param);
  121. /**
  122. * Definition of the timer ID type.
  123. */
  124. typedef int SDL_TimerID;
  125. /**
  126. * Call a callback function at a future time.
  127. *
  128. * If you use this function, you must pass `SDL_INIT_TIMER` to SDL_Init().
  129. *
  130. * The callback function is passed the current timer interval and the user
  131. * supplied parameter from the SDL_AddTimer() call and should return the next
  132. * timer interval. If the value returned from the callback is 0, the timer is
  133. * canceled.
  134. *
  135. * The callback is run on a separate thread.
  136. *
  137. * Timers take into account the amount of time it took to execute the
  138. * callback. For example, if the callback took 250 ms to execute and returned
  139. * 1000 (ms), the timer would only wait another 750 ms before its next
  140. * iteration.
  141. *
  142. * Timing may be inexact due to OS scheduling. Be sure to note the current
  143. * time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
  144. * callback needs to adjust for variances.
  145. *
  146. * \param interval the timer delay, in milliseconds, passed to `callback`
  147. * \param callback the SDL_TimerCallback function to call when the specified
  148. * `interval` elapses
  149. * \param param a pointer that is passed to `callback`
  150. * \returns a timer ID or 0 if an error occurs; call SDL_GetError() for more
  151. * information.
  152. *
  153. * \since This function is available since SDL 3.0.0.
  154. *
  155. * \sa SDL_RemoveTimer
  156. */
  157. extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval,
  158. SDL_TimerCallback callback,
  159. void *param);
  160. /**
  161. * Remove a timer created with SDL_AddTimer().
  162. *
  163. * \param id the ID of the timer to remove
  164. * \returns SDL_TRUE if the timer is removed or SDL_FALSE if the timer wasn't
  165. * found.
  166. *
  167. * \since This function is available since SDL 3.0.0.
  168. *
  169. * \sa SDL_AddTimer
  170. */
  171. extern DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID id);
  172. /* Ends C function definitions when using C++ */
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #include <SDL3/SDL_close_code.h>
  177. #endif /* SDL_timer_h_ */