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 7649 4f801cd08f3f
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
--- a/src/timer/SDL_timer.c	Sat Aug 17 09:54:30 2013 -0700
+++ b/src/timer/SDL_timer.c	Sat Aug 17 18:07:29 2013 -0400
@@ -26,8 +26,6 @@
 #include "SDL_cpuinfo.h"
 #include "SDL_thread.h"
 
-extern void SDL_StartTicks(void);
-
 /* #define DEBUG_TIMERS */
 
 typedef struct _SDL_Timer
@@ -72,17 +70,6 @@
 
 static SDL_TimerData SDL_timer_data;
 
-static Uint32 ticks_started = 0;
-
-void
-SDL_InitTicks(void)
-{
-    if (!ticks_started) {
-        SDL_StartTicks();
-        ticks_started = 1;
-    }
-}
-
 /* The idea here is that any thread might add a timer, but a single
  * thread manages the active timer queue, sorted by scheduling time.
  *
--- a/src/timer/beos/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
+++ b/src/timer/beos/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
@@ -27,10 +27,16 @@
 #include "SDL_timer.h"
 
 static bigtime_t start;
+static SDL_bool ticks_started = SDL_FALSE;
 
 void
-SDL_StartTicks(void)
+SDL_InitTicks(void)
 {
+    if (ticks_started) {
+        return;
+    }
+    ticks_started = SDL_TRUE;
+
     /* Set first ticks value */
     start = system_time();
 }
@@ -38,6 +44,10 @@
 Uint32
 SDL_GetTicks(void)
 {
+    if (!ticks_started) {
+        SDL_InitTicks();
+    }
+
     return ((system_time() - start) / 1000);
 }
 
--- a/src/timer/dummy/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
+++ b/src/timer/dummy/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
@@ -24,14 +24,24 @@
 
 #include "SDL_timer.h"
 
+static SDL_bool ticks_started = SDL_FALSE;
+
 void
-SDL_StartTicks(void)
+SDL_InitTicks(void)
 {
+    if (ticks_started) {
+        return;
+    }
+    ticks_started = SDL_TRUE;
 }
 
 Uint32
 SDL_GetTicks(void)
 {
+    if (!ticks_started) {
+        SDL_InitTicks();
+    }
+
     SDL_Unsupported();
     return 0;
 }
--- a/src/timer/psp/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
+++ b/src/timer/psp/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
@@ -29,14 +29,24 @@
 #include <pspthreadman.h>
 
 static struct timeval start;
+static SDL_bool ticks_started = SDL_FALSE;
 
-void SDL_StartTicks(void)
+void SDL_InitTicks(void)
 {
+    if (ticks_started) {
+        return;
+    }
+    ticks_started = SDL_TRUE;
+
     gettimeofday(&start, NULL);
 }
 
 Uint32 SDL_GetTicks(void)
 {
+    if (!ticks_started) {
+        SDL_InitTicks();
+    }
+
     struct timeval now;
     Uint32 ticks;
 
--- a/src/timer/unix/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
+++ b/src/timer/unix/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
@@ -56,10 +56,16 @@
 #endif
 static SDL_bool has_monotonic_time = SDL_FALSE;
 static struct timeval start_tv;
+static SDL_bool ticks_started = SDL_FALSE;
 
 void
-SDL_StartTicks(void)
+SDL_InitTicks(void)
 {
+    if (ticks_started) {
+        return;
+    }
+    ticks_started = SDL_TRUE;
+
     /* Set first ticks value */
 #if HAVE_CLOCK_GETTIME
     if (clock_gettime(CLOCK_MONOTONIC, &start_ts) == 0) {
@@ -80,6 +86,10 @@
 Uint32
 SDL_GetTicks(void)
 {
+    if (!ticks_started) {
+        SDL_InitTicks();
+    }
+
     Uint32 ticks;
     if (has_monotonic_time) {
 #if HAVE_CLOCK_GETTIME
@@ -105,6 +115,10 @@
 Uint64
 SDL_GetPerformanceCounter(void)
 {
+    if (!ticks_started) {
+        SDL_InitTicks();
+    }
+
     Uint64 ticks;
     if (has_monotonic_time) {
 #if HAVE_CLOCK_GETTIME
@@ -131,6 +145,10 @@
 Uint64
 SDL_GetPerformanceFrequency(void)
 {
+    if (!ticks_started) {
+        SDL_InitTicks();
+    }
+
     if (has_monotonic_time) {
 #if HAVE_CLOCK_GETTIME
         return 1000000000;
--- a/src/timer/windows/SDL_systimer.c	Sat Aug 17 09:54:30 2013 -0700
+++ b/src/timer/windows/SDL_systimer.c	Sat Aug 17 18:07:29 2013 -0400
@@ -29,6 +29,7 @@
 #include "SDL_hints.h"
 
 
+static BOOL ticks_started = FALSE; 
 /* The first (low-resolution) ticks value of the application */
 static DWORD start;
 
@@ -76,8 +77,13 @@
 }
 
 void
-SDL_StartTicks(void)
+SDL_InitTicks(void)
 {
+    if (ticks_started) {
+        return;
+    }
+    ticks_started = TRUE;
+
     /* Set first ticks value */
 #ifdef USE_GETTICKCOUNT
     start = GetTickCount();
@@ -102,6 +108,8 @@
 Uint32
 SDL_GetTicks(void)
 {
+    if (!ticks_started) SDL_InitTicks();
+
     DWORD now;
 #ifndef USE_GETTICKCOUNT
     LARGE_INTEGER hires_now;