sdl2-compat: Update to use SDL_Renderer logical presentation

From 132b90275e540749bd1eed40fd22e0d17cef150f Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Sun, 5 Feb 2023 21:10:34 +0100
Subject: [PATCH] Update to use SDL_Renderer logical presentation

---
 src/sdl2_compat.c | 87 ++++++++++++++++++++++++++++++++++++++++++-----
 src/sdl3_syms.h   | 14 +++-----
 2 files changed, 83 insertions(+), 18 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 1c3a989..6d96db8 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -599,6 +599,8 @@ BOOL WINAPI _DllMainCRTStartup(HANDLE dllhandle, DWORD reason, LPVOID reserved)
 #define SDL2_DISABLE  0
 #define SDL2_ENABLE   1
 
+#define SDL2_RENDERER_TARGETTEXTURE 0x00000008
+
 /* this enum changed in SDL3. */
 typedef enum
 {
@@ -1583,7 +1585,7 @@ SDL_RenderWindowToLogical(SDL_Renderer *renderer,
                           int windowX, int windowY,
                           float *logicalX, float *logicalY)
 {
-    SDL3_RenderWindowToLogical(renderer, (float)windowX, (float)windowY, logicalX, logicalY);
+    SDL3_RenderCoordinatesFromWindow(renderer, (float)windowX, (float)windowY, logicalX, logicalY);
 }
 
 DECLSPEC void SDLCALL
@@ -1592,7 +1594,7 @@ SDL_RenderLogicalToWindow(SDL_Renderer *renderer,
                           int *windowX, int *windowY)
 {
     float x, y;
-    SDL3_RenderLogicalToWindow(renderer, logicalX, logicalY, &x, &y);
+    SDL3_RenderCoordinatesToWindow(renderer, logicalX, logicalY, &x, &y);
     if (windowX) *windowX = (int)x;
     if (windowY) *windowY = (int)y;
 }
@@ -2932,7 +2934,7 @@ SDL_GetRenderDriverInfo(int index, SDL_RendererInfo *info)
 
     /* these are the values that SDL2 returns. */
     if ((SDL3_strcmp(name, "opengl") == 0) || (SDL3_strcmp(name, "opengles2") == 0)) {
-        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
+        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL2_RENDERER_TARGETTEXTURE;
         info->num_texture_formats = 4;
         info->texture_formats[0] = SDL_PIXELFORMAT_ARGB8888;
         info->texture_formats[1] = SDL_PIXELFORMAT_ABGR8888;
@@ -2943,11 +2945,11 @@ SDL_GetRenderDriverInfo(int index, SDL_RendererInfo *info)
         info->num_texture_formats = 1;
         info->texture_formats[0] = SDL_PIXELFORMAT_ABGR8888;
     } else if (SDL3_strcmp(name, "direct3d") == 0) {
-        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
+        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL2_RENDERER_TARGETTEXTURE;
         info->num_texture_formats = 1;
         info->texture_formats[0] = SDL_PIXELFORMAT_ARGB8888;
     } else if (SDL3_strcmp(name, "direct3d11") == 0) {
-        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
+        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL2_RENDERER_TARGETTEXTURE;
         info->num_texture_formats = 6;
         info->texture_formats[0] = SDL_PIXELFORMAT_ARGB8888;
         info->texture_formats[1] = SDL_PIXELFORMAT_RGB888;
@@ -2956,7 +2958,7 @@ SDL_GetRenderDriverInfo(int index, SDL_RendererInfo *info)
         info->texture_formats[4] = SDL_PIXELFORMAT_NV12;
         info->texture_formats[5] = SDL_PIXELFORMAT_NV21;
     } else if (SDL3_strcmp(name, "direct3d12") == 0) {
-        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
+        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL2_RENDERER_TARGETTEXTURE;
         info->num_texture_formats = 6;
         info->texture_formats[0] = SDL_PIXELFORMAT_ARGB8888;
         info->texture_formats[1] = SDL_PIXELFORMAT_RGB888;
@@ -2967,7 +2969,7 @@ SDL_GetRenderDriverInfo(int index, SDL_RendererInfo *info)
         info->max_texture_width = 16384;
         info->max_texture_height = 16384;
     } else if (SDL3_strcmp(name, "metal") == 0) {
-        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
+        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL2_RENDERER_TARGETTEXTURE;
         info->num_texture_formats = 6;
         info->texture_formats[0] = SDL_PIXELFORMAT_ARGB8888;
         info->texture_formats[1] = SDL_PIXELFORMAT_ABGR8888;
@@ -2976,9 +2978,9 @@ SDL_GetRenderDriverInfo(int index, SDL_RendererInfo *info)
         info->texture_formats[4] = SDL_PIXELFORMAT_NV12;
         info->texture_formats[5] = SDL_PIXELFORMAT_NV21;
     } else if (SDL3_strcmp(name, "software") == 0) {
-        info->flags = SDL_RENDERER_SOFTWARE | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
+        info->flags = SDL_RENDERER_SOFTWARE | SDL_RENDERER_PRESENTVSYNC | SDL2_RENDERER_TARGETTEXTURE;
     } else {  /* this seems reasonable if something currently-unknown shows up in SDL3. */
-        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
+        info->flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL2_RENDERER_TARGETTEXTURE;
         info->num_texture_formats = 1;
         info->texture_formats[0] = SDL_PIXELFORMAT_ARGB8888;
     }
@@ -2999,6 +3001,73 @@ SDL_CreateRenderer(SDL_Window *window, int index, Uint32 flags)
     return SDL3_CreateRenderer(window, name, flags);
 }
 
