# sdl12-compat: Factor out enough of SDL\_CreateRGBSurface to create surfaces in-place

**URL:** <https://discourse.libsdl.org/t/sdl12-compat-factor-out-enough-of-sdl-creatergbsurface-to-create-surfaces-in-place/45731>\
**Category:** SDL Commits\
**Created:** [August 26, 2023, 3:08pm UTC](https://discourse.libsdl.org/t/sdl12-compat-factor-out-enough-of-sdl-creatergbsurface-to-create-surfaces-in-place/45731 "2023-08-26T15:08:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [August 26, 2023, 3:08pm UTC](https://discourse.libsdl.org/t/sdl12-compat-factor-out-enough-of-sdl-creatergbsurface-to-create-surfaces-in-place/45731/1 "2023-08-26T15:08:14Z")

</div>

> <https://github.com/libsdl-org/sdl12-compat/commit/13efe605cc426752dc952522a735337078e03c6c>
>
> The part before we create the SDL 1.2 surface (creating the SDL 2.0
> surface) bec…omes CreateRGBSurface(), and the part after becomes
> Surface12SetMasks().
> 
> Signed-off-by: Simon McVittie \<smcv@debian.org\>

```diff
From 13efe605cc426752dc952522a735337078e03c6c Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Thu, 20 Jul 2023 14:04:11 +0100
Subject: [PATCH] Factor out enough of SDL_CreateRGBSurface to create surfaces
 in-place

The part before we create the SDL 1.2 surface (creating the SDL 2.0
surface) becomes CreateRGBSurface(), and the part after becomes
Surface12SetMasks().

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

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index d06225124..6b58078d4 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -5131,11 +5131,10 @@ SetPalette12ForMasks(SDL12_Surface *surface12, const Uint32 Rmask, const Uint32
     }
 }
 
-DECLSPEC12 SDL12_Surface * SDLCALL
-SDL_CreateRGBSurface(Uint32 flags12, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
+static SDL_Surface *
+CreateRGBSurface(Uint32 flags12, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
 {
     SDL_Surface *surface20;
- SDL12_Surface *surface12;
 
     /* SDL 1.2 checks this. */
     if ((width >= 16384) || (height >= 65536)) {
@@ -5181,21 +5180,38 @@ SDL_CreateRGBSurface(Uint32 flags12, int width, int height, int depth, Uint32 Rm
         surface20 = SDL20_CreateRGBSurface(0, width, height, depth, Rmask, Gmask, Bmask, Amask);
     }
 
- surface12 = Surface20to12(surface20);
- if (!surface12) {
- SDL20_FreeSurface(surface20);
- return NULL;
- }
-
- SDL_assert((surface12->flags & ~(SDL12_SRCCOLORKEY|SDL12_SRCALPHA)) == 0); /* shouldn't have prealloc, rleaccel, or dontfree. */
+ return surface20;
+}
 
+static void
+Surface12SetMasks(SDL12_Surface *surface12, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
+{
     SetPalette12ForMasks(surface12, Rmask, Gmask, Bmask);
 
     if (Amask != 0) {
         surface12->flags |= SDL12_SRCALPHA;
- SDL20_SetSurfaceBlendMode(surface20, SDL_BLENDMODE_BLEND);
+ SDL20_SetSurfaceBlendMode(surface12->surface20, SDL_BLENDMODE_BLEND);
     }
+}
 
+DECLSPEC12 SDL12_Surface * SDLCALL
+SDL_CreateRGBSurface(Uint32 flags12, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
+{
+ SDL12_Surface *surface12;
+ SDL_Surface *surface20;
+
+ surface20 = CreateRGBSurface(flags12, width, height, depth, Rmask, Gmask, Bmask, Amask);
+ if (!surface20) {
+ return NULL;
+ }
+ surface12 = Surface20to12(surface20);
+ if (!surface12) {
+ SDL20_FreeSurface(surface20);
+ return NULL;
+ }
+
+ SDL_assert((surface12->flags & ~(SDL12_SRCCOLORKEY|SDL12_SRCALPHA)) == 0); /* shouldn't have prealloc, rleaccel, or dontfree. */
+ Surface12SetMasks(surface12, Rmask, Gmask, Bmask, Amask);
     return surface12;
 }
 
@@ -5224,6 +5240,8 @@ SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pit
 
     SDL_assert((surface12->flags & ~(SDL12_SRCCOLORKEY|SDL12_SRCALPHA)) == SDL12_PREALLOC); /* should _only_ have prealloc. */
 
+ /* TODO: Is it correct that this always ignored Amask, or should it be
+ * using Surface12SetMasks which takes Amask into account? */
     SetPalette12ForMasks(surface12, Rmask, Gmask, Bmask);
 
     return surface12;

```
