Skip to content

Commit 5156043

Browse files
Expose color key more directly, use SDL_HasColorKey
1 parent dfaa76e commit 5156043

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

src_c/surface.c

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,14 @@ static struct PyMethodDef surface_methods[] = {
290290

291291
{"set_colorkey", (PyCFunction)surf_set_colorkey, METH_VARARGS,
292292
DOC_SURFACE_SETCOLORKEY},
293+
{"set_colorkey_raw", (PyCFunction)surf_set_colorkey_raw, METH_VARARGS,
294+
"TODO"},
293295
{"get_colorkey", (PyCFunction)surf_get_colorkey, METH_NOARGS,
294296
DOC_SURFACE_GETCOLORKEY},
297+
{"get_colorkey_raw", (PyCFunction)surf_get_colorkey_raw, METH_NOARGS,
298+
"TODO"},
299+
{"has_colorkey", (PyCFunction)surf_has_colorkey, METH_NOARGS,
300+
"TODO"},
295301
{"set_alpha", (PyCFunction)surf_set_alpha, METH_VARARGS,
296302
DOC_SURFACE_SETALPHA},
297303
{"get_alpha", (PyCFunction)surf_get_alpha, METH_NOARGS,
@@ -1057,12 +1063,14 @@ surf_get_palette(PyObject *self, PyObject *_null)
10571063
if (!list)
10581064
return NULL;
10591065

1066+
10601067
for (i = 0; i < pal->ncolors; i++) {
10611068
c = &pal->colors[i];
10621069
rgba[0] = c->r;
10631070
rgba[1] = c->g;
10641071
rgba[2] = c->b;
1065-
color = pgColor_NewLength(rgba, 3);
1072+
rgba[3] = c->a;
1073+
color = pgColor_New(rgba);
10661074

10671075
if (!color) {
10681076
Py_DECREF(list);
@@ -1252,6 +1260,30 @@ surf_set_colorkey(pgSurfaceObject *self, PyObject *args)
12521260
Py_RETURN_NONE;
12531261
}
12541262

1263+
static PyObject *
1264+
surf_set_colorkey_raw(pgSurfaceObject *self, PyObject *args)
1265+
{
1266+
SDL_Surface *surf = pgSurface_AsSurface(self);
1267+
Uint32 flags = 0, color = 0;
1268+
int result;
1269+
int hascolor = SDL_FALSE;
1270+
1271+
if (!PyArg_ParseTuple(args, "I", &color))
1272+
return NULL;
1273+
1274+
SURF_INIT_CHECK(surf)
1275+
1276+
1277+
pgSurface_Prep(self);
1278+
result = SDL_SetColorKey(surf, SDL_TRUE, color);
1279+
pgSurface_Unprep(self);
1280+
1281+
if (result == -1)
1282+
return RAISE(pgExc_SDLError, SDL_GetError());
1283+
1284+
Py_RETURN_NONE;
1285+
}
1286+
12551287
static PyObject *
12561288
surf_get_colorkey(pgSurfaceObject *self, PyObject *_null)
12571289
{
@@ -1261,11 +1293,12 @@ surf_get_colorkey(pgSurfaceObject *self, PyObject *_null)
12611293

12621294
SURF_INIT_CHECK(surf)
12631295

1264-
if (SDL_GetColorKey(surf, &mapped_color) != 0) {
1265-
SDL_ClearError();
1296+
if(!SDL_HasColorKey(surf)){
12661297
Py_RETURN_NONE;
12671298
}
12681299

1300+
SDL_GetColorKey(surf, &mapped_color);
1301+
12691302
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format))
12701303
SDL_GetRGBA(mapped_color, surf->format, &r, &g, &b, &a);
12711304
else
@@ -1274,6 +1307,38 @@ surf_get_colorkey(pgSurfaceObject *self, PyObject *_null)
12741307
return Py_BuildValue("(bbbb)", r, g, b, a);
12751308
}
12761309

1310+
static PyObject *
1311+
surf_get_colorkey_raw(pgSurfaceObject *self, PyObject *_null)
1312+
{
1313+
SDL_Surface *surf = pgSurface_AsSurface(self);
1314+
Uint32 mapped_color;
1315+
Uint8 r, g, b, a = 255;
1316+
1317+
SURF_INIT_CHECK(surf)
1318+
1319+
if (SDL_GetColorKey(surf, &mapped_color) != 0) {
1320+
return RAISE(pgExc_SDLError, SDL_GetError());
1321+
}
1322+
1323+
return PyLong_FromLong(mapped_color);
1324+
}
1325+
1326+
static PyObject *
1327+
surf_has_colorkey(pgSurfaceObject *self, PyObject *_null)
1328+
{
1329+
SDL_Surface *surf = pgSurface_AsSurface(self);
1330+
Uint32 mapped_color;
1331+
Uint8 r, g, b, a = 255;
1332+
1333+
SURF_INIT_CHECK(surf)
1334+
1335+
if(SDL_HasColorKey(surf)){
1336+
Py_RETURN_TRUE;
1337+
} else {
1338+
Py_RETURN_FALSE;
1339+
}
1340+
}
1341+
12771342
static PyObject *
12781343
surf_set_alpha(pgSurfaceObject *self, PyObject *args)
12791344
{
@@ -2425,7 +2490,7 @@ surf_get_flags(PyObject *self, PyObject *_null)
24252490
if (is_alpha) {
24262491
flags |= PGS_SRCALPHA;
24272492
}
2428-
if (SDL_GetColorKey(surf, NULL) == 0)
2493+
if (SDL_HasColorKey(surf, NULL))
24292494
flags |= PGS_SRCCOLORKEY;
24302495
if (sdl_flags & SDL_PREALLOC)
24312496
flags |= PGS_PREALLOC;
@@ -2831,7 +2896,7 @@ surf_get_bounding_rect(PyObject *self, PyObject *args, PyObject *kwargs)
28312896

28322897
format = surf->format;
28332898

2834-
if (SDL_GetColorKey(surf, &colorkey) == 0) {
2899+
if (SDL_HasColorKey(surf, &colorkey)) {
28352900
has_colorkey = 1;
28362901
SDL_GetRGBA(colorkey, surf->format, &keyr, &keyg, &keyb, &a);
28372902
}
@@ -3860,7 +3925,7 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
38603925
pgSurface_Prep(srcobj);
38613926

38623927
if ((blend_flags != 0 && blend_flags != PYGAME_BLEND_ALPHA_SDL2) ||
3863-
((SDL_GetColorKey(src, &key) == 0 || _PgSurface_SrcAlpha(src) == 1) &&
3928+
((SDL_HasColorKey(src) || _PgSurface_SrcAlpha(src) == 1) &&
38643929
/* This simplification is possible because a source subsurface
38653930
is converted to its owner with a clip rect and a dst
38663931
subsurface cannot be blitted to its owner because the

0 commit comments

Comments
 (0)