include/SDL_timer.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1358 c71e05b4dc2e
child 2859 99210400e8b9
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@251
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifndef _SDL_timer_h
slouken@0
    24
#define _SDL_timer_h
slouken@0
    25
slouken@1895
    26
/**
slouken@1895
    27
 * \file SDL_timer.h
slouken@1895
    28
 *
slouken@1895
    29
 * Header for the SDL time management routines
slouken@1895
    30
 */
slouken@0
    31
slouken@1356
    32
#include "SDL_stdinc.h"
slouken@1358
    33
#include "SDL_error.h"
slouken@0
    34
slouken@0
    35
#include "begin_code.h"
slouken@0
    36
/* Set up for C function definitions, even when using C++ */
slouken@0
    37
#ifdef __cplusplus
slouken@1895
    38
/* *INDENT-OFF* */
slouken@0
    39
extern "C" {
slouken@1895
    40
/* *INDENT-ON* */
slouken@0
    41
#endif
slouken@0
    42
slouken@0
    43
/* This is the OS scheduler timeslice, in milliseconds */
slouken@0
    44
#define SDL_TIMESLICE		10
slouken@0
    45
slouken@0
    46
/* This is the maximum resolution of the SDL timer on all platforms */
slouken@1895
    47
#define TIMER_RESOLUTION	10      /* Experimentally determined */
slouken@0
    48
slouken@0
    49
/* Get the number of milliseconds since the SDL library initialization.
slouken@0
    50
 * Note that this value wraps if the program runs for more than ~49 days.
slouken@1895
    51
 */
slouken@337
    52
extern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void);
slouken@0
    53
slouken@0
    54
/* Wait a specified number of milliseconds before returning */
slouken@337
    55
extern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
slouken@0
    56
slouken@0
    57
/* Function prototype for the timer callback function */
slouken@1895
    58
typedef Uint32(SDLCALL * SDL_TimerCallback) (Uint32 interval);
slouken@0
    59
slouken@0
    60
/* Set a callback to run after the specified number of milliseconds has
slouken@0
    61
 * elapsed. The callback function is passed the current timer interval
slouken@0
    62
 * and returns the next timer interval.  If the returned value is the 
slouken@0
    63
 * same as the one passed in, the periodic alarm continues, otherwise a
slouken@0
    64
 * new alarm is scheduled.  If the callback returns 0, the periodic alarm
slouken@0
    65
 * is cancelled.
slouken@0
    66
 *
slouken@0
    67
 * To cancel a currently running timer, call SDL_SetTimer(0, NULL);
slouken@0
    68
 *
slouken@0
    69
 * The timer callback function may run in a different thread than your
slouken@0
    70
 * main code, and so shouldn't call any functions from within itself.
slouken@0
    71
 *
slouken@0
    72
 * The maximum resolution of this timer is 10 ms, which means that if
slouken@0
    73
 * you request a 16 ms timer, your callback will run approximately 20 ms
slouken@0
    74
 * later on an unloaded system.  If you wanted to set a flag signaling
slouken@0
    75
 * a frame update at 30 frames per second (every 33 ms), you might set a 
slouken@0
    76
 * timer for 30 ms:
slouken@0
    77
 *   SDL_SetTimer((33/10)*10, flag_update);
slouken@0
    78
 *
slouken@0
    79
 * If you use this function, you need to pass SDL_INIT_TIMER to SDL_Init().
slouken@0
    80
 *
slouken@0
    81
 * Under UNIX, you should not use raise or use SIGALRM and this function
slouken@0
    82
 * in the same program, as it is implemented using setitimer().  You also
slouken@0
    83
 * should not use this function in multi-threaded applications as signals
slouken@0
    84
 * to multi-threaded apps have undefined behavior in some implementations.
slouken@1028
    85
 *
slouken@1028
    86
 * This function returns 0 if successful, or -1 if there was an error.
slouken@0
    87
 */
slouken@1895
    88
extern DECLSPEC int SDLCALL SDL_SetTimer(Uint32 interval,
slouken@1895
    89
                                         SDL_TimerCallback callback);
slouken@0
    90
slouken@0
    91
/* New timer API, supports multiple timers
slouken@0
    92
 * Written by Stephane Peter <megastep@lokigames.com>
slouken@0
    93
 */
slouken@0
    94
slouken@0
    95
/* Function prototype for the new timer callback function.
slouken@0
    96
 * The callback function is passed the current timer interval and returns
slouken@0
    97
 * the next timer interval.  If the returned value is the same as the one
slouken@0
    98
 * passed in, the periodic alarm continues, otherwise a new alarm is
slouken@0
    99
 * scheduled.  If the callback returns 0, the periodic alarm is cancelled.
slouken@0
   100
 */
slouken@1895
   101
typedef Uint32(SDLCALL * SDL_NewTimerCallback) (Uint32 interval, void *param);
slouken@0
   102
slouken@0
   103
/* Definition of the timer ID type */
slouken@0
   104
typedef struct _SDL_TimerID *SDL_TimerID;
slouken@0
   105
slouken@0
   106
/* Add a new timer to the pool of timers already running.
slouken@0
   107
   Returns a timer ID, or NULL when an error occurs.
slouken@0
   108
 */
slouken@1895
   109
extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval,
slouken@1895
   110
                                                 SDL_NewTimerCallback
slouken@1895
   111
                                                 callback, void *param);
slouken@0
   112
slouken@0
   113
/* Remove one of the multiple timers knowing its ID.
slouken@0
   114
 * Returns a boolean value indicating success.
slouken@0
   115
 */
slouken@337
   116
extern DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID t);
slouken@0
   117
slouken@0
   118
/* Ends C function definitions when using C++ */
slouken@0
   119
#ifdef __cplusplus
slouken@1895
   120
/* *INDENT-OFF* */
slouken@0
   121
}
slouken@1895
   122
/* *INDENT-ON* */
slouken@0
   123
#endif
slouken@0
   124
#include "close_code.h"
slouken@0
   125
slouken@0
   126
#endif /* _SDL_timer_h */
slouken@1895
   127
slouken@1895
   128
/* vi: set ts=4 sw=4 expandtab: */