Skip to content

Commit ac09c5b

Browse files
authored
Merge pull request #3434 from Starbuck5/port-a-few-functions-sdl3
2 parents fbe0a52 + cf7adab commit ac09c5b

File tree

6 files changed

+70
-45
lines changed

6 files changed

+70
-45
lines changed

src_c/_pygame.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
131131
}
132132

133133
#define PG_GetSurfacePalette SDL_GetSurfacePalette
134+
#define PG_SetPaletteColors SDL_SetPaletteColors
135+
#define PG_SetSurfaceBlendMode SDL_SetSurfaceBlendMode
136+
#define PG_GetSurfaceBlendMode SDL_GetSurfaceBlendMode
137+
#define PG_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod
138+
#define PG_SetSurfaceAlphaMod SDL_SetSurfaceAlphaMod
134139

135140
#define PG_GetRGBA SDL_GetRGBA
136141
#define PG_GetRGB SDL_GetRGB
@@ -233,6 +238,37 @@ PG_GetSurfacePalette(SDL_Surface *surf)
233238
return surf->format->palette;
234239
}
235240

241+
static inline bool
242+
PG_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors,
243+
int firstcolor, int ncolors)
244+
{
245+
return SDL_SetPaletteColors(palette, colors, firstcolor, ncolors) == 0;
246+
}
247+
248+
static inline bool
249+
PG_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
250+
{
251+
return SDL_SetSurfaceBlendMode(surface, blendMode) == 0;
252+
}
253+
254+
static inline bool
255+
PG_GetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode *blendMode)
256+
{
257+
return SDL_GetSurfaceBlendMode(surface, blendMode) == 0;
258+
}
259+
260+
static inline bool
261+
PG_GetSurfaceAlphaMod(SDL_Surface *surface, Uint8 *alpha)
262+
{
263+
return SDL_GetSurfaceAlphaMod(surface, alpha) == 0;
264+
}
265+
266+
static inline bool
267+
PG_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
268+
{
269+
return SDL_SetSurfaceAlphaMod(surface, alpha) == 0;
270+
}
271+
236272
// NOTE:
237273
// palette is part of the format in SDL2, so these functions below have it
238274
// as a separate parameter to be consistent with the SDL3 signature.

