annotate src/timer/win32/SDL_systimer.c @ 1180:bdcb8bb4c831

From: Tyler Montbriand <tsm@accesscomm.ca> To: sdl@libsdl.org Date: Fri, 30 Sep 2005 02:24:50 -0600 Subject: [SDL] WinCE timers, continued Here's a strange timer for Windows CE that doesn't ignore time across suspends. It uses GetSystemTime to keep the time continuous, and GetTicks to get finer-grained readings than 1 second. It detects the difference between the GetTicks time and GetSystemTime time on power-on to keep the error within one second max. It's not a patch on the current win32 timer code -- took one look at that and figured it had more than enough #ifdefs already. It's windows-ce specific. Another thing I've noticed is that the Windows CE 4.0 and newer API has functions warn processes about suspends. This is something SDL REALLY needs for audio in particular, because turning it off while it's playing causes anything that uses audio to hardlock the system on power-on. Unfortunately I don't have 4.0 to play with. :(
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 22 Nov 2005 07:10:07 +0000
parents b8d311d90021
children c9b51268668f
rev   line source
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1 /*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
769
b8d311d90021 Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents: 517
diff changeset
3 Copyright (C) 1997-2004 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
6 modify it under the terms of the GNU Library General Public
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
8 version 2 of the License, or (at your option) any later version.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
13 Library General Public License for more details.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
14
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
15 You should have received a copy of the GNU Library General Public
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
16 License along with this library; if not, write to the Free
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
19 Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 89
diff changeset
20 slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
21 */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
22
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23 #ifdef SAVE_RCSID
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24 static char rcsid =
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25 "@(#) $Id$";
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
26 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
27
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28 #include <windows.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29 #include <mmsystem.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31 #include "SDL_timer.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
32 #include "SDL_timer_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
33 #include "SDL_error.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
34
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
35 #ifdef _WIN32_WCE
1180
bdcb8bb4c831 From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
36 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead.
bdcb8bb4c831 From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
37
bdcb8bb4c831 From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
38 /* but if you really want to use this file, use these #defines... */
bdcb8bb4c831 From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
39 #define USE_GETTICKCOUNT
bdcb8bb4c831 From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
40 #define USE_SETTIMER
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
41 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
42
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
43 #define TIME_WRAP_VALUE (~(DWORD)0)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
44
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
45 /* The first (low-resolution) ticks value of the application */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
46 static DWORD start;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
47
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
48 #ifndef USE_GETTICKCOUNT
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
49 /* Store if a high-resolution performance counter exists on the system */
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
50 static BOOL hires_timer_available;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
51 /* The first high-resolution ticks value of the application */
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
52 static LARGE_INTEGER hires_start_ticks;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
53 /* The number of ticks per second of the high-resolution performance counter */
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
54 static LARGE_INTEGER hires_ticks_per_second;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
55 #endif
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
56
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
57 void SDL_StartTicks(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
58 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
59 /* Set first ticks value */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
60 #ifdef USE_GETTICKCOUNT
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
61 start = GetTickCount();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
62 #else
326
72d55d02cb47 Disabled QueryPerformanceCounter(), due to problems on Win2K
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
63 #if 0 /* Apparently there are problems with QPC on Win2K */
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
64 if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE)
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
65 {
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
66 hires_timer_available = TRUE;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
67 QueryPerformanceCounter(&hires_start_ticks);
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
68 }
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
69 else
326
72d55d02cb47 Disabled QueryPerformanceCounter(), due to problems on Win2K
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
70 #endif
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
71 {
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
72 hires_timer_available = FALSE;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
73 timeBeginPeriod(1); /* use 1 ms timer precision */
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
74 start = timeGetTime();
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
75 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
76 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
77 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
78
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
79 Uint32 SDL_GetTicks(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
80 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
81 DWORD now, ticks;
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
82 #ifndef USE_GETTICKCOUNT
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
83 LARGE_INTEGER hires_now;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
84 #endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
85
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
86 #ifdef USE_GETTICKCOUNT
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
87 now = GetTickCount();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
88 #else
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
89 if (hires_timer_available)
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
90 {
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
91 QueryPerformanceCounter(&hires_now);
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
92
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
93 hires_now.QuadPart -= hires_start_ticks.QuadPart;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
94 hires_now.QuadPart *= 1000;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
95 hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
96
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
97 return (DWORD)hires_now.QuadPart;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
98 }
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
99 else
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
100 {
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
101 now = timeGetTime();
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
102 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
103 #endif
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
104
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
105 if ( now < start ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
106 ticks = (TIME_WRAP_VALUE-start) + now;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
107 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
108 ticks = (now - start);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
109 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
110 return(ticks);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
111 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
112
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
113 void SDL_Delay(Uint32 ms)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
114 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
115 Sleep(ms);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
116 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
117
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
118 #ifdef USE_SETTIMER
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
119
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
120 static UINT WIN_timer;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
121
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
122 int SDL_SYS_TimerInit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
123 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
124 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
125 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
126
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
127 void SDL_SYS_TimerQuit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
128 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
129 return;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
130 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
131
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
132 /* Forward declaration because this is called by the timer callback */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
133 int SDL_SYS_StartTimer(void);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
134
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
135 static VOID CALLBACK TimerCallbackProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
136 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
137 Uint32 ms;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
138
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
139 ms = SDL_alarm_callback(SDL_alarm_interval);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
140 if ( ms != SDL_alarm_interval ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
141 KillTimer(NULL, idEvent);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
142 if ( ms ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
143 SDL_alarm_interval = ROUND_RESOLUTION(ms);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
144 SDL_SYS_StartTimer();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
145 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
146 SDL_alarm_interval = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
147 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
148 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
149 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
150
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
151 int SDL_SYS_StartTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
152 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
153 int retval;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
154
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
155 WIN_timer = SetTimer(NULL, 0, SDL_alarm_interval, TimerCallbackProc);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
156 if ( WIN_timer ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
157 retval = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
158 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
159 retval = -1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
160 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
161 return retval;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
162 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
163
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
164 void SDL_SYS_StopTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
165 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
166 if ( WIN_timer ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
167 KillTimer(NULL, WIN_timer);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
168 WIN_timer = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
169 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
170 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
171
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
172 #else /* !USE_SETTIMER */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
173
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
174 /* Data to handle a single periodic alarm */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
175 static UINT timerID = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
176
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
177 static void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
178 DWORD dw1, DWORD dw2)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
179 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
180 SDL_ThreadedTimerCheck();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
181 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
182
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
183
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
184 int SDL_SYS_TimerInit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
185 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
186 MMRESULT result;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
187
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
188 /* Set timer resolution */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
189 result = timeBeginPeriod(TIMER_RESOLUTION);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
190 if ( result != TIMERR_NOERROR ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
191 SDL_SetError("Warning: Can't set %d ms timer resolution",
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
192 TIMER_RESOLUTION);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
193 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
194 /* Allow 10 ms of drift so we don't chew on CPU */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
195 timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
196 if ( ! timerID ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
197 SDL_SetError("timeSetEvent() failed");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
198 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
199 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
200 return(SDL_SetTimerThreaded(1));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
201 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
202
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
203 void SDL_SYS_TimerQuit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
204 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
205 if ( timerID ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
206 timeKillEvent(timerID);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
207 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
208 timeEndPeriod(TIMER_RESOLUTION);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
209 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
210
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
211 int SDL_SYS_StartTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
212 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
213 SDL_SetError("Internal logic error: Win32 uses threaded timer");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
214 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
215 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
216
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
217 void SDL_SYS_StopTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
218 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
219 return;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
220 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
221
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
222 #endif /* USE_SETTIMER */