Skip to content

Commit 3e65f43

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

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

src_c/surface.c

+69-6
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,13 @@ 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, "TODO"},
295300
{"set_alpha", (PyCFunction)surf_set_alpha, METH_VARARGS,
296301
DOC_SURFACE_SETALPHA},
297302
{"get_alpha", (PyCFunction)surf_get_alpha, METH_NOARGS,
@@ -1062,7 +1067,8 @@ surf_get_palette(PyObject *self, PyObject *_null)
10621067
rgba[0] = c->r;
10631068
rgba[1] = c->g;
10641069
rgba[2] = c->b;
1065-
color = pgColor_NewLength(rgba, 3);
1070+
rgba[3] = c->a;
1071+
color = pgColor_New(rgba);
10661072

10671073
if (!color) {
10681074
Py_DECREF(list);
@@ -1252,6 +1258,29 @@ surf_set_colorkey(pgSurfaceObject *self, PyObject *args)
12521258
Py_RETURN_NONE;
12531259
}
12541260

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

12621291
SURF_INIT_CHECK(surf)
12631292

1264-
if (SDL_GetColorKey(surf, &mapped_color) != 0) {
1265-
SDL_ClearError();
1293+
if (!SDL_HasColorKey(surf)) {
12661294
Py_RETURN_NONE;
12671295
}
12681296

1297+
SDL_GetColorKey(surf, &mapped_color);
1298+
12691299
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format))
12701300
SDL_GetRGBA(mapped_color, surf->format, &r, &g, &b, &a);
12711301
else
@@ -1274,6 +1304,39 @@ surf_get_colorkey(pgSurfaceObject *self, PyObject *_null)
12741304
return Py_BuildValue("(bbbb)", r, g, b, a);
12751305
}
12761306

1307+
static PyObject *
1308+
surf_get_colorkey_raw(pgSurfaceObject *self, PyObject *_null)
1309+
{
1310+
SDL_Surface *surf = pgSurface_AsSurface(self);
1311+
Uint32 mapped_color;
1312+
Uint8 r, g, b, a = 255;
1313+
1314+
SURF_INIT_CHECK(surf)
1315+
1316+
if (SDL_GetColorKey(surf, &mapped_color) != 0) {
1317+
return RAISE(pgExc_SDLError, SDL_GetError());
1318+
}
1319+
1320+
return PyLong_FromLong(mapped_color);
1321+
}
1322+
1323+
static PyObject *
1324+
surf_has_colorkey(pgSurfaceObject *self, PyObject *_null)
1325+
{
1326+
SDL_Surface *surf = pgSurface_AsSurface(self);
1327+
Uint32 mapped_color;
1328+
Uint8 r, g, b, a = 255;
1329+
1330+
SURF_INIT_CHECK(surf)
1331+
1332+
if (SDL_HasColorKey(surf)) {
1333+
Py_RETURN_TRUE;
1334+
}
1335+
else {
1336+
Py_RETURN_FALSE;
1337+
}
1338+
}
1339+
12771340
static PyObject *
12781341
surf_set_alpha(pgSurfaceObject *self, PyObject *args)
12791342
{
@@ -2425,7 +2488,7 @@ surf_get_flags(PyObject *self, PyObject *_null)
24252488
if (is_alpha) {
24262489
flags |= PGS_SRCALPHA;
24272490
}
2428-
if (SDL_GetColorKey(surf, NULL) == 0)
2491+
if (SDL_HasColorKey(surf, NULL))
24292492
flags |= PGS_SRCCOLORKEY;
24302493
if (sdl_flags & SDL_PREALLOC)
24312494
flags |= PGS_PREALLOC;
@@ -2831,7 +2894,7 @@ surf_get_bounding_rect(PyObject *self, PyObject *args, PyObject *kwargs)
28312894

28322895
format = surf->format;
28332896

2834-
if (SDL_GetColorKey(surf, &colorkey) == 0) {
2897+
if (SDL_HasColorKey(surf, &colorkey)) {
28352898
has_colorkey = 1;
28362899
SDL_GetRGBA(colorkey, surf->format, &keyr, &keyg, &keyb, &a);
28372900
}
@@ -3860,7 +3923,7 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
38603923
pgSurface_Prep(srcobj);
38613924

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

0 commit comments

Comments
 (0)