Skip to content

Commit c6d0877

Browse files
committed
Port SDL_SetColorKey
1 parent 2adc056 commit c6d0877

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

src_c/_pygame.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
132132

133133
#define PG_GetSurfacePalette SDL_GetSurfacePalette
134134
#define PG_SetPaletteColors SDL_SetPaletteColors
135+
#define PG_SetSurfaceColorKey SDL_SetSurfaceColorKey
135136
#define PG_SetSurfaceBlendMode SDL_SetSurfaceBlendMode
136137
#define PG_GetSurfaceBlendMode SDL_GetSurfaceBlendMode
137138
#define PG_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod
@@ -248,6 +249,12 @@ PG_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors,
248249
return SDL_SetPaletteColors(palette, colors, firstcolor, ncolors) == 0;
249250
}
250251

252+
static inline bool
253+
PG_SetSurfaceColorKey(SDL_Surface *surface, bool enabled, Uint32 key)
254+
{
255+
return SDL_SetColorKey(surface, enabled, key) == 0;
256+
}
257+
251258
static inline bool
252259
PG_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
253260
{

src_c/rotozoom.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
589589
PG_CreateSurface(dstwidth, dstheight, PG_SURF_FORMATENUM(rz_src));
590590
if (SDL_HasColorKey(src)) {
591591
SDL_GetColorKey(src, &colorkey);
592-
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
592+
if (!PG_SetSurfaceColorKey(rz_dst, SDL_TRUE, colorkey)) {
593593
SDL_FreeSurface(rz_dst);
594594
return NULL;
595595
}
@@ -649,7 +649,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
649649
PG_CreateSurface(dstwidth, dstheight, PG_SURF_FORMATENUM(rz_src));
650650
if (SDL_HasColorKey(src)) {
651651
SDL_GetColorKey(src, &colorkey);
652-
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
652+
if (!PG_SetSurfaceColorKey(rz_dst, SDL_TRUE, colorkey)) {
653653
SDL_FreeSurface(rz_dst);
654654
return NULL;
655655
}

src_c/surface.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,6 @@ surf_set_colorkey(pgSurfaceObject *self, PyObject *args)
14171417
SDL_Surface *surf = pgSurface_AsSurface(self);
14181418
Uint32 flags = 0, color = 0;
14191419
PyObject *rgba_obj = NULL;
1420-
int result;
14211420
int hascolor = SDL_FALSE;
14221421

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

14381437
pgSurface_Prep(self);
1439-
result = 0;
1438+
bool success = true;
14401439
if (hascolor && PG_SURF_BytesPerPixel(surf) == 1) {
14411440
/* For an indexed surface, remove the previous colorkey first.
14421441
*/
1443-
result = SDL_SetColorKey(surf, SDL_FALSE, color);
1442+
success = PG_SetSurfaceColorKey(surf, SDL_FALSE, color);
14441443
}
1445-
if (result == 0 && hascolor) {
1446-
result = SDL_SetSurfaceRLE(
1447-
surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
1444+
if (success && hascolor) {
1445+
success =
1446+
SDL_SetSurfaceRLE(
1447+
surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE) == 0;
14481448
}
1449-
if (result == 0) {
1450-
result = SDL_SetColorKey(surf, hascolor, color);
1449+
if (success) {
1450+
success = PG_SetSurfaceColorKey(surf, hascolor, color);
14511451
}
14521452
pgSurface_Unprep(self);
14531453

1454-
if (result == -1) {
1454+
if (!success) {
14551455
return RAISE(pgExc_SDLError, SDL_GetError());
14561456
}
14571457

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

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

19781978
if (has_colorkey) {
19791979
colorkey = SDL_MapRGBA(newsurf->format, key_r, key_g, key_b, key_a);
1980-
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
1980+
if (!PG_SetSurfaceColorKey(newsurf, SDL_TRUE, colorkey)) {
19811981
PyErr_SetString(pgExc_SDLError, SDL_GetError());
19821982
SDL_FreeSurface(newsurf);
19831983
return NULL;
@@ -3259,7 +3259,7 @@ surf_subsurface(PyObject *self, PyObject *args)
32593259
}
32603260
if (SDL_HasColorKey(surf)) {
32613261
SDL_GetColorKey(surf, &colorkey);
3262-
if (SDL_SetColorKey(sub, SDL_TRUE, colorkey) != 0) {
3262+
if (!PG_SetSurfaceColorKey(sub, SDL_TRUE, colorkey)) {
32633263
PyErr_SetString(pgExc_SDLError, SDL_GetError());
32643264
SDL_FreeSurface(sub);
32653265
return NULL;

src_c/transform.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
197197

198198
if (SDL_HasColorKey(surf)) {
199199
SDL_GetColorKey(surf, &colorkey);
200-
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
200+
if (!PG_SetSurfaceColorKey(newsurf, SDL_TRUE, colorkey)) {
201201
PyErr_SetString(pgExc_SDLError, SDL_GetError());
202202
SDL_FreeSurface(newsurf);
203203
return NULL;

src_c/window.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,13 +1251,8 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
12511251
return -1;
12521252
}
12531253
if (icon_colorkey != -1) {
1254-
#if SDL_VERSION_ATLEAST(3, 0, 0)
1255-
if (!SDL_SetColorKey(pgSurface_AsSurface(icon), SDL_TRUE,
1256-
icon_colorkey)) {
1257-
#else
1258-
if (SDL_SetColorKey(pgSurface_AsSurface(icon), SDL_TRUE,
1259-
icon_colorkey) < 0) {
1260-
#endif
1254+
if (!PG_SetSurfaceColorKey(pgSurface_AsSurface(icon), SDL_TRUE,
1255+
icon_colorkey)) {
12611256
PyErr_SetString(pgExc_SDLError, SDL_GetError());
12621257
return -1;
12631258
}

0 commit comments

Comments
 (0)