src_c/alphablit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ SoftBlitPyGame(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
127127
if ((info.src_has_colorkey = SDL_HasColorKey(src))) {
128128
SDL_GetColorKey(src, &info.src_colorkey);
129129
}
130-
if (SDL_GetSurfaceBlendMode(src, &info.src_blend) ||
131-
SDL_GetSurfaceBlendMode(dst, &info.dst_blend)) {
130+
if (!PG_GetSurfaceBlendMode(src, &info.src_blend) ||
131+
!PG_GetSurfaceBlendMode(dst, &info.dst_blend)) {
132132
okay = 0;
133133
}
134134
if (okay) {

src_c/freetype/ft_render.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ _PGFT_Render_NewSurface(FreeTypeInstance *ft, pgFontObject *fontobj,
494494
colors[0].g = ~colors[1].g;
495495
colors[0].b = ~colors[1].b;
496496
colors[0].a = SDL_ALPHA_OPAQUE;
497-
if (SDL_SetPaletteColors(palette, colors, 0, 2)) {
497+
if (!PG_SetPaletteColors(palette, colors, 0, 2)) {
498498
PyErr_Format(PyExc_SystemError,
499499
"Pygame bug in _PGFT_Render_NewSurface: %.200s",
500500
SDL_GetError());

src_c/pixelcopy.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,7 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf,
287287
intsize);
288288
return -1;
289289
}
290-
#if SDL_VERSION_ATLEAST(3, 0, 0)
291-
if (!SDL_GetSurfaceBlendMode(surf, &mode))
292-
#else
293-
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0)
294-
#endif
295-
{
290+
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
296291
PyErr_SetString(pgExc_SDLError, SDL_GetError());
297292
return -1;
298293
}
@@ -1210,8 +1205,8 @@ make_surface(PyObject *self, PyObject *arg)
12101205
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) {
12111206
/* Give the surface something other than an all white palette.
12121207
* */
1213-
if (SDL_SetPaletteColors(palette, default_palette_colors, 0,
1214-
default_palette_size - 1) != 0) {
1208+
if (!PG_SetPaletteColors(palette, default_palette_colors, 0,
1209+
default_palette_size - 1)) {
12151210
PyErr_SetString(pgExc_SDLError, SDL_GetError());
12161211
SDL_FreeSurface(surf);
12171212
return 0;

src_c/surface.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,8 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
674674
/* Give the surface something other than an all white palette.
675675
* */
676676
SDL_Palette *surf_palette = SDL_CreateSurfacePalette(surface);
677-
if (SDL_SetPaletteColors(surf_palette, default_palette_colors, 0,
678-
default_palette_size - 1) != 0) {
677+
if (!PG_SetPaletteColors(surf_palette, default_palette_colors, 0,
678+
default_palette_size - 1)) {
679679
PyErr_SetString(pgExc_SDLError, SDL_GetError());
680680
SDL_FreeSurface(surface);
681681
return -1;
@@ -858,9 +858,9 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
858858
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surface))) {
859859
/* Give the surface something other than an all white palette.
860860
* */
861-
if (SDL_SetPaletteColors(surface->format->palette,
861+
if (!PG_SetPaletteColors(surface->format->palette,
862862
default_palette_colors, 0,
863-
default_palette_size - 1) != 0) {
863+
default_palette_size - 1)) {
864864
PyErr_SetString(pgExc_SDLError, SDL_GetError());
865865
SDL_FreeSurface(surface);
866866
return -1;
@@ -1332,7 +1332,7 @@ surf_set_palette(PyObject *self, PyObject *seq)
13321332
}
13331333

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

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

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

1408-
if (SDL_SetPaletteColors(pal, &color, _index, 1) != 0) {
1407+
if (!PG_SetPaletteColors(pal, &color, _index, 1)) {
14091408
return RAISE(pgExc_SDLError, SDL_GetError());
14101409
}
14111410

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

1521-
if (SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND) != 0) {
1520+
if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND)) {
15221521
return RAISE(pgExc_SDLError, SDL_GetError());
15231522
}
15241523
}
15251524
else {
1526-
if (SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE) != 0) {
1525+
if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE)) {
15271526
return RAISE(pgExc_SDLError, SDL_GetError());
15281527
}
15291528
}
@@ -1540,7 +1539,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
15401539

15411540
if (alpha == 255 && (PG_SURF_BytesPerPixel(surf) == 1)) {
15421541
/* Can't blend with a surface alpha of 255 and 8bit surfaces */
1543-
if (SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE) != 0) {
1542+
if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE)) {
15441543
return RAISE(pgExc_SDLError, SDL_GetError());
15451544
}
15461545
}
@@ -1563,11 +1562,11 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
15631562
}
15641563
/* HACK HACK HACK */
15651564
if (result == 0) {
1566-
result = SDL_SetSurfaceAlphaMod(surf, alpha);
1565+
result = !PG_SetSurfaceAlphaMod(surf, alpha);
15671566
}
15681567
pgSurface_Unprep(self);
15691568

