sdl12-compat: Factor out Surface20to12InPlace

From 119974a6e85a456066aeb7522926b92ff029580b Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Thu, 20 Jul 2023 13:58:17 +0100
Subject: [PATCH] Factor out Surface20to12InPlace

This is a step towards making it possible to use the same SDL12_Surface
for the video surface at all times, and only reallocating its underlying
SDL 2 SDL_Surface.

Signed-off-by: Simon McVittie <smcv@debian.org>
---
 src/SDL12_compat.c | 40 ++++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 9b5ba459b..531fd73a1 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -4937,26 +4937,21 @@ Rect12to20(const SDL12_Rect *rect12, SDL_Rect *rect20)
     return rect20;
 }
 
-static SDL12_Surface *
-Surface20to12(SDL_Surface *surface20)
+static SDL_bool
+Surface20to12InPlace(SDL_Surface *surface20,
+                     SDL12_Surface *surface12)
 {
     SDL_BlendMode blendmode = SDL_BLENDMODE_NONE;
-    SDL12_Surface *surface12 = NULL;
     SDL12_Palette *palette12 = NULL;
     SDL12_PixelFormat *format12 = NULL;
     Uint32 flags = 0;
 
     if (!surface20) {
-        return NULL;
+        return SDL_FALSE;
     }
     if (surface20->pitch > 65535) {
         SDL20_SetError("Pitch is too large");  /* can't fit to 16-bits */
-        return NULL;
-    }
-
-    surface12 = (SDL12_Surface *) SDL20_malloc(sizeof (SDL12_Surface));
-    if (!surface12) {
-        goto failed;
+        return SDL_FALSE;
     }
 
     if (surface20->format->palette) {
@@ -5029,12 +5024,33 @@ Surface20to12(SDL_Surface *surface20)
     Rect20to12(&surface20->clip_rect, &surface12->clip_rect);
     surface12->refcount = surface20->refcount;
 
-    return surface12;
+    return SDL_TRUE;
 
 failed:
-    SDL20_free(surface12);
     SDL20_free(palette12);
     SDL20_free(format12);
+    return SDL_FALSE;
+}
+
+static SDL12_Surface *
+Surface20to12(SDL_Surface *surface20)
+{
+    SDL12_Surface *surface12 = NULL;
+
+    surface12 = (SDL12_Surface *) SDL20_malloc(sizeof (SDL12_Surface));
+    if (!surface12) {
+        goto failed;
+    }
+
+    SDL20_zerop(surface12);
+    if (!Surface20to12InPlace(surface20, surface12)) {
+        goto failed;
+    }
+
+    return surface12;
+
+failed:
+    SDL20_free(surface12);
     return NULL;
 }