Skip to content

Commit

Permalink
auto init the ticks if the GetTicks and the like methods are called b…
Browse files Browse the repository at this point in the history
…efore SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
  • Loading branch information
urkle committed Aug 17, 2013
1 parent 6995ff1 commit e187810
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
13 changes: 0 additions & 13 deletions src/timer/SDL_timer.c
Expand Up @@ -26,8 +26,6 @@
#include "SDL_cpuinfo.h"
#include "SDL_thread.h"

extern void SDL_StartTicks(void);

/* #define DEBUG_TIMERS */

typedef struct _SDL_Timer
Expand Down Expand Up @@ -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.
*
Expand Down
12 changes: 11 additions & 1 deletion src/timer/beos/SDL_systimer.c
Expand Up @@ -27,17 +27,27 @@
#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();
}

Uint32
SDL_GetTicks(void)
{
if (!ticks_started) {
SDL_InitTicks();
}

return ((system_time() - start) / 1000);
}

Expand Down
12 changes: 11 additions & 1 deletion src/timer/dummy/SDL_systimer.c
Expand Up @@ -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;
}
Expand Down
12 changes: 11 additions & 1 deletion src/timer/psp/SDL_systimer.c
Expand Up @@ -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;

Expand Down
20 changes: 19 additions & 1 deletion src/timer/unix/SDL_systimer.c
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/timer/windows/SDL_systimer.c
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit e187810

Please sign in to comment.