2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #ifdef SDL_TIMER_MACOS
30 #include <Processes.h>
34 #include "SDL_timer.h"
35 #include "../SDL_timer_c.h"
37 #define MS_PER_TICK (1000/60) /* MacOS tick = 1/60 second */
39 /* Note: This is only a step above the original 1/60s implementation.
40 * For a good implementation, see FastTimes.[ch], by Matt Slot.
42 #define USE_MICROSECONDS
43 #define WideTo64bit(w) (*(UInt64 *) &(w))
47 void SDL_StartTicks(void)
49 #ifdef USE_MICROSECONDS
53 start = WideTo64bit(now);
55 /* FIXME: Should we implement a wrapping algorithm, like Win32? */
59 Uint32 SDL_GetTicks(void)
61 #ifdef USE_MICROSECONDS
65 return (Uint32)((WideTo64bit(now)-start)/1000);
67 return(LMGetTicks()*MS_PER_TICK);
71 void SDL_Delay(Uint32 ms)
73 #ifdef USE_MICROSECONDS
76 end_ms = SDL_GetTicks() + ms;
78 /* FIXME: Yield CPU? */ ;
79 } while ( SDL_GetTicks() < end_ms );
81 UInt32 unused; /* MJS */
82 Delay(ms/MS_PER_TICK, &unused);
87 /* Data to handle a single periodic alarm */
88 typedef struct _ExtendedTimerRec
91 ProcessSerialNumber taskPSN;
92 } ExtendedTimerRec, *ExtendedTimerPtr;
94 static ExtendedTimerRec gExtendedTimerRec;
97 int SDL_SYS_TimerInit(void)
99 /* We don't need a setup? */
103 void SDL_SYS_TimerQuit(void)
105 /* We don't need a cleanup? */
109 /* Our Stub routine to set up and then call the real routine. */
110 pascal void TimerCallbackProc(TMTaskPtr tmTaskPtr)
114 WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN);
116 ms = SDL_alarm_callback(SDL_alarm_interval);
118 SDL_alarm_interval = ROUND_RESOLUTION(ms);
119 PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask,
122 SDL_alarm_interval = 0;
126 int SDL_SYS_StartTimer(void)
129 * Configure the global structure that stores the timing information.
131 gExtendedTimerRec.tmTask.qLink = NULL;
132 gExtendedTimerRec.tmTask.qType = 0;
133 gExtendedTimerRec.tmTask.tmAddr = NewTimerUPP(TimerCallbackProc);
134 gExtendedTimerRec.tmTask.tmCount = 0;
135 gExtendedTimerRec.tmTask.tmWakeUp = 0;
136 gExtendedTimerRec.tmTask.tmReserved = 0;
137 GetCurrentProcess(&gExtendedTimerRec.taskPSN);
139 /* Install the task record */
140 InsXTime((QElemPtr)&gExtendedTimerRec.tmTask);
143 PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval);
147 void SDL_SYS_StopTimer(void)
149 RmvTime((QElemPtr)&gExtendedTimerRec.tmTask);
152 #endif /* SDL_TIMER_MACOS */