Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
}

#define PG_GetSurfacePalette SDL_GetSurfacePalette
#define PG_SetPaletteColors SDL_SetPaletteColors
#define PG_SetSurfaceBlendMode SDL_SetSurfaceBlendMode
#define PG_GetSurfaceBlendMode SDL_GetSurfaceBlendMode
#define PG_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod
#define PG_SetSurfaceAlphaMod SDL_SetSurfaceAlphaMod

#define PG_GetRGBA SDL_GetRGBA
#define PG_GetRGB SDL_GetRGB
Expand Down Expand Up @@ -233,6 +238,37 @@ PG_GetSurfacePalette(SDL_Surface *surf)
return surf->format->palette;
}

static inline bool
PG_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors,
int firstcolor, int ncolors)
{
return SDL_SetPaletteColors(palette, colors, firstcolor, ncolors) == 0;
}

static inline bool
PG_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
{
return SDL_SetSurfaceBlendMode(surface, blendMode) == 0;
}

static inline bool
PG_GetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode *blendMode)
{
return SDL_GetSurfaceBlendMode(surface, blendMode) == 0;
}

static inline bool
PG_GetSurfaceAlphaMod(SDL_Surface *surface, Uint8 *alpha)
{
return SDL_GetSurfaceAlphaMod(surface, alpha) == 0;
}

static inline bool
PG_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
{
return SDL_SetSurfaceAlphaMod(surface, alpha) == 0;
}

