SDL standard library
SDL_timer.h@0:dda4f4550403, 2016-09-07 (annotated)
- Committer:
- miruga27
- Date:
- Wed Sep 07 18:46:53 2016 +0000
- Revision:
- 0:dda4f4550403
7/09/2016;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
miruga27 | 0:dda4f4550403 | 1 | /* |
miruga27 | 0:dda4f4550403 | 2 | Simple DirectMedia Layer |
miruga27 | 0:dda4f4550403 | 3 | Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org> |
miruga27 | 0:dda4f4550403 | 4 | |
miruga27 | 0:dda4f4550403 | 5 | This software is provided 'as-is', without any express or implied |
miruga27 | 0:dda4f4550403 | 6 | warranty. In no event will the authors be held liable for any damages |
miruga27 | 0:dda4f4550403 | 7 | arising from the use of this software. |
miruga27 | 0:dda4f4550403 | 8 | |
miruga27 | 0:dda4f4550403 | 9 | Permission is granted to anyone to use this software for any purpose, |
miruga27 | 0:dda4f4550403 | 10 | including commercial applications, and to alter it and redistribute it |
miruga27 | 0:dda4f4550403 | 11 | freely, subject to the following restrictions: |
miruga27 | 0:dda4f4550403 | 12 | |
miruga27 | 0:dda4f4550403 | 13 | 1. The origin of this software must not be misrepresented; you must not |
miruga27 | 0:dda4f4550403 | 14 | claim that you wrote the original software. If you use this software |
miruga27 | 0:dda4f4550403 | 15 | in a product, an acknowledgment in the product documentation would be |
miruga27 | 0:dda4f4550403 | 16 | appreciated but is not required. |
miruga27 | 0:dda4f4550403 | 17 | 2. Altered source versions must be plainly marked as such, and must not be |
miruga27 | 0:dda4f4550403 | 18 | misrepresented as being the original software. |
miruga27 | 0:dda4f4550403 | 19 | 3. This notice may not be removed or altered from any source distribution. |
miruga27 | 0:dda4f4550403 | 20 | */ |
miruga27 | 0:dda4f4550403 | 21 | |
miruga27 | 0:dda4f4550403 | 22 | #ifndef _SDL_timer_h |
miruga27 | 0:dda4f4550403 | 23 | #define _SDL_timer_h |
miruga27 | 0:dda4f4550403 | 24 | |
miruga27 | 0:dda4f4550403 | 25 | /** |
miruga27 | 0:dda4f4550403 | 26 | * \file SDL_timer.h |
miruga27 | 0:dda4f4550403 | 27 | * |
miruga27 | 0:dda4f4550403 | 28 | * Header for the SDL time management routines. |
miruga27 | 0:dda4f4550403 | 29 | */ |
miruga27 | 0:dda4f4550403 | 30 | |
miruga27 | 0:dda4f4550403 | 31 | #include "SDL_stdinc.h" |
miruga27 | 0:dda4f4550403 | 32 | #include "SDL_error.h" |
miruga27 | 0:dda4f4550403 | 33 | |
miruga27 | 0:dda4f4550403 | 34 | #include "begin_code.h" |
miruga27 | 0:dda4f4550403 | 35 | /* Set up for C function definitions, even when using C++ */ |
miruga27 | 0:dda4f4550403 | 36 | #ifdef __cplusplus |
miruga27 | 0:dda4f4550403 | 37 | extern "C" { |
miruga27 | 0:dda4f4550403 | 38 | #endif |
miruga27 | 0:dda4f4550403 | 39 | |
miruga27 | 0:dda4f4550403 | 40 | /** |
miruga27 | 0:dda4f4550403 | 41 | * \brief Get the number of milliseconds since the SDL library initialization. |
miruga27 | 0:dda4f4550403 | 42 | * |
miruga27 | 0:dda4f4550403 | 43 | * \note This value wraps if the program runs for more than ~49 days. |
miruga27 | 0:dda4f4550403 | 44 | */ |
miruga27 | 0:dda4f4550403 | 45 | extern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void); |
miruga27 | 0:dda4f4550403 | 46 | |
miruga27 | 0:dda4f4550403 | 47 | /** |
miruga27 | 0:dda4f4550403 | 48 | * \brief Compare SDL ticks values, and return true if A has passed B |
miruga27 | 0:dda4f4550403 | 49 | * |
miruga27 | 0:dda4f4550403 | 50 | * e.g. if you want to wait 100 ms, you could do this: |
miruga27 | 0:dda4f4550403 | 51 | * Uint32 timeout = SDL_GetTicks() + 100; |
miruga27 | 0:dda4f4550403 | 52 | * while (!SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) { |
miruga27 | 0:dda4f4550403 | 53 | * ... do work until timeout has elapsed |
miruga27 | 0:dda4f4550403 | 54 | * } |
miruga27 | 0:dda4f4550403 | 55 | */ |
miruga27 | 0:dda4f4550403 | 56 | #define SDL_TICKS_PASSED(A, B) ((Sint32)((B) - (A)) <= 0) |
miruga27 | 0:dda4f4550403 | 57 | |
miruga27 | 0:dda4f4550403 | 58 | /** |
miruga27 | 0:dda4f4550403 | 59 | * \brief Get the current value of the high resolution counter |
miruga27 | 0:dda4f4550403 | 60 | */ |
miruga27 | 0:dda4f4550403 | 61 | extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void); |
miruga27 | 0:dda4f4550403 | 62 | |
miruga27 | 0:dda4f4550403 | 63 | /** |
miruga27 | 0:dda4f4550403 | 64 | * \brief Get the count per second of the high resolution counter |
miruga27 | 0:dda4f4550403 | 65 | */ |
miruga27 | 0:dda4f4550403 | 66 | extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void); |
miruga27 | 0:dda4f4550403 | 67 | |
miruga27 | 0:dda4f4550403 | 68 | /** |
miruga27 | 0:dda4f4550403 | 69 | * \brief Wait a specified number of milliseconds before returning. |
miruga27 | 0:dda4f4550403 | 70 | */ |
miruga27 | 0:dda4f4550403 | 71 | extern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms); |
miruga27 | 0:dda4f4550403 | 72 | |
miruga27 | 0:dda4f4550403 | 73 | /** |
miruga27 | 0:dda4f4550403 | 74 | * Function prototype for the timer callback function. |
miruga27 | 0:dda4f4550403 | 75 | * |
miruga27 | 0:dda4f4550403 | 76 | * The callback function is passed the current timer interval and returns |
miruga27 | 0:dda4f4550403 | 77 | * the next timer interval. If the returned value is the same as the one |
miruga27 | 0:dda4f4550403 | 78 | * passed in, the periodic alarm continues, otherwise a new alarm is |
miruga27 | 0:dda4f4550403 | 79 | * scheduled. If the callback returns 0, the periodic alarm is cancelled. |
miruga27 | 0:dda4f4550403 | 80 | */ |
miruga27 | 0:dda4f4550403 | 81 | typedef Uint32 (SDLCALL * SDL_TimerCallback) (Uint32 interval, void *param); |
miruga27 | 0:dda4f4550403 | 82 | |
miruga27 | 0:dda4f4550403 | 83 | /** |
miruga27 | 0:dda4f4550403 | 84 | * Definition of the timer ID type. |
miruga27 | 0:dda4f4550403 | 85 | */ |
miruga27 | 0:dda4f4550403 | 86 | typedef int SDL_TimerID; |
miruga27 | 0:dda4f4550403 | 87 | |
miruga27 | 0:dda4f4550403 | 88 | /** |
miruga27 | 0:dda4f4550403 | 89 | * \brief Add a new timer to the pool of timers already running. |
miruga27 | 0:dda4f4550403 | 90 | * |
miruga27 | 0:dda4f4550403 | 91 | * \return A timer ID, or NULL when an error occurs. |
miruga27 | 0:dda4f4550403 | 92 | */ |
miruga27 | 0:dda4f4550403 | 93 | extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval, |
miruga27 | 0:dda4f4550403 | 94 | SDL_TimerCallback callback, |
miruga27 | 0:dda4f4550403 | 95 | void *param); |
miruga27 | 0:dda4f4550403 | 96 | |
miruga27 | 0:dda4f4550403 | 97 | /** |
miruga27 | 0:dda4f4550403 | 98 | * \brief Remove a timer knowing its ID. |
miruga27 | 0:dda4f4550403 | 99 | * |
miruga27 | 0:dda4f4550403 | 100 | * \return A boolean value indicating success or failure. |
miruga27 | 0:dda4f4550403 | 101 | * |
miruga27 | 0:dda4f4550403 | 102 | * \warning It is not safe to remove a timer multiple times. |
miruga27 | 0:dda4f4550403 | 103 | */ |
miruga27 | 0:dda4f4550403 | 104 | extern DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID id); |
miruga27 | 0:dda4f4550403 | 105 | |
miruga27 | 0:dda4f4550403 | 106 | |
miruga27 | 0:dda4f4550403 | 107 | /* Ends C function definitions when using C++ */ |
miruga27 | 0:dda4f4550403 | 108 | #ifdef __cplusplus |
miruga27 | 0:dda4f4550403 | 109 | } |
miruga27 | 0:dda4f4550403 | 110 | #endif |
miruga27 | 0:dda4f4550403 | 111 | #include "close_code.h" |
miruga27 | 0:dda4f4550403 | 112 | |
miruga27 | 0:dda4f4550403 | 113 | #endif /* _SDL_timer_h */ |
miruga27 | 0:dda4f4550403 | 114 | |
miruga27 | 0:dda4f4550403 | 115 | /* vi: set ts=4 sw=4 expandtab: */ |