sdl2-compat: render: Avoid unnecessary flushing in SDL_RenderDrawRects()

From 3d1d0e317c540870a59e876cad9814acc7db1725 Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Thu, 20 Feb 2025 17:35:15 -0600
Subject: [PATCH] render: Avoid unnecessary flushing in SDL_RenderDrawRects()

---
 src/sdl2_compat.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 8371004..f978b4e 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -5334,7 +5334,10 @@ SDL_RenderDrawRect(SDL_Renderer *renderer, const SDL_Rect *rect)
 SDL_DECLSPEC int SDLCALL
 SDL_RenderDrawRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count)
 {
+    SDL_FRect *frects;
     int i;
+    int retval;
+    int isstack;
 
     if (rects == NULL) {
         SDL3_InvalidParamError("rects");
@@ -5344,12 +5347,23 @@ SDL_RenderDrawRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count)
         return 0;
     }
 
+    frects = SDL3_small_alloc(SDL_FRect, count, &isstack);
+    if (frects == NULL) {
+        return -1;
+    }
+
     for (i = 0; i < count; ++i) {
-        if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
-            return -1;
-        }
+        frects[i].x = (float)rects[i].x;
+        frects[i].y = (float)rects[i].y;
+        frects[i].w = (float)rects[i].w;
+        frects[i].h = (float)rects[i].h;
     }
-    return 0;
+
+    retval = SDL3_RenderRects(renderer, frects, count) ? 0 : -1;
+
+    SDL3_small_free(frects, isstack);
+
+    return retval < 0 ? retval : FlushRendererIfNotBatching(renderer);
 }
 
 SDL_DECLSPEC int SDLCALL