Skip to content

Port more SDL functions (SDL3) #3515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
@@ -82,6 +82,7 @@
#define PG_PixelFormatEnum SDL_PixelFormat

#define PG_SurfaceHasRLE SDL_SurfaceHasRLE
#define PG_SetSurfaceRLE SDL_SetSurfaceRLE

#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \
SDL_StretchSurface(src, srcrect, dst, dstrect, SDL_SCALEMODE_NEAREST)
@@ -132,6 +133,8 @@ PG_GetSurfaceFormat(SDL_Surface *surf)

#define PG_GetSurfacePalette SDL_GetSurfacePalette
#define PG_SetPaletteColors SDL_SetPaletteColors
#define PG_SetSurfacePalette SDL_SetSurfacePalette
#define PG_SetSurfaceColorKey SDL_SetSurfaceColorKey
#define PG_SetSurfaceBlendMode SDL_SetSurfaceBlendMode
#define PG_GetSurfaceBlendMode SDL_GetSurfaceBlendMode
#define PG_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod
@@ -248,6 +251,18 @@ PG_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors,
return SDL_SetPaletteColors(palette, colors, firstcolor, ncolors) == 0;
}

static inline bool
PG_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
{
return SDL_SetSurfacePalette(surface, palette) == 0;
}

static inline bool
PG_SetSurfaceColorKey(SDL_Surface *surface, bool enabled, Uint32 key)
{
return SDL_SetColorKey(surface, enabled, key) == 0;
}

static inline bool
PG_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
{
@@ -343,6 +358,12 @@ PG_InitSubSystem(Uint32 flags)

#define PG_SurfaceHasRLE SDL_HasSurfaceRLE

static inline bool
PG_SetSurfaceRLE(SDL_Surface *surface, bool enabled)
{
return SDL_SetSurfaceRLE(surface, enabled) == 0;
}

static inline bool
PG_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect)
{
7 changes: 2 additions & 5 deletions src_c/image.c
Original file line number Diff line number Diff line change
@@ -1795,12 +1795,9 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
}

if (h.has_cmap) {
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_SetSurfacePalette(linebuf, surf_palette))
#else
if (SDL_SetSurfacePalette(linebuf, surf_palette) < 0)
#endif
if (!PG_SetSurfacePalette(linebuf, surf_palette)) {
goto error; /* SDL error already set. */
}
}