// NOTE:
// palette is part of the format in SDL2, so these functions below have it
// as a separate parameter to be consistent with the SDL3 signature.
Expand Down
4 changes: 2 additions & 2 deletions src_c/alphablit.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ SoftBlitPyGame(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
if ((info.src_has_colorkey = SDL_HasColorKey(src))) {
SDL_GetColorKey(src, &info.src_colorkey);
}
if (SDL_GetSurfaceBlendMode(src, &info.src_blend) ||
SDL_GetSurfaceBlendMode(dst, &info.dst_blend)) {
if (!PG_GetSurfaceBlendMode(src, &info.src_blend) ||
!PG_GetSurfaceBlendMode(dst, &info.dst_blend)) {
okay = 0;
}
if (okay) {
Expand Down
2 changes: 1 addition & 1 deletion src_c/freetype/ft_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ _PGFT_Render_NewSurface(FreeTypeInstance *ft, pgFontObject *fontobj,
colors[0].g = ~colors[1].g;
colors[0].b = ~colors[1].b;
colors[0].a = SDL_ALPHA_OPAQUE;
if (SDL_SetPaletteColors(palette, colors, 0, 2)) {
if (!PG_SetPaletteColors(palette, colors, 0, 2)) {
PyErr_Format(PyExc_SystemError,
"Pygame bug in _PGFT_Render_NewSurface: %.200s",
SDL_GetError());
Expand Down
11 changes: 3 additions & 8 deletions src_c/pixelcopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,7 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf,
intsize);
return -1;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_GetSurfaceBlendMode(surf, &mode))
#else
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0)
#endif
{
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}
Expand Down Expand Up @@ -1210,8 +1205,8 @@ make_surface(PyObject *self, PyObject *arg)
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) {
/* Give the surface something other than an all white palette.
* */
if (SDL_SetPaletteColors(palette, default_palette_colors, 0,
default_palette_size - 1) != 0) {
if (!PG_SetPaletteColors(palette, default_palette_colors, 0,
default_palette_size - 1)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(surf);
return 0;
Expand Down
41 changes: 20 additions & 21 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,8 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
/* Give the surface something other than an all white palette.
* */
SDL_Palette *surf_palette = SDL_CreateSurfacePalette(surface);
if (SDL_SetPaletteColors(surf_palette, default_palette_colors, 0,
default_palette_size - 1) != 0) {
if (!PG_SetPaletteColors(surf_palette, default_palette_colors, 0,
default_palette_size - 1)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(surface);
return -1;
Expand Down Expand Up @@ -858,9 +858,9 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surface))) {
/* Give the surface something other than an all white palette.
* */
if (SDL_SetPaletteColors(surface->format->palette,
if (!PG_SetPaletteColors(surface->format->palette,
default_palette_colors, 0,
default_palette_size - 1) != 0) {
default_palette_size - 1)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(surface);
return -1;
Expand Down Expand Up @@ -1332,7 +1332,7 @@ surf_set_palette(PyObject *self, PyObject *seq)
}

if (!pal) {
return RAISE(pgExc_SDLError, "Surface is not palettitized\n");
return RAISE(pgExc_SDLError, "Surface is not palettized\n");
}
old_colors = pal->colors;

Expand Down Expand Up @@ -1360,8 +1360,7 @@ surf_set_palette(PyObject *self, PyObject *seq)
colors[i].a = (unsigned char)old_colors[i].a;
}

ecode = SDL_SetPaletteColors(pal, colors, 0, len);
if (ecode != 0) {
if (!PG_SetPaletteColors(pal, colors, 0, len)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
Py_RETURN_NONE;
Expand Down Expand Up @@ -1405,7 +1404,7 @@ surf_set_palette_at(PyObject *self, PyObject *args)
color.b = rgba[2];
color.a = pal->colors[_index].a; /* May be a colorkey color. */

if (SDL_SetPaletteColors(pal, &color, _index, 1) != 0) {
if (!PG_SetPaletteColors(pal, &color, _index, 1)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}

Expand Down Expand Up @@ -1518,12 +1517,12 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
return RAISE(PyExc_TypeError, "invalid alpha argument");
}

if (SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND) != 0) {
if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
else {
if (SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE) != 0) {
if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
Expand All @@ -1540,7 +1539,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)

if (alpha == 255 && (PG_SURF_BytesPerPixel(surf) == 1)) {
/* Can't blend with a surface alpha of 255 and 8bit surfaces */
if (SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE) != 0) {
if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
Expand All @@ -1563,11 +1562,11 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
}
/* HACK HACK HACK */
if (result == 0) {
result = SDL_SetSurfaceAlphaMod(surf, alpha);
result = !PG_SetSurfaceAlphaMod(surf, alpha);
}
pgSurface_Unprep(self);

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

Expand All @@ -1583,15 +1582,15 @@ surf_get_alpha(pgSurfaceObject *self, PyObject *_null)

SURF_INIT_CHECK(surf)

if (SDL_GetSurfaceBlendMode(surf, &mode) != 0) {
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}

if (mode != SDL_BLENDMODE_BLEND) {
Py_RETURN_NONE;
}

if (SDL_GetSurfaceAlphaMod(surf, &alpha) != 0) {
if (!PG_GetSurfaceAlphaMod(surf, &alpha)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}

Expand All @@ -1606,7 +1605,7 @@ surf_get_blendmode(PyObject *self, PyObject *_null)

SURF_INIT_CHECK(surf)

if (SDL_GetSurfaceBlendMode(surf, &mode) != 0) {
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
return PyLong_FromLong((long)mode);
Expand Down Expand Up @@ -2971,7 +2970,7 @@ static int
_PgSurface_SrcAlpha(SDL_Surface *surf)
{
SDL_BlendMode mode;
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0) {
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}
Expand Down Expand Up @@ -3232,7 +3231,7 @@ surf_subsurface(PyObject *self, PyObject *args)
SDL_FreeSurface(sub);
return NULL;
}
if (SDL_SetPaletteColors(pal, colors, 0, ncolors) != 0) {
if (!PG_SetPaletteColors(pal, colors, 0, ncolors)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreePalette(pal);
SDL_FreeSurface(sub);
Expand All @@ -3246,13 +3245,13 @@ surf_subsurface(PyObject *self, PyObject *args)
}
SDL_FreePalette(pal);
}
if (SDL_GetSurfaceAlphaMod(surf, &alpha) != 0) {
if (!PG_GetSurfaceAlphaMod(surf, &alpha)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(sub);
return NULL;
}
if (alpha != 255) {
if (SDL_SetSurfaceAlphaMod(sub, alpha) != 0) {
if (!PG_SetSurfaceAlphaMod(sub, alpha)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(sub);
return NULL;
Expand Down Expand Up @@ -4516,7 +4515,7 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
/* can't blit alpha to 8bit, crashes SDL */
else if (PG_SURF_BytesPerPixel(dst) == 1 &&
(SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(src)) ||
((SDL_GetSurfaceAlphaMod(src, &alpha) == 0 && alpha != 255)))) {
((PG_GetSurfaceAlphaMod(src, &alpha) && alpha != 255)))) {
/* Py_BEGIN_ALLOW_THREADS */
if (PG_SURF_BytesPerPixel(src) == 1) {
result = pygame_Blit(src, srcrect, dst, dstrect, 0);
Expand Down
21 changes: 8 additions & 13 deletions src_c/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ _PgSurface_SrcAlpha(SDL_Surface *surf)
{
if (SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(surf))) {
SDL_BlendMode mode;
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_GetSurfaceBlendMode(surf, &mode))
#else
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0)
#endif
{
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
return -1;
}
if (mode == SDL_BLENDMODE_BLEND) {
Expand All @@ -109,7 +104,7 @@ _PgSurface_SrcAlpha(SDL_Surface *surf)
}
else {
Uint8 color = SDL_ALPHA_OPAQUE;
if (SDL_GetSurfaceAlphaMod(surf, &color) != 0) {
if (!PG_GetSurfaceAlphaMod(surf, &color)) {
return -1;
}
if (color != SDL_ALPHA_OPAQUE) {
Expand Down Expand Up @@ -158,21 +153,21 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
return NULL;
}

if (SDL_SetPaletteColors(newsurf_palette, surf_palette->colors, 0,
surf_palette->ncolors) != 0) {
if (!PG_SetPaletteColors(newsurf_palette, surf_palette->colors, 0,
surf_palette->ncolors)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
}
}

if (SDL_GetSurfaceAlphaMod(surf, &alpha) != 0) {
if (!PG_GetSurfaceAlphaMod(surf, &alpha)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
}
if (alpha != 255) {
if (SDL_SetSurfaceAlphaMod(newsurf, alpha) != 0) {
if (!PG_SetSurfaceAlphaMod(newsurf, alpha)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
Expand All @@ -181,7 +176,7 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)

isalpha = _PgSurface_SrcAlpha(surf);
if (isalpha == 1) {
if (SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_BLEND) != 0) {
if (!PG_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_BLEND)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
Expand All @@ -193,7 +188,7 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
return NULL;
}
else {
if (SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE) != 0) {
if (!PG_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
Expand Down
Loading