From c6d08771b20f844f81d50513baa192bf35c905c4 Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sat, 21 Jun 2025 13:59:17 -0700 Subject: [PATCH 1/3] Port SDL_SetColorKey --- src_c/_pygame.h | 7 +++++++ src_c/rotozoom.c | 4 ++-- src_c/surface.c | 24 ++++++++++++------------ src_c/transform.c | 2 +- src_c/window.c | 9 ++------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src_c/_pygame.h b/src_c/_pygame.h index c46211e888..62a1460a59 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -132,6 +132,7 @@ PG_GetSurfaceFormat(SDL_Surface *surf) #define PG_GetSurfacePalette SDL_GetSurfacePalette #define PG_SetPaletteColors SDL_SetPaletteColors +#define PG_SetSurfaceColorKey SDL_SetSurfaceColorKey #define PG_SetSurfaceBlendMode SDL_SetSurfaceBlendMode #define PG_GetSurfaceBlendMode SDL_GetSurfaceBlendMode #define PG_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod @@ -248,6 +249,12 @@ PG_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors, return SDL_SetPaletteColors(palette, colors, firstcolor, ncolors) == 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) { diff --git a/src_c/rotozoom.c b/src_c/rotozoom.c index f0db4c6866..1dc7b9163e 100644 --- a/src_c/rotozoom.c +++ b/src_c/rotozoom.c @@ -589,7 +589,7 @@ 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; } @@ -649,7 +649,7 @@ 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; } diff --git a/src_c/surface.c b/src_c/surface.c index 35b0ef1786..0cf4a3d3c1 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -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,23 @@ 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( - surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE); + if (success && hascolor) { + success = + SDL_SetSurfaceRLE( + surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE) == 0; } - 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()); } @@ -1814,7 +1814,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 +1977,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; @@ -3259,7 +3259,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; diff --git a/src_c/transform.c b/src_c/transform.c index 810c32865b..1bdea76ca1 100644 --- a/src_c/transform.c +++ b/src_c/transform.c @@ -197,7 +197,7 @@ 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; diff --git a/src_c/window.c b/src_c/window.c index 5c0b88e1e8..594c0321d2 100644 --- a/src_c/window.c +++ b/src_c/window.c @@ -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; } From 872c3dcb5b87e56db4173f2f909aa98b357b5e42 Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sat, 21 Jun 2025 14:06:40 -0700 Subject: [PATCH 2/3] Port SDL_SetSurfaceRLE --- src_c/_pygame.h | 7 +++++++ src_c/rotozoom.c | 6 ++---- src_c/surface.c | 17 ++++++++--------- src_c/transform.c | 3 +-- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src_c/_pygame.h b/src_c/_pygame.h index 62a1460a59..3b8f3a15c8 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -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) @@ -350,6 +351,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) { diff --git a/src_c/rotozoom.c b/src_c/rotozoom.c index 1dc7b9163e..ae146989ed 100644 --- a/src_c/rotozoom.c +++ b/src_c/rotozoom.c @@ -593,8 +593,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth) 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; } @@ -653,8 +652,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth) 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; } diff --git a/src_c/surface.c b/src_c/surface.c index 0cf4a3d3c1..44b2e5d78d 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -1442,9 +1442,8 @@ surf_set_colorkey(pgSurfaceObject *self, PyObject *args) success = PG_SetSurfaceColorKey(surf, SDL_FALSE, color); } if (success && hascolor) { - success = - SDL_SetSurfaceRLE( - surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE) == 0; + success = PG_SetSurfaceRLE( + surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE); } if (success) { success = PG_SetSurfaceColorKey(surf, hascolor, color); @@ -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()); } diff --git a/src_c/transform.c b/src_c/transform.c index 1bdea76ca1..a13f040021 100644 --- a/src_c/transform.c +++ b/src_c/transform.c @@ -202,8 +202,7 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height) 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; From ef726d9d3d89777e960d049fac7a16d1581b8e0d Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sat, 21 Jun 2025 15:03:11 -0700 Subject: [PATCH 3/3] Port SDL_SetSurfacePalette --- src_c/_pygame.h | 7 +++++++ src_c/image.c | 7 ++----- src_c/surface.c | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src_c/_pygame.h b/src_c/_pygame.h index 3b8f3a15c8..e3381053ae 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -133,6 +133,7 @@ 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 @@ -250,6 +251,12 @@ 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) { diff --git a/src_c/image.c b/src_c/image.c index e4f9cf70f9..1de25f8dda 100644 --- a/src_c/image.c +++ b/src_c/image.c @@ -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) { diff --git a/src_c/surface.c b/src_c/surface.c index 44b2e5d78d..408a96e08a 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -3236,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);