Mercurial > sdl-ios-xcode
annotate src/timer/macos/SDL_MPWtimer.c @ 1399:c35c161a1100
*** empty log message ***
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 20 Feb 2006 23:18:49 +0000 |
parents | 19418e4422cb |
children | d910939febfa |
rev | line source |
---|---|
0 | 1 /* |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
2 SDL - Simple DirectMedia Layer |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
5 This library is free software; you can redistribute it and/or |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
7 License as published by the Free Software Foundation; either |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
10 This library is distributed in the hope that it will be useful, |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
19 Sam Lantinga |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
20 slouken@libsdl.org |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
21 */ |
0 | 22 |
23 #include <Types.h> | |
24 #include <Timer.h> | |
25 #include <OSUtils.h> | |
26 #include <Gestalt.h> | |
27 #include <Processes.h> | |
28 | |
29 #include <LowMem.h> | |
30 | |
31 #include "SDL_timer.h" | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
32 #include "../SDL_timer_c.h" |
0 | 33 |
34 #define MS_PER_TICK (1000/60) /* MacOS tick = 1/60 second */ | |
35 | |
36 /* Note: This is only a step above the original 1/60s implementation. | |
37 * For a good implementation, see FastTimes.[ch], by Matt Slot. | |
38 */ | |
39 #define USE_MICROSECONDS | |
40 #define WideTo64bit(w) (*(UInt64 *) &(w)) | |
41 | |
42 UInt64 start; | |
43 | |
44 void SDL_StartTicks(void) | |
45 { | |
46 #ifdef USE_MICROSECONDS | |
47 UnsignedWide now; | |
48 | |
49 Microseconds(&now); | |
50 start = WideTo64bit(now); | |
51 #else | |
52 /* FIXME: Should we implement a wrapping algorithm, like Win32? */ | |
53 #endif | |
54 } | |
55 | |
56 Uint32 SDL_GetTicks(void) | |
57 { | |
58 #ifdef USE_MICROSECONDS | |
59 UnsignedWide now; | |
60 | |
61 Microseconds(&now); | |
62 return (Uint32)((WideTo64bit(now)-start)/1000); | |
63 #else | |
64 return(LMGetTicks()*MS_PER_TICK); | |
65 #endif | |
66 } | |
67 | |
68 void SDL_Delay(Uint32 ms) | |
69 { | |
70 #ifdef USE_MICROSECONDS | |
71 Uint32 end_ms; | |
72 | |
73 end_ms = SDL_GetTicks() + ms; | |
74 do { | |
75 /* FIXME: Yield CPU? */ ; | |
76 } while ( SDL_GetTicks() < end_ms ); | |
77 #else | |
78 UInt32 unused; /* MJS */ | |
79 Delay(ms/MS_PER_TICK, &unused); | |
80 #endif | |
81 } | |
82 | |
83 | |
84 /* Data to handle a single periodic alarm */ | |
85 typedef struct _ExtendedTimerRec | |
86 { | |
87 TMTask tmTask; | |
88 ProcessSerialNumber taskPSN; | |
89 } ExtendedTimerRec, *ExtendedTimerPtr; | |
90 | |
91 static ExtendedTimerRec gExtendedTimerRec; | |
92 | |
93 | |
94 int SDL_SYS_TimerInit(void) | |
95 { | |
96 /* We don't need a setup? */ | |
97 return(0); | |
98 } | |
99 | |
100 void SDL_SYS_TimerQuit(void) | |
101 { | |
102 /* We don't need a cleanup? */ | |
103 return; | |
104 } | |
105 | |
106 /* Our Stub routine to set up and then call the real routine. */ | |
107 pascal void TimerCallbackProc(TMTaskPtr tmTaskPtr) | |
108 { | |
109 Uint32 ms; | |
110 | |
111 WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN); | |
112 | |
113 ms = SDL_alarm_callback(SDL_alarm_interval); | |
114 if ( ms ) { | |
115 SDL_alarm_interval = ROUND_RESOLUTION(ms); | |
116 PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, | |
117 SDL_alarm_interval); | |
118 } else { | |
119 SDL_alarm_interval = 0; | |
120 } | |
121 } | |
122 | |
123 int SDL_SYS_StartTimer(void) | |
124 { | |
125 /* | |
126 * Configure the global structure that stores the timing information. | |
127 */ | |
128 gExtendedTimerRec.tmTask.qLink = NULL; | |
129 gExtendedTimerRec.tmTask.qType = 0; | |
130 gExtendedTimerRec.tmTask.tmAddr = NewTimerUPP(TimerCallbackProc); | |
131 gExtendedTimerRec.tmTask.tmCount = 0; | |
132 gExtendedTimerRec.tmTask.tmWakeUp = 0; | |
133 gExtendedTimerRec.tmTask.tmReserved = 0; | |
134 GetCurrentProcess(&gExtendedTimerRec.taskPSN); | |
135 | |
136 /* Install the task record */ | |
137 InsXTime((QElemPtr)&gExtendedTimerRec.tmTask); | |
138 | |
139 /* Go! */ | |
140 PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval); | |
141 return(0); | |
142 } | |
143 | |
144 void SDL_SYS_StopTimer(void) | |
145 { | |
146 RmvTime((QElemPtr)&gExtendedTimerRec.tmTask); | |
147 } |