1570-
if (result == -1) {
1569+
if (result != 0) {
15711570
return RAISE(pgExc_SDLError, SDL_GetError());
15721571
}
15731572

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

15841583
SURF_INIT_CHECK(surf)
15851584

1586-
if (SDL_GetSurfaceBlendMode(surf, &mode) != 0) {
1585+
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
15871586
return RAISE(pgExc_SDLError, SDL_GetError());
15881587
}
15891588

15901589
if (mode != SDL_BLENDMODE_BLEND) {
15911590
Py_RETURN_NONE;
15921591
}
15931592

1594-
if (SDL_GetSurfaceAlphaMod(surf, &alpha) != 0) {
1593+
if (!PG_GetSurfaceAlphaMod(surf, &alpha)) {
15951594
return RAISE(pgExc_SDLError, SDL_GetError());
15961595
}
15971596

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

16071606
SURF_INIT_CHECK(surf)
16081607

1609-
if (SDL_GetSurfaceBlendMode(surf, &mode) != 0) {
1608+
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
16101609
return RAISE(pgExc_SDLError, SDL_GetError());
16111610
}
16121611
return PyLong_FromLong((long)mode);
@@ -2971,7 +2970,7 @@ static int
29712970
_PgSurface_SrcAlpha(SDL_Surface *surf)
29722971
{
29732972
SDL_BlendMode mode;
2974-
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0) {
2973+
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
29752974
PyErr_SetString(pgExc_SDLError, SDL_GetError());
29762975
return -1;
29772976
}
@@ -3232,7 +3231,7 @@ surf_subsurface(PyObject *self, PyObject *args)
32323231
SDL_FreeSurface(sub);
32333232
return NULL;
32343233
}
3235-
if (SDL_SetPaletteColors(pal, colors, 0, ncolors) != 0) {
3234+
if (!PG_SetPaletteColors(pal, colors, 0, ncolors)) {
32363235
PyErr_SetString(pgExc_SDLError, SDL_GetError());
32373236
SDL_FreePalette(pal);
32383237
SDL_FreeSurface(sub);
@@ -3246,13 +3245,13 @@ surf_subsurface(PyObject *self, PyObject *args)
32463245
}
32473246
SDL_FreePalette(pal);
32483247
}
3249-
if (SDL_GetSurfaceAlphaMod(surf, &alpha) != 0) {
3248+
if (!PG_GetSurfaceAlphaMod(surf, &alpha)) {
32503249
PyErr_SetString(pgExc_SDLError, SDL_GetError());
32513250
SDL_FreeSurface(sub);
32523251
return NULL;
32533252
}
32543253
if (alpha != 255) {
3255-
if (SDL_SetSurfaceAlphaMod(sub, alpha) != 0) {
3254+
if (!PG_SetSurfaceAlphaMod(sub, alpha)) {
32563255
PyErr_SetString(pgExc_SDLError, SDL_GetError());
32573256
SDL_FreeSurface(sub);
32583257
return NULL;
@@ -4516,7 +4515,7 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
45164515
/* can't blit alpha to 8bit, crashes SDL */
45174516
else if (PG_SURF_BytesPerPixel(dst) == 1 &&
45184517
(SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(src)) ||
4519-
((SDL_GetSurfaceAlphaMod(src, &alpha) == 0 && alpha != 255)))) {
4518+
((PG_GetSurfaceAlphaMod(src, &alpha) && alpha != 255)))) {
45204519
/* Py_BEGIN_ALLOW_THREADS */
45214520
if (PG_SURF_BytesPerPixel(src) == 1) {
45224521
result = pygame_Blit(src, srcrect, dst, dstrect, 0);

src_c/transform.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,7 @@ _PgSurface_SrcAlpha(SDL_Surface *surf)
9595
{
9696
if (SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(surf))) {
9797
SDL_BlendMode mode;
98-
#if SDL_VERSION_ATLEAST(3, 0, 0)
99-
if (!SDL_GetSurfaceBlendMode(surf, &mode))
100-
#else
101-
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0)
102-
#endif
103-
{
98+
if (!PG_GetSurfaceBlendMode(surf, &mode)) {
10499
return -1;
105100
}
106101
if (mode == SDL_BLENDMODE_BLEND) {
@@ -109,7 +104,7 @@ _PgSurface_SrcAlpha(SDL_Surface *surf)
109104
}
110105
else {
111106
Uint8 color = SDL_ALPHA_OPAQUE;
112-
if (SDL_GetSurfaceAlphaMod(surf, &color) != 0) {
107+
if (!PG_GetSurfaceAlphaMod(surf, &color)) {
113108
return -1;
114109
}
115110
if (color != SDL_ALPHA_OPAQUE) {
@@ -158,21 +153,21 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
158153
return NULL;
159154
}
160155

161-
if (SDL_SetPaletteColors(newsurf_palette, surf_palette->colors, 0,
162-
surf_palette->ncolors) != 0) {
156+
if (!PG_SetPaletteColors(newsurf_palette, surf_palette->colors, 0,
157+
surf_palette->ncolors)) {
163158
PyErr_SetString(pgExc_SDLError, SDL_GetError());
164159
SDL_FreeSurface(newsurf);
165160
return NULL;
166161
}
167162
}
168163

169-
if (SDL_GetSurfaceAlphaMod(surf, &alpha) != 0) {
164+
if (!PG_GetSurfaceAlphaMod(surf, &alpha)) {
170165
PyErr_SetString(pgExc_SDLError, SDL_GetError());
171166
SDL_FreeSurface(newsurf);
172167
return NULL;
173168
}
174169
if (alpha != 255) {
175-
if (SDL_SetSurfaceAlphaMod(newsurf, alpha) != 0) {
170+
if (!PG_SetSurfaceAlphaMod(newsurf, alpha)) {
176171
PyErr_SetString(pgExc_SDLError, SDL_GetError());
177172
SDL_FreeSurface(newsurf);
178173
return NULL;
@@ -181,7 +176,7 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
181176

182177
isalpha = _PgSurface_SrcAlpha(surf);
183178
if (isalpha == 1) {
184-
if (SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_BLEND) != 0) {
179+
if (!PG_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_BLEND)) {
185180
PyErr_SetString(pgExc_SDLError, SDL_GetError());
186181
SDL_FreeSurface(newsurf);
187182
return NULL;
@@ -193,7 +188,7 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
193188
return NULL;
194189
}
195190
else {
196-
if (SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE) != 0) {
191+
if (!PG_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE)) {
197192
PyErr_SetString(pgExc_SDLError, SDL_GetError());
198193
SDL_FreeSurface(newsurf);
199194
return NULL;

0 commit comments

Comments
 (0)