Merge remote-tracking branch 'sgayda2/call_once' into develop

develop
lethosor 2018-07-14 21:08:15 -04:00
commit 790d16cc1c
1 changed files with 26 additions and 39 deletions

@ -26,6 +26,7 @@ distribution.
#include <windows.h> #include <windows.h>
#include <stdint.h> #include <stdint.h>
#include <mutex>
#include <vector> #include <vector>
#include <string> #include <string>
#include "Core.h" #include "Core.h"
@ -40,8 +41,8 @@ distribution.
// we don't know which of the SDL functions will be called first... so we // we don't know which of the SDL functions will be called first... so we
// just catch the first one and init all our function pointers at that time // just catch the first one and init all our function pointers at that time
bool FirstCall(void); static void InitSDLPointers(void);
bool inited = false; static std::once_flag inited;
/// wrappers for SDL 1.2 functions used in 40d16 /// wrappers for SDL 1.2 functions used in 40d16
/***** Condition variables /***** Condition variables
@ -612,8 +613,7 @@ DFhackCExport int SDL_NumJoysticks(void)
static void (*_SDL_GL_SwapBuffers)(void) = 0; static void (*_SDL_GL_SwapBuffers)(void) = 0;
DFhackCExport void SDL_GL_SwapBuffers(void) DFhackCExport void SDL_GL_SwapBuffers(void)
{ {
if(!inited) InitSDLPointers();
FirstCall();
_SDL_GL_SwapBuffers(); _SDL_GL_SwapBuffers();
} }
@ -621,16 +621,14 @@ DFhackCExport void SDL_GL_SwapBuffers(void)
static int (*_SDL_Flip)(void * some_ptr) = 0; static int (*_SDL_Flip)(void * some_ptr) = 0;
DFhackCExport int SDL_Flip(void * some_ptr) DFhackCExport int SDL_Flip(void * some_ptr)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_Flip(some_ptr); return _SDL_Flip(some_ptr);
} }
static int (*_SDL_Init)(uint32_t flags) = 0; static int (*_SDL_Init)(uint32_t flags) = 0;
DFhackCExport int SDL_Init(uint32_t flags) DFhackCExport int SDL_Init(uint32_t flags)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_Init(flags); return _SDL_Init(flags);
} }
@ -640,16 +638,14 @@ MORE CRAP
static void * (*_SDL_CreateSemaphore)(uint32_t initial_value) = 0; static void * (*_SDL_CreateSemaphore)(uint32_t initial_value) = 0;
DFhackCExport void *SDL_CreateSemaphore(uint32_t initial_value) DFhackCExport void *SDL_CreateSemaphore(uint32_t initial_value)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_CreateSemaphore(initial_value); return _SDL_CreateSemaphore(initial_value);
} }
static vPtr (*_SDL_CreateThread)(int (*fn)(void *), void *data) = 0; static vPtr (*_SDL_CreateThread)(int (*fn)(void *), void *data) = 0;
DFhackCExport vPtr SDL_CreateThread(int (*fn)(void *), void *data) DFhackCExport vPtr SDL_CreateThread(int (*fn)(void *), void *data)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_CreateThread(fn,data); return _SDL_CreateThread(fn,data);
} }
@ -657,93 +653,81 @@ DFhackCExport vPtr SDL_CreateThread(int (*fn)(void *), void *data)
static void (*_SDL_Delay)(uint32_t ms) = 0; static void (*_SDL_Delay)(uint32_t ms) = 0;
DFhackCExport void SDL_Delay(uint32_t ms) DFhackCExport void SDL_Delay(uint32_t ms)
{ {
if(!inited) InitSDLPointers();
FirstCall();
_SDL_Delay(ms); _SDL_Delay(ms);
} }
static void (*_SDL_DestroySemaphore)(void *sem) = 0; static void (*_SDL_DestroySemaphore)(void *sem) = 0;
DFhackCExport void SDL_DestroySemaphore(void *sem) DFhackCExport void SDL_DestroySemaphore(void *sem)
{ {
if(!inited) InitSDLPointers();
FirstCall();
_SDL_DestroySemaphore(sem); _SDL_DestroySemaphore(sem);
} }
static uint8_t (*_SDL_GetAppState)(void) = 0; static uint8_t (*_SDL_GetAppState)(void) = 0;
DFhackCExport uint8_t SDL_GetAppState(void) DFhackCExport uint8_t SDL_GetAppState(void)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_GetAppState(); return _SDL_GetAppState();
} }
static uint8_t (*_SDL_GetMouseState)(int *, int *) = 0; static uint8_t (*_SDL_GetMouseState)(int *, int *) = 0;
DFhackCExport uint8_t SDL_GetMouseState(int *x, int *y) DFhackCExport uint8_t SDL_GetMouseState(int *x, int *y)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_GetMouseState(x,y); return _SDL_GetMouseState(x,y);
} }
static int (*_SDL_InitSubSystem)(uint32_t flags) = 0; static int (*_SDL_InitSubSystem)(uint32_t flags) = 0;
DFhackCExport int SDL_InitSubSystem(uint32_t flags) DFhackCExport int SDL_InitSubSystem(uint32_t flags)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_InitSubSystem(flags); return _SDL_InitSubSystem(flags);
} }
static int (*_SDL_SemPost)(void *sem) = 0; static int (*_SDL_SemPost)(void *sem) = 0;
DFhackCExport int SDL_SemPost(void *sem) DFhackCExport int SDL_SemPost(void *sem)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_SemPost(sem); return _SDL_SemPost(sem);
} }
static int (*_SDL_SemTryWait)(void *sem) = 0; static int (*_SDL_SemTryWait)(void *sem) = 0;
DFhackCExport int SDL_SemTryWait(void *sem) DFhackCExport int SDL_SemTryWait(void *sem)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_SemTryWait(sem); return _SDL_SemTryWait(sem);
} }
static int (*_SDL_SemWait)(void *sem) = 0; static int (*_SDL_SemWait)(void *sem) = 0;
DFhackCExport int SDL_SemWait(void *sem) DFhackCExport int SDL_SemWait(void *sem)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_SemWait(sem); return _SDL_SemWait(sem);
} }
static uint32_t (*_SDL_ThreadID)(void) = 0; static uint32_t (*_SDL_ThreadID)(void) = 0;
DFhackCExport uint32_t SDL_ThreadID(void) DFhackCExport uint32_t SDL_ThreadID(void)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_ThreadID(); return _SDL_ThreadID();
} }
static char* (*_SDL_getenv)(const char *name) = 0; static char* (*_SDL_getenv)(const char *name) = 0;
DFhackCExport char* SDL_getenv(const char *name) DFhackCExport char* SDL_getenv(const char *name)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_getenv(name); return _SDL_getenv(name);
} }
static size_t (*_SDL_strlcat)(char *dst, const char *src, size_t maxlen) = 0; static size_t (*_SDL_strlcat)(char *dst, const char *src, size_t maxlen) = 0;
DFhackCExport size_t SDL_strlcat(char *dst, const char *src, size_t maxlen) DFhackCExport size_t SDL_strlcat(char *dst, const char *src, size_t maxlen)
{ {
if(!inited) InitSDLPointers();
FirstCall();
return _SDL_strlcat(dst, src, maxlen); return _SDL_strlcat(dst, src, maxlen);
} }
// FIXME: this has to be thread-safe. void FirstCall()
bool FirstCall()
{ {
// reroute stdout and stderr // reroute stdout and stderr
freopen("stdout.log", "w", stdout); freopen("stdout.log", "w", stdout);
@ -753,7 +737,7 @@ bool FirstCall()
{ {
MessageBox(0,"Can't load SDLreal.dll\n","Error", MB_OK); MessageBox(0,"Can't load SDLreal.dll\n","Error", MB_OK);
fprintf(stderr, "Can't load SDLreal.dll\n"); fprintf(stderr, "Can't load SDLreal.dll\n");
return 0; return;
} }
fprintf(stderr, "FirstCall()\n"); fprintf(stderr, "FirstCall()\n");
// stuff for DF // stuff for DF
@ -836,6 +820,9 @@ bool FirstCall()
_SDL_EnableUNICODE(1); _SDL_EnableUNICODE(1);
fprintf(stderr,"Initized HOOKS!\n"); fprintf(stderr,"Initized HOOKS!\n");
inited = true; }
return 1;
void InitSDLPointers()
{
std::call_once(inited, [](){ FirstCall(); });
} }