sdl2-compat: render: SDL2 SDL_SetRenderTarget resets a bunch of view state for textures.

From fe43e143dd9c143bbbbc24a6daad6713f11af184 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 28 Mar 2025 21:49:34 -0400
Subject: [PATCH] render: SDL2 SDL_SetRenderTarget resets a bunch of view state
 for textures.

Reference Issue #425.
---
 src/sdl2_compat.c | 31 +++++++++++++++++++++++++++++++
 src/sdl3_syms.h   |  3 ++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 49947ad..4cc8e3a 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -5961,6 +5961,37 @@ SDL_RenderSetClipRect(SDL_Renderer *renderer, const SDL_Rect *rect)
     return retval < 0 ? retval : FlushRendererIfNotBatching(renderer);
 }
 
+SDL_DECLSPEC int SDLCALL
+SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
+{
+    if (!SDL3_SetRenderTarget(renderer, texture)) {
+        return -1;
+    }
+
+    /* SDL2 reset the viewport, scale, and logical presentation for textures.
+        If moving from the backbuffer to a texture, it makes a backup of that
+        state and restores it when moving back to the backbuffer...but SDL3
+        already tracks this state (its "view") separately, so we don't need
+        to reproduce the backup efforts here; the backbuffer view state isn't
+        lost. */
+    if (texture) {
+        SDL_Rect viewport;
+        float w = 0.0f;
+        float h = 0.0f;
+        SDL3_GetTextureSize(texture, &w, &h);
+        viewport.x = 0;
+        viewport.y = 0;
+        viewport.w = (int) w;
+        viewport.h = (int) h;
+        SDL3_SetRenderViewport(renderer, &viewport);
+        SDL3_SetRenderClipRect(renderer, NULL);
+        SDL3_SetRenderScale(renderer, 1.0f, 1.0f);
+        SDL3_SetRenderLogicalPresentation(renderer, w, h, SDL_LOGICAL_PRESENTATION_DISABLED);
+    }
+
+    return FlushRendererIfNotBatching(renderer);
+}
+
 SDL_DECLSPEC int SDLCALL
 SDL_RenderClear(SDL_Renderer *renderer)
 {
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index 507d2cd..25baa26 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -396,6 +396,7 @@ SDL3_SYM_PASSTHROUGH_RETCODE(bool,GetTextureBlendMode,(SDL_Texture *a, SDL_Blend
 SDL3_SYM_PASSTHROUGH_RETCODE(bool,GetTextureColorMod,(SDL_Texture *a, Uint8 *b, Uint8 *c, Uint8 *d),(a,b,c,d),return)
 SDL3_SYM(SDL_PropertiesID,GetTextureProperties,(SDL_Texture *a),(a),return)
 SDL3_SYM(bool,GetTextureScaleMode,(SDL_Texture *a, SDL_ScaleMode *b),(a,b),return)
+SDL3_SYM(bool,GetTextureSize,(SDL_Texture *a, float *b, float *c),(a,b),return)
 SDL3_SYM(Uint64,GetThreadID,(SDL_Thread *a),(a),return)
 SDL3_SYM_PASSTHROUGH(const char*,GetThreadName,(SDL_Thread *a),(a),return)
 SDL3_SYM(Uint64,GetTicks,(void),(),return)
@@ -642,7 +643,7 @@ SDL3_SYM(bool,SetRenderClipRect,(SDL_Renderer *a, const SDL_Rect *b),(a,b),retur
 SDL3_SYM_PASSTHROUGH_RETCODE(bool,SetRenderDrawBlendMode,(SDL_Renderer *a, SDL_BlendMode b),(a,b),return)
 SDL3_SYM_PASSTHROUGH_RETCODE(bool,SetRenderDrawColor,(SDL_Renderer *a, Uint8 b, Uint8 c, Uint8 d, Uint8 e),(a,b,c,d,e),return)
 SDL3_SYM(bool,SetRenderLogicalPresentation,(SDL_Renderer *a, int b, int c, SDL_RendererLogicalPresentation d),(a,b,c,d),return)
-SDL3_SYM_PASSTHROUGH_RETCODE(bool,SetRenderTarget,(SDL_Renderer *a, SDL_Texture *b),(a,b),return)
+SDL3_SYM(bool,SetRenderTarget,(SDL_Renderer *a, SDL_Texture *b),(a,b),return)
 SDL3_SYM(bool,SetRenderViewport,(SDL_Renderer *a, const SDL_Rect *b),(a,b),return)
 SDL3_SYM(bool,SetStringProperty,(SDL_PropertiesID a, const char *b, const char *c),(a,b,c),return)
 SDL3_SYM(bool,SetSurfaceAlphaMod,(SDL_Surface *a, Uint8 b),(a,b),return)