sdl2-compat: Updated for latest SDL3 SDL_CreateThread changes.

From a86072260e81ec62b5f75449e3e5bddbfa5c20a5 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 22 May 2024 12:36:54 -0400
Subject: [PATCH] Updated for latest SDL3 SDL_CreateThread changes.

---
 src/sdl2_compat.c          | 57 ++++++++++++++++++++++++++++++--------
 src/sdl3_include_wrapper.h | 10 +++----
 src/sdl3_syms.h            |  2 +-
 3 files changed, 51 insertions(+), 18 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 70fcfad..3a8eab3 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -8190,19 +8190,51 @@ SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id)
     return retval;
 }
 
-#if (defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)) && !defined(SDL_PLATFORM_WINRT)
 
-SDL_DECLSPEC SDL_Thread *SDLCALL
-SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
-                 SDL_BeginThreadEx pfnBeginThread,
-                 SDL_EndThreadEx pfnEndThread)
+static SDL_Thread *SDL2_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *userdata, SDL_FunctionPointer pfnBeginThread, SDL_FunctionPointer pfnEndThread)
+{
+    const SDL_PropertiesID props = SDL3_CreateProperties();
+    SDL_Thread *thread;
+    SDL3_SetProperty(props, SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER, (void *) fn);
+    SDL3_SetStringProperty(props, SDL_PROP_THREAD_CREATE_NAME_STRING, name);
+    SDL3_SetProperty(props, SDL_PROP_THREAD_CREATE_USERDATA_POINTER, userdata);
+    SDL3_SetNumberProperty(props, SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER, (Sint64) stacksize);
+    thread = SDL3_CreateThreadWithPropertiesRuntime(props, pfnBeginThread, pfnEndThread);
+    SDL3_DestroyProperties(props);
+    return thread;
+}
+
+static SDL_Thread *SDL2_CreateThread(SDL_ThreadFunction fn, const char *name, void *userdata, SDL_FunctionPointer pfnBeginThread, SDL_FunctionPointer pfnEndThread)
 {
     size_t stacksize = 0;
     const char *hint = SDL3_GetHint("SDL_THREAD_STACK_SIZE");
     if (hint) {
         stacksize = (size_t)SDL3_strtoul(hint, NULL, 0);
     }
-    return SDL3_CreateThreadWithStackSizeRuntime(fn, name, stacksize, data, (SDL_BeginThreadEx) pfnBeginThread, (SDL_EndThreadEx) pfnEndThread);
+    return SDL2_CreateThreadWithStackSize(fn, name, stacksize, userdata, pfnBeginThread, pfnEndThread);
+}
+
+#if (defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)) && !defined(SDL_PLATFORM_WINRT)
+
+typedef uintptr_t (__cdecl * pfnSDL_CurrentBeginThread)
+                   (void *, unsigned, unsigned (__stdcall *func)(void *),
+                    void * /*arg*/, unsigned, unsigned * /* threadID */);
+typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
+
+SDL_DECLSPEC SDL_Thread *SDLCALL
+SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
+                 pfnSDL_CurrentBeginThread pfnBeginThread,
+                 pfnSDL_CurrentEndThread pfnEndThread)
+{
+    return SDL2_CreateThread(fn, name, data, (SDL_FunctionPointer) pfnBeginThread, (SDL_FunctionPointer) pfnEndThread);
+}
+
+extern SDL_DECLSPEC SDL_Thread *SDLCALL
+SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data,
+                              pfnSDL_CurrentBeginThread pfnBeginThread,
+                              pfnSDL_CurrentEndThread pfnEndThread);
+{
+    return SDL2_CreateThreadWithStackSize(fn, name, stacksize, data, (SDL_FunctionPointer) pfnBeginThread, (SDL_FunctionPointer) pfnEndThread);
 }
 
 #else
