diff --git a/src_c/_pygame.h b/src_c/_pygame.h index c46211e888..e3381053ae 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) @@ -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) { 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/rotozoom.c b/src_c/rotozoom.c index f0db4c6866..ae146989ed 100644 --- a/src_c/rotozoom.c +++ b/src_c/rotozoom.c @@ -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; } diff --git a/src_c/surface.c b/src_c/surface.c index 35b0ef1786..408a96e08a 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,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; diff --git a/src_c/transform.c b/src_c/transform.c index 810c32865b..a13f040021 100644 --- a/src_c/transform.c +++ b/src_c/transform.c @@ -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; 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; }