Skip to content

Commit

Permalink
Port last SDL_CreateRGBSurface calls to SDL3-safe PG_CreateSurface
Browse files Browse the repository at this point in the history
  • Loading branch information
Starbuck5 committed Oct 5, 2023
1 parent 29ecb72 commit 3054e54
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
24 changes: 7 additions & 17 deletions src_c/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
Uint8 surf_alpha;
int have_surf_colorkey = 0;
Uint32 surf_colorkey;
Uint32 rmask, gmask, bmask, amask;
Uint32 format;
SDL_Rect r;
int bpp;
Uint8 *rlebuf = NULL;
Expand All @@ -1535,7 +1535,7 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
h.cmap_bits = 24;
SETLE16(h.cmap_len, surface->format->palette->ncolors);
h.pixel_bits = 8;
rmask = gmask = bmask = amask = 0;
format = SDL_PIXELFORMAT_INDEX8;
}
else {
h.has_cmap = 0;
Expand All @@ -1545,21 +1545,12 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
if (surface->format->Amask) {
alpha = 1;
h.pixel_bits = 32;
format = SDL_PIXELFORMAT_BGRA32;
}
else
else {
h.pixel_bits = 24;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
int s = alpha ? 0 : 8;
amask = 0x000000ff >> s;
rmask = 0x0000ff00 >> s;
gmask = 0x00ff0000 >> s;
bmask = 0xff000000 >> s;
#else /* SDL_BYTEORDER != SDL_BIG_ENDIAN */
amask = alpha ? 0xff000000 : 0;
rmask = 0x00ff0000;
gmask = 0x0000ff00;
bmask = 0x000000ff;
#endif /* SDL_BYTEORDER != SDL_BIG_ENDIAN */
format = SDL_PIXELFORMAT_BGR24;
}
}
bpp = h.pixel_bits >> 3;
if (rle)
Expand Down Expand Up @@ -1588,8 +1579,7 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
}
}

linebuf = SDL_CreateRGBSurface(0, surface->w, 1, h.pixel_bits, rmask,
gmask, bmask, amask);
linebuf = PG_CreateSurface(surface->w, 1, format);
if (!linebuf)
return -1;

Expand Down
27 changes: 11 additions & 16 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ static void
_release_buffer(Py_buffer *view_p);
static PyObject *
_raise_get_view_ndim_error(int bitsize, SurfViewKind kind);
static PyObject *
_raise_create_surface_error(void);
static SDL_Surface *
pg_DisplayFormatAlpha(SDL_Surface *surface);
static SDL_Surface *
Expand Down Expand Up @@ -717,12 +715,19 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
}
}

surface = SDL_CreateRGBSurface(0, width, height, bpp, Rmask, Gmask, Bmask,
Amask);
Uint32 pxformat =
SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
if (pxformat == SDL_PIXELFORMAT_UNKNOWN) {
PyErr_SetString(PyExc_ValueError, "Invalid mask values");
return -1;
}

surface = PG_CreateSurface(width, height, pxformat);
if (!surface) {
_raise_create_surface_error();
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}

if (!(flags & PGS_SRCALPHA)) {
/* We ignore the error if any. */
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
Expand Down Expand Up @@ -762,16 +767,6 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
return 0;
}

static PyObject *
_raise_create_surface_error(void)
{
const char *msg = SDL_GetError();

if (strcmp(msg, "Unknown pixel format") == 0)
return RAISE(PyExc_ValueError, "Invalid mask values");
return RAISE(pgExc_SDLError, msg);
}

/* surface object methods */
static PyObject *
surf_get_at(PyObject *self, PyObject *position)
Expand Down Expand Up @@ -2680,7 +2675,7 @@ surf_subsurface(PyObject *self, PyObject *args)
pgSurface_Unlock((pgSurfaceObject *)self);

if (!sub)
return _raise_create_surface_error();
return RAISE(pgExc_SDLError, SDL_GetError());

/* copy the colormap if we need it */
if (SDL_ISPIXELFORMAT_INDEXED(surf->format->format) &&
Expand Down

0 comments on commit 3054e54

Please sign in to comment.