@@ -8210,12 +8242,13 @@ SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
 SDL_DECLSPEC SDL_Thread *SDLCALL
 SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data)
 {
-    size_t stacksize = 0;
-    const char *hint = SDL3_GetHint("SDL_THREAD_STACK_SIZE");
-    if (hint) {
-        stacksize = (size_t)SDL3_strtoul(hint, NULL, 0);
-    }
-    return SDL3_CreateThreadWithStackSizeRuntime(fn, name, stacksize, data, NULL, NULL);
+    return SDL2_CreateThread(fn, name, data, NULL, NULL);
+}
+
+extern SDL_DECLSPEC SDL_Thread *SDLCALL
+SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data)
+{
+    return SDL2_CreateThreadWithStackSize(fn, name, stacksize, data, NULL, NULL);
 }
 
 #endif /* (SDL_PLATFORM_WIN32 || SDL_PLATFORM_GDK) && !SDL_PLATFORM_WINRT */
diff --git a/src/sdl3_include_wrapper.h b/src/sdl3_include_wrapper.h
index e055acf..77f7dce 100644
--- a/src/sdl3_include_wrapper.h
+++ b/src/sdl3_include_wrapper.h
@@ -117,7 +117,7 @@
 #define SDL_CreateTextureFromSurface IGNORE_THIS_VERSION_OF_SDL_CreateTextureFromSurface
 #define SDL_CreateTextureWithProperties IGNORE_THIS_VERSION_OF_SDL_CreateTextureWithProperties
 #define SDL_CreateThreadRuntime IGNORE_THIS_VERSION_OF_SDL_CreateThreadRuntime
-#define SDL_CreateThreadWithStackSizeRuntime IGNORE_THIS_VERSION_OF_SDL_CreateThreadWithStackSizeRuntime
+#define SDL_CreateThreadWithPropertiesRuntime IGNORE_THIS_VERSION_OF_SDL_CreateThreadWithPropertiesRuntime
 #define SDL_CreateWindow IGNORE_THIS_VERSION_OF_SDL_CreateWindow
 #define SDL_CreateWindowAndRenderer IGNORE_THIS_VERSION_OF_SDL_CreateWindowAndRenderer
 #define SDL_CreateWindowWithProperties IGNORE_THIS_VERSION_OF_SDL_CreateWindowWithProperties
@@ -1402,8 +1402,8 @@
 #undef SDL_CreateThreadRuntime
 #endif
 
-#ifdef SDL_CreateThreadWithStackSizeRuntime
-#undef SDL_CreateThreadWithStackSizeRuntime
+#ifdef SDL_CreateThreadWithPropertiesRuntime
+#undef SDL_CreateThreadWithPropertiesRuntime
 #endif
 
 /* dump the macro CreateThread versions, too. */
@@ -1411,8 +1411,8 @@
 #undef SDL_CreateThread
 #endif
 
-#ifdef SDL_CreateThreadWithStackSize
-#undef SDL_CreateThreadWithStackSize
+#ifdef SDL_CreateThreadWithProperties
+#undef SDL_CreateThreadWithProperties
 #endif
 
 #ifdef SDL_CreateWindow
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index c0bcdd5..74ab229 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -45,7 +45,7 @@ SDL3_SYM_VARARGS(void,LogCritical,(int a, SDL_PRINTF_FORMAT_STRING const char *b
 SDL3_SYM_VARARGS(void,LogMessage,(int a, SDL_LogPriority b, SDL_PRINTF_FORMAT_STRING const char *c, ...))
 SDL3_SYM_VARARGS(int,sscanf,(const char *a, SDL_SCANF_FORMAT_STRING const char *b, ...))
 SDL3_SYM_VARARGS(int,snprintf,(SDL_OUT_Z_CAP(b) char *a, size_t b, SDL_PRINTF_FORMAT_STRING const char *c, ...))
-SDL3_SYM(SDL_Thread*,CreateThreadWithStackSizeRuntime,(SDL_ThreadFunction a, const char *b, const size_t c, void *d, SDL_BeginThreadExCallback e, SDL_EndThreadExCallback f),(a,b,c,d,e,f),return)
+SDL3_SYM(SDL_Thread*,CreateThreadWithPropertiesRuntime,(SDL_PropertiesID a, SDL_FunctionPointer b, SDL_FunctionPointer c),(a,b,c),return)
 
 #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
 SDL3_SYM_PASSTHROUGH(int,RegisterApp,(const char *a, Uint32 b, void *c),(a,b,c),return)