Cleaned up the macro salsa in the Windows timer code.
authorRyan C. Gordon <icculus@icculus.org>
Mon, 20 Apr 2015 13:43:24 -0400
changeset 9567b69cac1cbc8f
parent 9566 7454bfce9202
child 9568 a02d603d1de3
Cleaned up the macro salsa in the Windows timer code.

- Removed USE_GETTICKCOUNT code; it's never used now.
- Reduced the number of preprocessor checks for WinRT.
- Renamed timeSetPeriod() so it doesn't look like a Win32 API call.
src/timer/windows/SDL_systimer.c
     1.1 --- a/src/timer/windows/SDL_systimer.c	Mon Apr 20 12:22:44 2015 -0400
     1.2 +++ b/src/timer/windows/SDL_systimer.c	Mon Apr 20 13:43:24 2015 -0400
     1.3 @@ -30,10 +30,9 @@
     1.4  
     1.5  
     1.6  /* The first (low-resolution) ticks value of the application */
     1.7 -static DWORD start;
     1.8 +static DWORD start = 0;
     1.9  static BOOL ticks_started = FALSE; 
    1.10  
    1.11 -#ifndef USE_GETTICKCOUNT
    1.12  /* Store if a high-resolution performance counter exists on the system */
    1.13  static BOOL hires_timer_available;
    1.14  /* The first high-resolution ticks value of the application */
    1.15 @@ -41,10 +40,10 @@
    1.16  /* The number of ticks per second of the high-resolution performance counter */
    1.17  static LARGE_INTEGER hires_ticks_per_second;
    1.18  
    1.19 +static void
    1.20 +SDL_SetSystemTimerResolution(const UINT uPeriod)
    1.21 +{
    1.22  #ifndef __WINRT__
    1.23 -static void
    1.24 -timeSetPeriod(const UINT uPeriod)
    1.25 -{
    1.26      static UINT timer_period = 0;
    1.27  
    1.28      if (uPeriod != timer_period) {
    1.29 @@ -58,6 +57,7 @@
    1.30              timeBeginPeriod(timer_period);
    1.31          }
    1.32      }
    1.33 +#endif
    1.34  }
    1.35  
    1.36  static void
    1.37 @@ -72,12 +72,9 @@
    1.38          uPeriod = 1;
    1.39      }
    1.40      if (uPeriod || oldValue != hint) {
    1.41 -        timeSetPeriod(uPeriod);
    1.42 +        SDL_SetSystemTimerResolution(uPeriod);
    1.43      }
    1.44  }
    1.45 -#endif /* ifndef __WINRT__ */
    1.46 -
    1.47 -#endif /* !USE_GETTICKCOUNT */
    1.48  
    1.49  void
    1.50  SDL_TicksInit(void)
    1.51 @@ -93,9 +90,6 @@
    1.52                          SDL_TimerResolutionChanged, NULL);
    1.53  
    1.54      /* Set first ticks value */
    1.55 -#ifdef USE_GETTICKCOUNT
    1.56 -    start = GetTickCount();
    1.57 -#else
    1.58      /* QueryPerformanceCounter has had problems in the past, but lots of games
    1.59         use it, so we'll rely on it here.
    1.60       */
    1.61 @@ -104,49 +98,36 @@
    1.62          QueryPerformanceCounter(&hires_start_ticks);
    1.63      } else {
    1.64          hires_timer_available = FALSE;
    1.65 -#ifdef __WINRT__
    1.66 -        start = 0;            /* the timer failed to start! */
    1.67 -#else
    1.68 +#ifndef __WINRT__
    1.69          start = timeGetTime();
    1.70  #endif /* __WINRT__ */
    1.71      }
    1.72 -#endif /* USE_GETTICKCOUNT */
    1.73  }
    1.74  
    1.75  void
    1.76  SDL_TicksQuit(void)
    1.77  {
    1.78 -#ifndef USE_GETTICKCOUNT
    1.79      if (!hires_timer_available) {
    1.80 -#ifndef __WINRT__
    1.81          SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
    1.82                              SDL_TimerResolutionChanged, NULL);
    1.83 -#endif /* __WINRT__ */
    1.84      }
    1.85 -#endif /* USE_GETTICKCOUNT */
    1.86  
    1.87 -#ifndef __WINRT__
    1.88 -    timeSetPeriod(0);  /* always release our timer resolution request. */
    1.89 -#endif
    1.90 +    SDL_SetSystemTimerResolution(0);  /* always release our timer resolution request. */
    1.91  
    1.92 +    start = 0;
    1.93      ticks_started = SDL_FALSE;
    1.94  }
    1.95  
    1.96  Uint32
    1.97  SDL_GetTicks(void)
    1.98  {
    1.99 -    DWORD now;
   1.100 -#ifndef USE_GETTICKCOUNT
   1.101 +    DWORD now = 0;
   1.102      LARGE_INTEGER hires_now;
   1.103 -#endif
   1.104  
   1.105      if (!ticks_started) {
   1.106          SDL_TicksInit();
   1.107      }
   1.108  
   1.109 -#ifdef USE_GETTICKCOUNT
   1.110 -    now = GetTickCount();
   1.111 -#else
   1.112      if (hires_timer_available) {
   1.113          QueryPerformanceCounter(&hires_now);
   1.114  
   1.115 @@ -156,13 +137,10 @@
   1.116  
   1.117          return (DWORD) hires_now.QuadPart;
   1.118      } else {
   1.119 -#ifdef __WINRT__
   1.120 -        now = 0;
   1.121 -#else
   1.122 +#ifndef __WINRT__
   1.123          now = timeGetTime();
   1.124  #endif /* __WINRT__ */
   1.125      }
   1.126 -#endif
   1.127  
   1.128      return (now - start);
   1.129  }
   1.130 @@ -189,35 +167,30 @@
   1.131      return frequency.QuadPart;
   1.132  }
   1.133  
   1.134 -/* Sleep() is not publicly available to apps in early versions of WinRT.
   1.135 - *
   1.136 - * Visual C++ 2013 Update 4 re-introduced Sleep() for Windows 8.1 and
   1.137 - * Windows Phone 8.1.
   1.138 - *
   1.139 - * Use the compiler version to determine availability.
   1.140 - *
   1.141 - * NOTE #1: _MSC_FULL_VER == 180030723 for Visual C++ 2013 Update 3.
   1.142 - * NOTE #2: Visual C++ 2013, when compiling for Windows 8.0 and
   1.143 - *    Windows Phone 8.0, uses the Visual C++ 2012 compiler to build
   1.144 - *    apps and libraries.
   1.145 - */
   1.146 -#if defined(__WINRT__) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER <= 180030723)
   1.147 -static void
   1.148 -Sleep(DWORD timeout)
   1.149 -{
   1.150 -    static HANDLE mutex = 0;
   1.151 -    if ( ! mutex )
   1.152 -    {
   1.153 -        mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
   1.154 -    }
   1.155 -    WaitForSingleObjectEx(mutex, timeout, FALSE);
   1.156 -}
   1.157 -#endif
   1.158 -
   1.159  void
   1.160  SDL_Delay(Uint32 ms)
   1.161  {
   1.162 +    /* Sleep() is not publicly available to apps in early versions of WinRT.
   1.163 +     *
   1.164 +     * Visual C++ 2013 Update 4 re-introduced Sleep() for Windows 8.1 and
   1.165 +     * Windows Phone 8.1.
   1.166 +     *
   1.167 +     * Use the compiler version to determine availability.
   1.168 +     *
   1.169 +     * NOTE #1: _MSC_FULL_VER == 180030723 for Visual C++ 2013 Update 3.
   1.170 +     * NOTE #2: Visual C++ 2013, when compiling for Windows 8.0 and
   1.171 +     *    Windows Phone 8.0, uses the Visual C++ 2012 compiler to build
   1.172 +     *    apps and libraries.
   1.173 +     */
   1.174 +#if defined(__WINRT__) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER <= 180030723)
   1.175 +    static HANDLE mutex = 0;
   1.176 +    if (!mutex) {
   1.177 +        mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
   1.178 +    }
   1.179 +    WaitForSingleObjectEx(mutex, ms, FALSE);
   1.180 +#else
   1.181      Sleep(ms);
   1.182 +#endif
   1.183  }
   1.184  
   1.185  #endif /* SDL_TIMER_WINDOWS */