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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
diff --git a/src/timer/SDL_timer.c b/src/timer/SDL_timer.c
index 5b6550c..2980f4d 100644
--- a/src/timer/SDL_timer.c
+++ b/src/timer/SDL_timer.c
@@ -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 @@ typedef struct {
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.
*
diff --git a/src/timer/beos/SDL_systimer.c b/src/timer/beos/SDL_systimer.c
index edaf275..fd22e44 100644
--- a/src/timer/beos/SDL_systimer.c
+++ b/src/timer/beos/SDL_systimer.c
@@ -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 @@ SDL_StartTicks(void)
Uint32
SDL_GetTicks(void)
{
+ if (!ticks_started) {
+ SDL_InitTicks();
+ }
+
return ((system_time() - start) / 1000);
}
diff --git a/src/timer/dummy/SDL_systimer.c b/src/timer/dummy/SDL_systimer.c
index 2b5a9f8..387675d 100644
--- a/src/timer/dummy/SDL_systimer.c
+++ b/src/timer/dummy/SDL_systimer.c
@@ -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;
}
diff --git a/src/timer/psp/SDL_systimer.c b/src/timer/psp/SDL_systimer.c
index 59bb4df..61c4690 100644
--- a/src/timer/psp/SDL_systimer.c
+++ b/src/timer/psp/SDL_systimer.c
@@ -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;
diff --git a/src/timer/unix/SDL_systimer.c b/src/timer/unix/SDL_systimer.c
index 596d749..7934032 100644
--- a/src/timer/unix/SDL_systimer.c
+++ b/src/timer/unix/SDL_systimer.c
@@ -56,10 +56,16 @@ mach_timebase_info_data_t mach_base_info;
#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 @@ SDL_StartTicks(void)
Uint32
SDL_GetTicks(void)
{
+ if (!ticks_started) {
+ SDL_InitTicks();
+ }
+
Uint32 ticks;
if (has_monotonic_time) {
#if HAVE_CLOCK_GETTIME
@@ -105,6 +115,10 @@ SDL_GetTicks(void)
Uint64
SDL_GetPerformanceCounter(void)
{
+ if (!ticks_started) {
+ SDL_InitTicks();
+ }
+
Uint64 ticks;
if (has_monotonic_time) {
#if HAVE_CLOCK_GETTIME
@@ -131,6 +145,10 @@ SDL_GetPerformanceCounter(void)
Uint64
SDL_GetPerformanceFrequency(void)
{
+ if (!ticks_started) {
+ SDL_InitTicks();
+ }
+
if (has_monotonic_time) {
#if HAVE_CLOCK_GETTIME
return 1000000000;
diff --git a/src/timer/windows/SDL_systimer.c b/src/timer/windows/SDL_systimer.c
index ce6bd7a..24a0c8c 100644
--- a/src/timer/windows/SDL_systimer.c
+++ b/src/timer/windows/SDL_systimer.c
@@ -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 @@ SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValu
}
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 @@ SDL_StartTicks(void)
Uint32
SDL_GetTicks(void)
{
+ if (!ticks_started) SDL_InitTicks();
+
DWORD now;
#ifndef USE_GETTICKCOUNT
LARGE_INTEGER hires_now;