sdl2-compat: Updated for SDL3 SDL_RenderReadPixels() change

From 635c8e5fdb92a64b2a9d6d54f6bc4f1695323fa0 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 3 Feb 2024 12:25:48 -0800
Subject: [PATCH] Updated for SDL3 SDL_RenderReadPixels() change

---
 src/sdl2_compat.c | 21 +++++++++++++++++++++
 src/sdl3_syms.h   |  3 ++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 8b7d68e..d162496 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -4357,6 +4357,27 @@ SDL_RenderGeometryRaw(SDL_Renderer *renderer, SDL_Texture *texture, const float
     return retval < 0 ? retval : FlushRendererIfNotBatching(renderer);
 }
 
+DECLSPEC int SDLCALL
+SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 format, void *pixels, int pitch)
+{
+    SDL_Colorspace surface_colorspace = SDL_COLORSPACE_UNKNOWN;
+    int result = -1;
+
+    SDL_Surface *surface = SDL3_RenderReadPixels(renderer, rect);
+    if (!surface) {
+        return -1;
+    }
+
+    if (SDL3_GetSurfaceColorspace(surface, &surface_colorspace) == 0 &&
+        SDL3_ConvertPixelsAndColorspace(surface->w, surface->h, surface->format->format, surface_colorspace,  surface->pixels, surface->pitch, format, SDL_COLORSPACE_SRGB, pixels, pitch) == 0) {
+        result = 0;
+    }
+
+    SDL3_DestroySurface(surface);
+
+    return result;
+}
+
 DECLSPEC void SDLCALL
 SDL_RenderPresent(SDL_Renderer *renderer)
 {
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index fc0fa87..d5414a4 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -338,7 +338,7 @@ SDL3_SYM_PASSTHROUGH(int,GetRenderDrawColor,(SDL_Renderer *a, Uint8 *b, Uint8 *c
 SDL3_SYM_PASSTHROUGH(int,SetRenderDrawBlendMode,(SDL_Renderer *a, SDL_BlendMode b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(int,GetRenderDrawBlendMode,(SDL_Renderer *a, SDL_BlendMode *b),(a,b),return)
 SDL3_SYM(int,RenderClear,(SDL_Renderer *a),(a),return)
-SDL3_SYM_PASSTHROUGH(int,RenderReadPixels,(SDL_Renderer *a, const SDL_Rect *b, Uint32 c, void *d, int e),(a,b,c,d,e),return)
+SDL3_SYM(SDL_Surface *,RenderReadPixels,(SDL_Renderer *a, const SDL_Rect *b),(a,b),return)
 SDL3_SYM(int,RenderPresent,(SDL_Renderer *a),(a),return)
 SDL3_SYM(void,DestroyTexture,(SDL_Texture *a),(a),)
 SDL3_SYM(void,DestroyRenderer,(SDL_Renderer *a),(a),)
@@ -859,6 +859,7 @@ SDL3_SYM(SDL_Haptic *,GetHapticFromInstanceID,(SDL_HapticID a),(a),return)
 SDL3_SYM(SDL_HapticID,GetHapticInstanceID,(SDL_Haptic *a),(a),return)
 SDL3_SYM(SDL_PropertiesID,GetJoystickProperties,(SDL_Joystick *a),(a),return)
 SDL3_SYM(SDL_PropertiesID,GetGamepadProperties,(SDL_GameController *a),(a),return)
+SDL3_SYM(int,GetSurfaceColorspace,(SDL_Surface *a, SDL_Colorspace *b),(a,b),return)
 
 #undef SDL3_SYM
 #undef SDL3_SYM_PASSTHROUGH