auto init the ticks if the GetTicks and the like methods are called before SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
authorEdward Rudd <urkle@outoforder.cc>
Sat, 17 Aug 2013 18:07:29 -0400
changeset 76494f801cd08f3f
parent 7648 297c43ff3d7e
child 7650 cd3465de2f42
auto init the ticks if the GetTicks and the like methods are called before SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
src/timer/SDL_timer.c
src/timer/beos/SDL_systimer.c
src/timer/dummy/SDL_systimer.c
src/timer/psp/SDL_systimer.c
src/timer/unix/SDL_systimer.c
src/timer/windows/SDL_systimer.c
     1.1 --- a/src/timer/SDL_timer.c	Sat Aug 17 09:54:30 2013 -0700
     1.2 +++ b/src/timer/SDL_timer.c	Sat Aug 17 18:07:29 2013 -0400
     1.3 @@ -26,8 +26,6 @@
     1.4  #include "SDL_cpuinfo.h"
     1.5  #include "SDL_thread.h"
     1.6  
     1.7 -extern void SDL_StartTicks(void);
     1.8 -
     1.9  /* #define DEBUG_TIMERS */
    1.10  
    1.11  typedef struct _SDL_Timer
    1.12 @@ -72,17 +70,6 @@
    1.13  
    1.14  static SDL_TimerData SDL_timer_data;
    1.15  
    1.16 -static Uint32 ticks_started = 0;
    1.17 -
    1.18 -void
    1.19 -SDL_InitTicks(void)
    1.20 -{
    1.21 -    if (!ticks_started) {
    1.22 -        SDL_StartTicks();
    1.23 -        ticks_started = 1;
    1.24 -    }
    1.25 -}
    1.26 -
    1.27  /* The idea here is that any thread might add a timer, but a single
    1.28   * thread manages the active timer queue, sorted by scheduling time.
    1.29   *
     2.1 --- a/src/timer/beos/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
     2.2 +++ b/src/timer/beos/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
     2.3 @@ -27,10 +27,16 @@
     2.4  #include "SDL_timer.h"
     2.5  
     2.6  static bigtime_t start;
     2.7 +static SDL_bool ticks_started = SDL_FALSE;
     2.8  
     2.9  void
    2.10 -SDL_StartTicks(void)
    2.11 +SDL_InitTicks(void)
    2.12  {
    2.13 +    if (ticks_started) {
    2.14 +        return;
    2.15 +    }
    2.16 +    ticks_started = SDL_TRUE;
    2.17 +
    2.18      /* Set first ticks value */
    2.19      start = system_time();
    2.20  }
    2.21 @@ -38,6 +44,10 @@
    2.22  Uint32
    2.23  SDL_GetTicks(void)
    2.24  {
    2.25 +    if (!ticks_started) {
    2.26 +        SDL_InitTicks();
    2.27 +    }
    2.28 +
    2.29      return ((system_time() - start) / 1000);
    2.30  }
    2.31  
     3.1 --- a/src/timer/dummy/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
     3.2 +++ b/src/timer/dummy/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
     3.3 @@ -24,14 +24,24 @@
     3.4  
     3.5  #include "SDL_timer.h"
     3.6  
     3.7 +static SDL_bool ticks_started = SDL_FALSE;
     3.8 +
     3.9  void
    3.10 -SDL_StartTicks(void)
    3.11 +SDL_InitTicks(void)
    3.12  {
    3.13 +    if (ticks_started) {
    3.14 +        return;
    3.15 +    }
    3.16 +    ticks_started = SDL_TRUE;
    3.17  }
    3.18  
    3.19  Uint32
    3.20  SDL_GetTicks(void)
    3.21  {
    3.22 +    if (!ticks_started) {
    3.23 +        SDL_InitTicks();
    3.24 +    }
    3.25 +
    3.26      SDL_Unsupported();
    3.27      return 0;
    3.28  }
     4.1 --- a/src/timer/psp/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
     4.2 +++ b/src/timer/psp/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
     4.3 @@ -29,14 +29,24 @@
     4.4  #include <pspthreadman.h>
     4.5  
     4.6  static struct timeval start;
     4.7 +static SDL_bool ticks_started = SDL_FALSE;
     4.8  
     4.9 -void SDL_StartTicks(void)
    4.10 +void SDL_InitTicks(void)
    4.11  {
    4.12 +    if (ticks_started) {
    4.13 +        return;
    4.14 +    }
    4.15 +    ticks_started = SDL_TRUE;
    4.16 +
    4.17      gettimeofday(&start, NULL);
    4.18  }
    4.19  
    4.20  Uint32 SDL_GetTicks(void)
    4.21  {
    4.22 +    if (!ticks_started) {
    4.23 +        SDL_InitTicks();
    4.24 +    }
    4.25 +
    4.26      struct timeval now;
    4.27      Uint32 ticks;
    4.28  
     5.1 --- a/src/timer/unix/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
     5.2 +++ b/src/timer/unix/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
     5.3 @@ -56,10 +56,16 @@
     5.4  #endif
     5.5  static SDL_bool has_monotonic_time = SDL_FALSE;
     5.6  static struct timeval start_tv;
     5.7 +static SDL_bool ticks_started = SDL_FALSE;
     5.8  
     5.9  void
    5.10 -SDL_StartTicks(void)
    5.11 +SDL_InitTicks(void)
    5.12  {
    5.13 +    if (ticks_started) {
    5.14 +        return;
    5.15 +    }
    5.16 +    ticks_started = SDL_TRUE;
    5.17 +
    5.18      /* Set first ticks value */
    5.19  #if HAVE_CLOCK_GETTIME
    5.20      if (clock_gettime(CLOCK_MONOTONIC, &start_ts) == 0) {
    5.21 @@ -80,6 +86,10 @@
    5.22  Uint32
    5.23  SDL_GetTicks(void)
    5.24  {
    5.25 +    if (!ticks_started) {
    5.26 +        SDL_InitTicks();
    5.27 +    }
    5.28 +
    5.29      Uint32 ticks;
    5.30      if (has_monotonic_time) {
    5.31  #if HAVE_CLOCK_GETTIME
    5.32 @@ -105,6 +115,10 @@
    5.33  Uint64
    5.34  SDL_GetPerformanceCounter(void)
    5.35  {
    5.36 +    if (!ticks_started) {
    5.37 +        SDL_InitTicks();
    5.38 +    }
    5.39 +
    5.40      Uint64 ticks;
    5.41      if (has_monotonic_time) {
    5.42  #if HAVE_CLOCK_GETTIME
    5.43 @@ -131,6 +145,10 @@
    5.44  Uint64
    5.45  SDL_GetPerformanceFrequency(void)
    5.46  {
    5.47 +    if (!ticks_started) {
    5.48 +        SDL_InitTicks();
    5.49 +    }
    5.50 +
    5.51      if (has_monotonic_time) {
    5.52  #if HAVE_CLOCK_GETTIME
    5.53          return 1000000000;
     6.1 --- a/src/timer/windows/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
     6.2 +++ b/src/timer/windows/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
     6.3 @@ -29,6 +29,7 @@
     6.4  #include "SDL_hints.h"
     6.5  
     6.6  
     6.7 +static BOOL ticks_started = FALSE; 
     6.8  /* The first (low-resolution) ticks value of the application */
     6.9  static DWORD start;
    6.10  
    6.11 @@ -76,8 +77,13 @@
    6.12  }
    6.13  
    6.14  void
    6.15 -SDL_StartTicks(void)
    6.16 +SDL_InitTicks(void)
    6.17  {
    6.18 +    if (ticks_started) {
    6.19 +        return;
    6.20 +    }
    6.21 +    ticks_started = TRUE;
    6.22 +
    6.23      /* Set first ticks value */
    6.24  #ifdef USE_GETTICKCOUNT
    6.25      start = GetTickCount();
    6.26 @@ -102,6 +108,8 @@
    6.27  Uint32
    6.28  SDL_GetTicks(void)
    6.29  {
    6.30 +    if (!ticks_started) SDL_InitTicks();
    6.31 +
    6.32      DWORD now;
    6.33  #ifndef USE_GETTICKCOUNT
    6.34      LARGE_INTEGER hires_now;