sdl2-compat: Fixed SDL_RenderReadPixels when format argument is 0 (#407)

From 931258e386ea88c5088437a673eedb609d2f0494 Mon Sep 17 00:00:00 2001
From: The Hypnotron <[EMAIL REDACTED]>
Date: Wed, 12 Mar 2025 14:44:47 +0000
Subject: [PATCH] Fixed SDL_RenderReadPixels when format argument is 0 (#407)

---
 src/sdl2_compat.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index f381b48..0620ecd 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -6133,11 +6133,21 @@ SDL_DECLSPEC int SDLCALL
 SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 format, void *pixels, int pitch)
 {
     int result = -1;
+    SDL_Texture* target;
 
     SDL_Surface *surface = SDL3_RenderReadPixels(renderer, rect);
     if (!surface) {
         return -1;
     }
+   
+    if (!format) {
+        target = SDL3_GetRenderTarget(renderer);
+        if (target) {
+            format = SDL3_GetNumberProperty(SDL3_GetTextureProperties(target), SDL_PROP_TEXTURE_FORMAT_NUMBER, 0);
+        } else {
+            format = SDL3_GetWindowPixelFormat((SDL_Window *)SDL3_GetPointerProperty(SDL3_GetRendererProperties(renderer), SDL_PROP_RENDERER_WINDOW_POINTER, NULL));
+        }
+    }
 
     if (SDL3_ConvertPixelsAndColorspace(surface->w, surface->h, surface->format, SDL3_GetSurfaceColorspace(surface), SDL3_GetSurfaceProperties(surface), surface->pixels, surface->pitch, (SDL_PixelFormat)format, SDL_COLORSPACE_SRGB, 0, pixels, pitch)) {
         result = 0;