comparison src/timer/win32/SDL_systimer.c @ 0:74212992fb08

Initial revision
author Sam Lantinga <slouken@lokigames.com>
date Thu, 26 Apr 2001 16:45:43 +0000
parents
children 69b8fac3e1c0
comparison
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Sam Lantinga
20 slouken@devolution.com
21 */
22
23 #ifdef SAVE_RCSID
24 static char rcsid =
25 "@(#) $Id$";
26 #endif
27
28 #include <windows.h>
29 #include <mmsystem.h>
30
31 #include "SDL_timer.h"
32 #include "SDL_timer_c.h"
33 #include "SDL_error.h"
34
35 #ifdef _WIN32_WCE
36 #define USE_GETTICKCOUNT
37 #define USE_SETTIMER
38 #endif
39
40 #define TIME_WRAP_VALUE (~(DWORD)0)
41
42 /* The first ticks value of the application */
43 static DWORD start;
44
45 void SDL_StartTicks(void)
46 {
47 /* Set first ticks value */
48 #ifdef USE_GETTICKCOUNT
49 start = GetTickCount();
50 #else
51 start = timeGetTime();
52 #endif
53 }
54
55 Uint32 SDL_GetTicks(void)
56 {
57 DWORD now, ticks;
58
59 #ifdef USE_GETTICKCOUNT
60 now = GetTickCount();
61 #else
62 now = timeGetTime();
63 #endif
64 if ( now < start ) {
65 ticks = (TIME_WRAP_VALUE-start) + now;
66 } else {
67 ticks = (now - start);
68 }
69 return(ticks);
70 }
71
72 void SDL_Delay(Uint32 ms)
73 {
74 Sleep(ms);
75 }
76
77 #ifdef USE_SETTIMER
78
79 static UINT WIN_timer;
80
81 int SDL_SYS_TimerInit(void)
82 {
83 return(0);
84 }
85
86 void SDL_SYS_TimerQuit(void)
87 {
88 return;
89 }
90
91 /* Forward declaration because this is called by the timer callback */
92 int SDL_SYS_StartTimer(void);
93
94 static VOID CALLBACK TimerCallbackProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
95 {
96 Uint32 ms;
97
98 ms = SDL_alarm_callback(SDL_alarm_interval);
99 if ( ms != SDL_alarm_interval ) {
100 KillTimer(NULL, idEvent);
101 if ( ms ) {
102 SDL_alarm_interval = ROUND_RESOLUTION(ms);
103 SDL_SYS_StartTimer();
104 } else {
105 SDL_alarm_interval = 0;
106 }
107 }
108 }
109
110 int SDL_SYS_StartTimer(void)
111 {
112 int retval;
113
114 WIN_timer = SetTimer(NULL, 0, SDL_alarm_interval, TimerCallbackProc);
115 if ( WIN_timer ) {
116 retval = 0;
117 } else {
118 retval = -1;
119 }
120 return retval;
121 }
122
123 void SDL_SYS_StopTimer(void)
124 {
125 if ( WIN_timer ) {
126 KillTimer(NULL, WIN_timer);
127 WIN_timer = 0;
128 }
129 }
130
131 #else /* !USE_SETTIMER */
132
133 /* Data to handle a single periodic alarm */
134 static UINT timerID = 0;
135
136 static void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser,
137 DWORD dw1, DWORD dw2)
138 {
139 SDL_ThreadedTimerCheck();
140 }
141
142
143 int SDL_SYS_TimerInit(void)
144 {
145 MMRESULT result;
146
147 /* Set timer resolution */
148 result = timeBeginPeriod(TIMER_RESOLUTION);
149 if ( result != TIMERR_NOERROR ) {
150 SDL_SetError("Warning: Can't set %d ms timer resolution",
151 TIMER_RESOLUTION);
152 }
153 /* Allow 10 ms of drift so we don't chew on CPU */
154 timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC);
155 if ( ! timerID ) {
156 SDL_SetError("timeSetEvent() failed");
157 return(-1);
158 }
159 return(SDL_SetTimerThreaded(1));
160 }
161
162 void SDL_SYS_TimerQuit(void)
163 {
164 if ( timerID ) {
165 timeKillEvent(timerID);
166 }
167 timeEndPeriod(TIMER_RESOLUTION);
168 }
169
170 int SDL_SYS_StartTimer(void)
171 {
172 SDL_SetError("Internal logic error: Win32 uses threaded timer");
173 return(-1);
174 }
175
176 void SDL_SYS_StopTimer(void)
177 {
178 return;
179 }
180
181 #endif /* USE_SETTIMER */