if (rle) {
10 changes: 4 additions & 6 deletions src_c/rotozoom.c
Original file line number Diff line number Diff line change
@@ -589,12 +589,11 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
PG_CreateSurface(dstwidth, dstheight, PG_SURF_FORMATENUM(rz_src));
if (SDL_HasColorKey(src)) {
SDL_GetColorKey(src, &colorkey);
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
if (!PG_SetSurfaceColorKey(rz_dst, SDL_TRUE, colorkey)) {
SDL_FreeSurface(rz_dst);
return NULL;
}
if (PG_SurfaceHasRLE(src) &&
SDL_SetSurfaceRLE(rz_dst, SDL_TRUE) != 0) {
if (PG_SurfaceHasRLE(src) && !PG_SetSurfaceRLE(rz_dst, SDL_TRUE)) {
SDL_FreeSurface(rz_dst);
return NULL;
}
@@ -649,12 +648,11 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
PG_CreateSurface(dstwidth, dstheight, PG_SURF_FORMATENUM(rz_src));
if (SDL_HasColorKey(src)) {
SDL_GetColorKey(src, &colorkey);
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
if (!PG_SetSurfaceColorKey(rz_dst, SDL_TRUE, colorkey)) {
SDL_FreeSurface(rz_dst);
return NULL;
}
if (PG_SurfaceHasRLE(src) &&
SDL_SetSurfaceRLE(rz_dst, SDL_TRUE) != 0) {
if (PG_SurfaceHasRLE(src) && !PG_SetSurfaceRLE(rz_dst, SDL_TRUE)) {
SDL_FreeSurface(rz_dst);
return NULL;
}
35 changes: 17 additions & 18 deletions src_c/surface.c
Original file line number Diff line number Diff line change
@@ -1417,7 +1417,6 @@ surf_set_colorkey(pgSurfaceObject *self, PyObject *args)
SDL_Surface *surf = pgSurface_AsSurface(self);
Uint32 flags = 0, color = 0;
PyObject *rgba_obj = NULL;
int result;
int hascolor = SDL_FALSE;

if (!PyArg_ParseTuple(args, "|Oi", &rgba_obj, &flags)) {
@@ -1436,22 +1435,22 @@ surf_set_colorkey(pgSurfaceObject *self, PyObject *args)
}

pgSurface_Prep(self);
result = 0;
bool success = true;
if (hascolor && PG_SURF_BytesPerPixel(surf) == 1) {
/* For an indexed surface, remove the previous colorkey first.
*/
result = SDL_SetColorKey(surf, SDL_FALSE, color);
success = PG_SetSurfaceColorKey(surf, SDL_FALSE, color);
}
if (result == 0 && hascolor) {
result = SDL_SetSurfaceRLE(
if (success && hascolor) {
success = PG_SetSurfaceRLE(
surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
}
if (result == 0) {
result = SDL_SetColorKey(surf, hascolor, color);
if (success) {
success = PG_SetSurfaceColorKey(surf, hascolor, color);
}
pgSurface_Unprep(self);

if (result == -1) {
if (!success) {
return RAISE(pgExc_SDLError, SDL_GetError());
}

@@ -1496,7 +1495,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
Uint32 flags = 0;
PyObject *alpha_obj = NULL, *intobj = NULL;
Uint8 alpha;
int result, alphaval = 255;
int alphaval = 255;
SDL_Rect sdlrect;
SDL_Surface *surface;

@@ -1544,8 +1543,8 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
}
}
pgSurface_Prep(self);
result =
SDL_SetSurfaceRLE(surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
bool success =
PG_SetSurfaceRLE(surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
/* HACK HACK HACK */
if ((surf->flags & SDL_RLEACCEL) && (!(flags & PGS_RLEACCEL))) {
/* hack to strip SDL_RLEACCEL flag off surface immediately when
@@ -1561,12 +1560,12 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
SDL_FreeSurface(surface);
}
/* HACK HACK HACK */
if (result == 0) {
result = !PG_SetSurfaceAlphaMod(surf, alpha);
if (success) {
success = PG_SetSurfaceAlphaMod(surf, alpha);
}
pgSurface_Unprep(self);

if (result != 0) {
if (!success) {
return RAISE(pgExc_SDLError, SDL_GetError());
}

@@ -1814,7 +1813,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)

if (has_colorkey) {
colorkey = SDL_MapSurfaceRGBA(newsurf, key_r, key_g, key_b, key_a);
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
if (!PG_SetSurfaceColorKey(newsurf, SDL_TRUE, colorkey)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
@@ -1977,7 +1976,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)

if (has_colorkey) {
colorkey = SDL_MapRGBA(newsurf->format, key_r, key_g, key_b, key_a);
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
if (!PG_SetSurfaceColorKey(newsurf, SDL_TRUE, colorkey)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
@@ -3237,7 +3236,7 @@ surf_subsurface(PyObject *self, PyObject *args)
SDL_FreeSurface(sub);
return NULL;
}
if (SDL_SetSurfacePalette(sub, pal) != 0) {
if (!PG_SetSurfacePalette(sub, pal)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreePalette(pal);
SDL_FreeSurface(sub);
@@ -3259,7 +3258,7 @@ surf_subsurface(PyObject *self, PyObject *args)
}
if (SDL_HasColorKey(surf)) {
SDL_GetColorKey(surf, &colorkey);
if (SDL_SetColorKey(sub, SDL_TRUE, colorkey) != 0) {
if (!PG_SetSurfaceColorKey(sub, SDL_TRUE, colorkey)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(sub);
return NULL;
5 changes: 2 additions & 3 deletions src_c/transform.c
Original file line number Diff line number Diff line change
@@ -197,13 +197,12 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)

if (SDL_HasColorKey(surf)) {
SDL_GetColorKey(surf, &colorkey);
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
if (!PG_SetSurfaceColorKey(newsurf, SDL_TRUE, colorkey)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
}
if (PG_SurfaceHasRLE(surf) &&
SDL_SetSurfaceRLE(newsurf, SDL_TRUE) != 0) {
if (PG_SurfaceHasRLE(surf) && !PG_SetSurfaceRLE(newsurf, SDL_TRUE)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
9 changes: 2 additions & 7 deletions src_c/window.c
Original file line number Diff line number Diff line change
@@ -1251,13 +1251,8 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
return -1;
}
if (icon_colorkey != -1) {
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_SetColorKey(pgSurface_AsSurface(icon), SDL_TRUE,
icon_colorkey)) {
#else
if (SDL_SetColorKey(pgSurface_AsSurface(icon), SDL_TRUE,
icon_colorkey) < 0) {
#endif
if (!PG_SetSurfaceColorKey(pgSurface_AsSurface(icon), SDL_TRUE,
icon_colorkey)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}