+DECLSPEC SDL_bool SDLCALL 
+SDL_RenderTargetSupported(SDL_Renderer *renderer)
+{
+    int ret; 
+    SDL_RendererInfo info;
+    ret = SDL_GetRendererInfo(renderer, &info);
+    if (ret == 0) {
+        if (info.flags & SDL2_RENDERER_TARGETTEXTURE) {
+            return SDL_TRUE;
+        }
+    }
+    return SDL_FALSE;
+}
+
+DECLSPEC int SDLCALL 
+SDL_RenderSetLogicalSize(SDL_Renderer *renderer, int w, int h)
+{
+    if (w == 0 && h == 0) {
+        return SDL3_SetRenderLogicalPresentation(renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED, SDL_SCALEMODE_NEAREST);
+    } else {
+        return SDL3_SetRenderLogicalPresentation(renderer, w, h, SDL_LOGICAL_PRESENTATION_LETTERBOX, SDL_SCALEMODE_LINEAR);
+    }
+}
+
+DECLSPEC void SDLCALL SDL_RenderGetLogicalSize(SDL_Renderer *renderer, int *w, int *h)
+{
+    SDL3_GetRenderLogicalPresentation(renderer, w, h, NULL, NULL);
+}
+
+DECLSPEC int SDLCALL SDL_RenderSetIntegerScale(SDL_Renderer *renderer, SDL_bool enable)
+{
+    SDL_ScaleMode scale_mode;
+    SDL_RendererLogicalPresentation mode;
+    int w, h;
+    int ret;
+
+    ret = SDL3_GetRenderLogicalPresentation(renderer, &w, &h, &mode, &scale_mode);
+    if (ret < 0) {
+        return ret;
+    }
+    
+    if (enable && mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) {
+        return 0;
+    }
+
+    if (!enable && mode != SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) {
+        return 0;
+    }
+
+    if (enable) {
+        return SDL3_SetRenderLogicalPresentation(renderer, w, h, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE, scale_mode);
+    } else {
+        return SDL3_SetRenderLogicalPresentation(renderer, w, h, SDL_LOGICAL_PRESENTATION_DISABLED, scale_mode);
+    }
+}
+
+DECLSPEC SDL_bool SDLCALL SDL_RenderGetIntegerScale(SDL_Renderer *renderer)
+{
+    SDL_RendererLogicalPresentation mode;
+    if (SDL3_GetRenderLogicalPresentation(renderer, NULL, NULL, &mode, NULL) == 0) {
+        if (mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) {
+            return SDL_TRUE;
+        }
+    }
+    return SDL_FALSE;
+}
+
 DECLSPEC int SDLCALL
 SDL_AudioInit(const char *driver_name)
 {
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index e4c32a2..71b0d09 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -299,7 +299,7 @@ SDL3_SYM(SDL_Renderer*,CreateRenderer,(SDL_Window *a, const char *b, Uint32 c),(
 SDL3_SYM_PASSTHROUGH(SDL_Renderer*,CreateSoftwareRenderer,(SDL_Surface *a),(a),return)
 SDL3_SYM_PASSTHROUGH(SDL_Renderer*,GetRenderer,(SDL_Window *a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,GetRendererInfo,(SDL_Renderer *a, SDL_RendererInfo *b),(a,b),return)
-SDL3_SYM_PASSTHROUGH(int,GetRendererOutputSize,(SDL_Renderer *a, int *b, int *c),(a,b,c),return)
+SDL3_SYM_RENAMED(int,GetRendererOutputSize,GetRenderOutputSize,(SDL_Renderer *a, int *b, int *c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(SDL_Texture*,CreateTexture,(SDL_Renderer *a, Uint32 b, int c, int d, int e),(a,b,c,d,e),return)
 SDL3_SYM_PASSTHROUGH(SDL_Texture*,CreateTextureFromSurface,(SDL_Renderer *a, SDL_Surface *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(int,QueryTexture,(SDL_Texture *a, Uint32 *b, int *c, int *d, int *e),(a,b,c,d,e),return)
@@ -313,11 +313,8 @@ SDL3_SYM_PASSTHROUGH(int,UpdateTexture,(SDL_Texture *a, const SDL_Rect *b, const
 SDL3_SYM_PASSTHROUGH(int,UpdateYUVTexture,(SDL_Texture *a, const SDL_Rect *b, const Uint8 *c, int d, const Uint8 *e, int f, const Uint8 *g, int h),(a,b,c,d,e,f,g,h),return)
 SDL3_SYM_PASSTHROUGH(int,LockTexture,(SDL_Texture *a, const SDL_Rect *b, void **c, int *d),(a,b,c,d),return)
 SDL3_SYM_PASSTHROUGH(void,UnlockTexture,(SDL_Texture *a),(a),)
-SDL3_SYM_PASSTHROUGH(SDL_bool,RenderTargetSupported,(SDL_Renderer *a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,SetRenderTarget,(SDL_Renderer *a, SDL_Texture *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(SDL_Texture*,GetRenderTarget,(SDL_Renderer *a),(a),return)
-SDL3_SYM_RENAMED(int,RenderSetLogicalSize,SetRenderLogicalSize,(SDL_Renderer *a, int b, int c),(a,b,c),return)
-SDL3_SYM_RENAMED(void,RenderGetLogicalSize,GetRenderLogicalSize,(SDL_Renderer *a, int *b, int *c),(a,b,c),)
 SDL3_SYM_RENAMED(int,RenderSetViewport,SetRenderViewport,(SDL_Renderer *a, const SDL_Rect *b),(a,b),return)
 SDL3_SYM_RENAMED(void,RenderGetViewport,GetRenderViewport,(SDL_Renderer *a, SDL_Rect *b),(a,b),)
 SDL3_SYM_RENAMED(int,RenderSetClipRect,SetRenderClipRect,(SDL_Renderer *a, const SDL_Rect *b),(a,b),return)
@@ -574,8 +571,6 @@ SDL3_SYM_PASSTHROUGH(int,SetWindowOpacity,(SDL_Window *a, float b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(int,GetWindowOpacity,(SDL_Window *a, float *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowInputFocus,(SDL_Window *a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowModalFor,(SDL_Window *a, SDL_Window *b),(a,b),return)
-SDL3_SYM_RENAMED(int,RenderSetIntegerScale,SetRenderIntegerScale,(SDL_Renderer *a, SDL_bool b),(a,b),return)
-SDL3_SYM_RENAMED(SDL_bool,RenderGetIntegerScale,GetRenderIntegerScale,(SDL_Renderer *a),(a),return)
 SDL3_SYM(Uint32,DequeueAudio,(SDL_AudioDeviceID a, void *b, Uint32 c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(void,SetWindowResizable,(SDL_Window *a, SDL_bool b),(a,b),)
 SDL3_SYM(SDL_bool,GetHintBoolean,(const char *a, SDL_bool b),(a,b),return)
@@ -824,8 +819,8 @@ SDL3_SYM_PASSTHROUGH(int,hid_get_serial_number_string,(SDL_hid_device *a, wchar_
 SDL3_SYM_PASSTHROUGH(int,hid_get_indexed_string,(SDL_hid_device *a, int b, wchar_t *c, size_t d),(a,b,c,d),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowMouseRect,(SDL_Window *a, const SDL_Rect *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(const SDL_Rect*,GetWindowMouseRect,(SDL_Window *a),(a),return)
-SDL3_SYM(void,RenderWindowToLogical,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),)
-SDL3_SYM(void,RenderLogicalToWindow,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),)
+SDL3_SYM(void,RenderCoordinatesFromWindow,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),)
+SDL3_SYM(void,RenderCoordinatesToWindow,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),)
 SDL3_SYM_PASSTHROUGH(SDL_bool,JoystickHasRumble,(SDL_Joystick *a),(a),return)
 SDL3_SYM_PASSTHROUGH(SDL_bool,JoystickHasRumbleTriggers,(SDL_Joystick *a),(a),return)
 SDL3_SYM_RENAMED(SDL_bool,GameControllerHasRumble,GamepadHasRumble,(SDL_GameController *a),(a),return)
@@ -915,7 +910,8 @@ SDL3_SYM(SDL_DisplayID,GetPrimaryDisplay,(),(),return)
 SDL3_SYM(int,RunApp,(int a, char **b, SDL_main_func c, void *d),(a,b,c,d),return)
 SDL3_SYM(const SDL_DisplayMode *,GetWindowFullscreenMode,(SDL_Window *a),(a),return)
 SDL3_SYM(const SDL_DisplayMode **,GetFullscreenDisplayModes,(SDL_DisplayID a, int *b),(a,b),return)
-
+SDL3_SYM(int,GetRenderLogicalPresentation,(SDL_Renderer *a, int *b, int *c, SDL_RendererLogicalPresentation *d, SDL_ScaleMode *e),(a,b,c,d,e),return)
+SDL3_SYM(int,SetRenderLogicalPresentation,(SDL_Renderer *a, int b, int c, SDL_RendererLogicalPresentation d, SDL_ScaleMode e),(a,b,c,d,e),return)
 #undef SDL3_SYM
 #undef SDL3_SYM_PASSTHROUGH
 #undef SDL3_SYM_RENAMED