diff --git a/CMakeLists.txt b/CMakeLists.txt index f3809454..cfee4c04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,8 @@ option(LOVR_BUILD_EXE "Build an executable" ON) option(LOVR_BUILD_SHARED "Build a shared library (takes precedence over LOVR_BUILD_EXE)" OFF) option(LOVR_BUILD_BUNDLE "On macOS, build a .app bundle instead of a raw program" OFF) +option(LOVR_USE_THREADLOCAL "Allow use of thread local storage; disable to run on Windows XP as a DLL" ON) + # Setup if(EMSCRIPTEN) string(CONCAT LOVR_EMSCRIPTEN_FLAGS @@ -286,6 +288,9 @@ if(LOVR_ENABLE_THREAD) find_package(Threads REQUIRED) set(LOVR_PTHREADS Threads::Threads) endif() + if (LOVR_USE_THREADLOCAL) + add_definitions(-DUSE_LOVR_STBI_THREADLOCAL) + endif() endif() # LÖVR diff --git a/src/lib/stb/stb_image.c b/src/lib/stb/stb_image.c index 2049b0f4..87375cf6 100644 --- a/src/lib/stb/stb_image.c +++ b/src/lib/stb/stb_image.c @@ -5,3 +5,7 @@ #define STBI_ONLY_HDR #define STBI_ASSERT(x) #include "stb_image.h" + +#ifndef LOVR_STBI_VFLIP_PATCH +#error "Somebody updated stb_image.h without replacing the thread-local patch" +#endif diff --git a/src/lib/stb/stb_image.h b/src/lib/stb/stb_image.h index ea5f5419..932a696a 100644 --- a/src/lib/stb/stb_image.h +++ b/src/lib/stb/stb_image.h @@ -995,7 +995,15 @@ static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp); static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp); #endif -static int stbi__vertically_flip_on_load = 0; +#define LOVR_STBI_VFLIP_PATCH +#ifdef USE_LOVR_STBI_THREADLOCAL +#include "lib/tinycthread/tinycthread.h" +#define THREADLOCAL_IF_AVAILABLE _Thread_local +#else +#define THREADLOCAL_IF_AVAILABLE +#endif + +static THREADLOCAL_IF_AVAILABLE int stbi__vertically_flip_on_load = 0; STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip) {