Skip to content

Commit

Permalink
Factor out enough of SDL_CreateRGBSurface to create surfaces in-place
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
smcv committed Aug 26, 2023
1 parent dcde64b commit b5703c2
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/SDL12_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b5703c2

Please sign in to comment.