Windows: Always set the system timer resolution to 1ms by default.
authorRyan C. Gordon <icculus@icculus.org>
Mon, 20 Apr 2015 12:22:44 -0400
changeset 95667454bfce9202
parent 9563 537881242756
child 9567 b69cac1cbc8f
Windows: Always set the system timer resolution to 1ms by default.

An existing hint lets apps that don't need the timer resolution changed avoid
this, to save battery, etc, but this fixes several problems in timing, audio
callbacks not firing fast enough, etc.

Fixes Bugzilla #2944.
src/timer/windows/SDL_systimer.c
     1.1 --- a/src/timer/windows/SDL_systimer.c	Thu Apr 16 22:04:35 2015 +0200
     1.2 +++ b/src/timer/windows/SDL_systimer.c	Mon Apr 20 12:22:44 2015 -0400
     1.3 @@ -43,7 +43,7 @@
     1.4  
     1.5  #ifndef __WINRT__
     1.6  static void
     1.7 -timeSetPeriod(UINT uPeriod)
     1.8 +timeSetPeriod(const UINT uPeriod)
     1.9  {
    1.10      static UINT timer_period = 0;
    1.11  
    1.12 @@ -87,6 +87,11 @@
    1.13      }
    1.14      ticks_started = SDL_TRUE;
    1.15  
    1.16 +    /* if we didn't set a precision, set it high. This affects lots of things
    1.17 +       on Windows besides the SDL timers, like audio callbacks, etc. */
    1.18 +    SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
    1.19 +                        SDL_TimerResolutionChanged, NULL);
    1.20 +
    1.21      /* Set first ticks value */
    1.22  #ifdef USE_GETTICKCOUNT
    1.23      start = GetTickCount();
    1.24 @@ -102,11 +107,7 @@
    1.25  #ifdef __WINRT__
    1.26          start = 0;            /* the timer failed to start! */
    1.27  #else
    1.28 -        timeSetPeriod(1);     /* use 1 ms timer precision */
    1.29          start = timeGetTime();
    1.30 -
    1.31 -        SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
    1.32 -                            SDL_TimerResolutionChanged, NULL);
    1.33  #endif /* __WINRT__ */
    1.34      }
    1.35  #endif /* USE_GETTICKCOUNT */
    1.36 @@ -120,12 +121,14 @@
    1.37  #ifndef __WINRT__
    1.38          SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
    1.39                              SDL_TimerResolutionChanged, NULL);
    1.40 -
    1.41 -        timeSetPeriod(0);
    1.42  #endif /* __WINRT__ */
    1.43      }
    1.44  #endif /* USE_GETTICKCOUNT */
    1.45  
    1.46 +#ifndef __WINRT__
    1.47 +    timeSetPeriod(0);  /* always release our timer resolution request. */
    1.48 +#endif
    1.49 +
    1.50      ticks_started = SDL_FALSE;
    1.51  }
    1.52