src/timer/win32/SDL_systimer.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. SDL_StartTicks
  2. SDL_GetTicks
  3. SDL_Delay
  4. SDL_SYS_TimerInit
  5. SDL_SYS_TimerQuit
  6. TimerCallbackProc
  7. SDL_SYS_StartTimer
  8. SDL_SYS_StopTimer
  9. HandleAlarm
  10. SDL_SYS_TimerInit
  11. SDL_SYS_TimerQuit
  12. SDL_SYS_StartTimer
  13. SDL_SYS_StopTimer

   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: SDL_systimer.c,v 1.1.2.7 2001/02/13 10:05:49 hercules Exp $";
  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)
     /* [<][>][^][v][top][bottom][index][help] */
  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)
     /* [<][>][^][v][top][bottom][index][help] */
  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)
     /* [<][>][^][v][top][bottom][index][help] */
  73 {
  74         Sleep(ms);
  75 }
  76 
  77 #ifdef USE_SETTIMER
  78 
  79 static UINT WIN_timer;
  80 
  81 int SDL_SYS_TimerInit(void)
     /* [<][>][^][v][top][bottom][index][help] */
  82 {
  83         return(0);
  84 }
  85 
  86 void SDL_SYS_TimerQuit(void)
     /* [<][>][^][v][top][bottom][index][help] */
  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)
     /* [<][>][^][v][top][bottom][index][help] */
  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)
     /* [<][>][^][v][top][bottom][index][help] */
 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)
     /* [<][>][^][v][top][bottom][index][help] */
 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,
     /* [<][>][^][v][top][bottom][index][help] */
 137                                                 DWORD dw1, DWORD dw2)
 138 {
 139         SDL_ThreadedTimerCheck();
 140 }
 141 
 142 
 143 int SDL_SYS_TimerInit(void)
     /* [<][>][^][v][top][bottom][index][help] */
 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)
     /* [<][>][^][v][top][bottom][index][help] */
 163 {
 164         if ( timerID ) {
 165                 timeKillEvent(timerID);
 166         }
 167         timeEndPeriod(TIMER_RESOLUTION);
 168 }
 169 
 170 int SDL_SYS_StartTimer(void)
     /* [<][>][^][v][top][bottom][index][help] */
 171 {
 172         SDL_SetError("Internal logic error: Win32 uses threaded timer");
 173         return(-1);
 174 }
 175 
 176 void SDL_SYS_StopTimer(void)
     /* [<][>][^][v][top][bottom][index][help] */
 177 {
 178         return;
 179 }
 180 
 181 #endif /* USE_SETTIMER */

/* [<][>][^][v][top][bottom][index][help] */