comparison src/timer/wince/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
children c9b51268668f
comparison
equal deleted inserted replaced
1179:abb4267e7028 1180:bdcb8bb4c831
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2004 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@libsdl.org
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 static Uint64 start_date;
36 static Uint64 start_ticks;
37
38 static Uint64 wce_ticks(void)
39 {
40 return((Uint64)GetTickCount());
41 }
42
43 static Uint64 wce_date(void)
44 {
45 union
46 {
47 FILETIME ftime;
48 Uint64 itime;
49 } ftime;
50 SYSTEMTIME stime;
51
52 GetSystemTime(&stime);
53 SystemTimeToFileTime(&stime,&ftime.ftime);
54 ftime.itime/=10000; // Convert 100ns intervals to 1ms intervals
55 // Remove ms portion, which can't be relied on
56 ftime.itime -= (ftime.itime % 1000);
57 return(ftime.itime);
58 }
59
60 static Sint32 wce_rel_ticks(void)
61 {
62 return((Sint32)(wce_ticks()-start_ticks));
63 }
64
65 static Sint32 wce_rel_date(void)
66 {
67 return((Sint32)(wce_date()-start_date));
68 }
69
70 /* Return time in ms relative to when SDL was started */
71 Uint32 SDL_GetTicks()
72 {
73 Sint32 offset=wce_rel_date()-wce_rel_ticks();
74 if((offset < -1000) || (offset > 1000))
75 {
76 // fprintf(stderr,"Time desync(%+d), resyncing\n",offset/1000);
77 start_ticks-=offset;
78 }
79
80 return((Uint32)wce_rel_ticks());
81 }
82
83 /* Give up approx. givem milliseconds to the OS. */
84 void SDL_Delay(Uint32 ms)
85 {
86 Sleep(ms);
87 }
88
89 /* Recard start-time of application for reference */
90 void SDL_StartTicks(void)
91 {
92 start_date=wce_date();
93 start_ticks=wce_ticks();
94 }
95
96 static UINT WIN_timer;
97
98 int SDL_SYS_TimerInit(void)
99 {
100 return(0);
101 }
102
103 void SDL_SYS_TimerQuit(void)
104 {
105 return;
106 }
107
108 /* Forward declaration because this is called by the timer callback */
109 int SDL_SYS_StartTimer(void);
110
111 static VOID CALLBACK TimerCallbackProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
112 {
113 Uint32 ms;
114
115 ms = SDL_alarm_callback(SDL_alarm_interval);
116 if ( ms != SDL_alarm_interval ) {
117 KillTimer(NULL, idEvent);
118 if ( ms ) {
119 SDL_alarm_interval = ROUND_RESOLUTION(ms);
120 SDL_SYS_StartTimer();
121 } else {
122 SDL_alarm_interval = 0;
123 }
124 }
125 }
126
127 int SDL_SYS_StartTimer(void)
128 {
129 int retval;
130
131 WIN_timer = SetTimer(NULL, 0, SDL_alarm_interval, TimerCallbackProc);
132 if ( WIN_timer ) {
133 retval = 0;
134 } else {
135 retval = -1;
136 }
137 return retval;
138 }
139
140 void SDL_SYS_StopTimer(void)
141 {
142 if ( WIN_timer ) {
143 KillTimer(NULL, WIN_timer);
144 WIN_timer = 0;
145 }
146 }
147