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