SDL_timer.h 5.4 KB

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