sdl2-compat: Use the window properties to set the initial window position at creation time

From 8716775444bdbb23132cf3ae24342ed7f46cd408 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Tue, 12 Dec 2023 15:11:25 -0500
Subject: [PATCH] Use the window properties to set the initial window position
 at creation time

Additionally, don't initially force the window to be created in the hidden state if the shown flag is specified.
---
 src/sdl2_compat.c | 63 +++++++++++++++++++++++++++++------------------
 src/sdl3_syms.h   |  2 ++
 2 files changed, 41 insertions(+), 24 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 535778e..703e3d0 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -5917,15 +5917,36 @@ SDL_GetTextureUserData(SDL_Texture * texture)
     return SDL3_GetProperty(SDL3_GetTextureProperties(texture), "userdata", NULL);
 }
 
+static void
+WindowPos2To3(int *x, int *y)
+{
+    /* Convert display indices to display IDs */
+    if (SDL_WINDOWPOS_ISUNDEFINED(*x) || SDL_WINDOWPOS_ISCENTERED(*x)) {
+      const int displayIndex = *x & 0xFFFF;
+      const SDL_DisplayID displayID = Display_IndexToID(displayIndex);
+
+      *x = (*x & 0xFFFF0000) | (0xFFFF & displayID);
+    }
+    if (SDL_WINDOWPOS_ISUNDEFINED(*y) || SDL_WINDOWPOS_ISCENTERED(*y)) {
+      const int displayIndex = *y & 0xFFFF;
+      const SDL_DisplayID displayID = Display_IndexToID(displayIndex);
+
+      *y = (*y & 0xFFFF0000) | (0xFFFF & displayID);
+    }
+}
+
 DECLSPEC SDL_Window * SDLCALL
 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
 {
     SDL_Window *window = NULL;
-    const SDL_bool hidden = (flags & SDL_WINDOW_HIDDEN) != 0;
     const Uint32 is_popup = flags & (SDL_WINDOW_POPUP_MENU | SDL_WINDOW_TOOLTIP);
 
-    flags &= ~SDL2_WINDOW_SHOWN;
-    flags |= SDL_WINDOW_HIDDEN;
+    /* SDL3 windows are shown by default */
+    if (flags & SDL2_WINDOW_SHOWN) {
+        flags &= ~(SDL2_WINDOW_SHOWN | SDL_WINDOW_HIDDEN);
+    } else {
+        flags |= SDL_WINDOW_HIDDEN;
+    }
     if (flags & SDL2_WINDOW_FULLSCREEN_DESKTOP) {
         flags &= ~SDL2_WINDOW_FULLSCREEN_DESKTOP;
         flags |= SDL_WINDOW_FULLSCREEN; /* This is fullscreen desktop for new windows */
@@ -5936,7 +5957,20 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
     }
 
     if (!is_popup) {
-        window = SDL3_CreateWindow(title, w, h, flags);
+        SDL_PropertiesID props = SDL3_CreateProperties();
+
+        WindowPos2To3(&x, &y);
+        if (title && *title) {
+            SDL3_SetStringProperty(props, "title", title);
+        }
+        SDL3_SetNumberProperty(props, "x", x);
+        SDL3_SetNumberProperty(props, "y", y);
+        SDL3_SetNumberProperty(props, "width", w);
+        SDL3_SetNumberProperty(props, "height", h);
+        SDL3_SetNumberProperty(props, "flags", flags);
+
+        window = SDL3_CreateWindowWithProperties(props);
+        SDL3_DestroyProperties(props);
     } else {
         SDL_Window *parent = SDL3_GetMouseFocus();
         if (!parent) {
@@ -5948,14 +5982,6 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
             SDL_SetWindowData(window, POPUP_PARENT_PROP_STR, parent);
         }
     }
-    if (window) {
-        if (!SDL_WINDOWPOS_ISUNDEFINED(x) || !SDL_WINDOWPOS_ISUNDEFINED(y)) {
-            SDL_SetWindowPosition(window, x, y);
-        }
-        if (!hidden) {
-            SDL3_ShowWindow(window);
-        }
-    }
     return window;
 }
 
@@ -6125,18 +6151,7 @@ SDL_SetWindowPosition(SDL_Window *window, int x, int y)
             parent = (SDL_Window *) SDL_GetWindowData(parent, POPUP_PARENT_PROP_STR);
         }
     } else {
-        if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(x)) {
-            const int displayIndex = x & 0xFFFF;
-            const SDL_DisplayID displayID = Display_IndexToID(displayIndex);
-
-            x = (x & 0xFFFF0000) | (0xFFFF & displayID);
-        }
-        if (SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) {
-            const int displayIndex = y & 0xFFFF;
-            const SDL_DisplayID displayID = Display_IndexToID(displayIndex);
-
-            y = (y & 0xFFFF0000) | (0xFFFF & displayID);
-        }
+        WindowPos2To3(&x, &y);
     }
 
     SDL3_SetWindowPosition(window, x, y);
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index 01e146b..943f6f5 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -861,6 +861,8 @@ SDL3_SYM(Sint64,GetNumberProperty,(SDL_PropertiesID a, const char *b, Sint64 c),
 SDL3_SYM(SDL_bool,GetBooleanProperty,(SDL_PropertiesID a, const char *b, SDL_bool c),(a,b,c),return)
 SDL3_SYM(SDL_PropertiesID,CreateProperties,(void),(),return)
 SDL3_SYM(int,SetBooleanProperty,(SDL_PropertiesID a, const char *b, SDL_bool c),(a,b,c),return)
+SDL3_SYM(int,SetNumberProperty,(SDL_PropertiesID a, const char *b, Sint64 c),(a,b,c),return)
+SDL3_SYM(int,SetStringProperty,(SDL_PropertiesID a, const char *b, const char *c),(a,b,c),return)
 SDL3_SYM(void,DestroyProperties,(SDL_PropertiesID a),(a),)
 SDL3_SYM(SDL_Window*,CreateWindowWithProperties,(SDL_PropertiesID a),(a),return)
 SDL3_SYM(char **,GetGamepadMappings,(int *a),(a),return)