19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _SDL_timer_h
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define _SDL_timer_h
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** @file SDL_timer.h
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  Header for the SDL time management routines
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_stdinc.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_error.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "begin_code.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Set up for C function definitions, even when using C++ */
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __cplusplus
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern "C" {
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** This is the OS scheduler timeslice, in milliseconds */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SDL_TIMESLICE		10
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** This is the maximum resolution of the SDL timer on all platforms */
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define TIMER_RESOLUTION	10	/**< Experimentally determined */
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/**
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Get the number of milliseconds since the SDL library initialization.
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Note that this value wraps if the program runs for more than ~49 days.
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** Wait a specified number of milliseconds before returning */
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** Function prototype for the timer callback function */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef Uint32 (SDLCALL *SDL_TimerCallback)(Uint32 interval);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/**
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Set a callback to run after the specified number of milliseconds has
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * elapsed. The callback function is passed the current timer interval
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * and returns the next timer interval.  If the returned value is the
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * same as the one passed in, the periodic alarm continues, otherwise a
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * new alarm is scheduled.  If the callback returns 0, the periodic alarm
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * is cancelled.
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * To cancel a currently running timer, call SDL_SetTimer(0, NULL);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * The timer callback function may run in a different thread than your
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * main code, and so shouldn't call any functions from within itself.
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * The maximum resolution of this timer is 10 ms, which means that if
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * you request a 16 ms timer, your callback will run approximately 20 ms
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * later on an unloaded system.  If you wanted to set a flag signaling
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * a frame update at 30 frames per second (every 33 ms), you might set a
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * timer for 30 ms:
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *   @code SDL_SetTimer((33/10)*10, flag_update); @endcode
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * If you use this function, you need to pass SDL_INIT_TIMER to SDL_Init().
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Under UNIX, you should not use raise or use SIGALRM and this function
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * in the same program, as it is implemented using setitimer().  You also
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * should not use this function in multi-threaded applications as signals
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * to multi-threaded apps have undefined behavior in some implementations.
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function returns 0 if successful, or -1 if there was an error.
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern DECLSPEC int SDLCALL SDL_SetTimer(Uint32 interval, SDL_TimerCallback callback);
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** @name New timer API
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * New timer API, supports multiple timers
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Written by Stephane Peter <megastep@lokigames.com>
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*@{*/
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/**
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Function prototype for the new timer callback function.
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * The callback function is passed the current timer interval and returns
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * the next timer interval.  If the returned value is the same as the one
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * passed in, the periodic alarm continues, otherwise a new alarm is
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * scheduled.  If the callback returns 0, the periodic alarm is cancelled.
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef Uint32 (SDLCALL *SDL_NewTimerCallback)(Uint32 interval, void *param);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** Definition of the timer ID type */
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct _SDL_TimerID *SDL_TimerID;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** Add a new timer to the pool of timers already running.
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  Returns a timer ID, or NULL when an error occurs.
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/**
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Remove one of the multiple timers knowing its ID.
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Returns a boolean value indicating success.
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID t);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*@}*/
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Ends C function definitions when using C++ */
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __cplusplus
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "close_code.h"
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* _SDL_timer_h */
126