Skip to content

Commit a6db5c7

Browse files
authored
Merge pull request #3515 from Starbuck5/port-more-sdl-functions
Port more SDL functions (SDL3)
2 parents 1048811 + ef726d9 commit a6db5c7

File tree

6 files changed

+48
-39
lines changed

6 files changed

+48
-39
lines changed

src_c/_pygame.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#define PG_PixelFormatEnum SDL_PixelFormat
8383

8484
#define PG_SurfaceHasRLE SDL_SurfaceHasRLE
85+
#define PG_SetSurfaceRLE SDL_SetSurfaceRLE
8586

8687
#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \
8788
SDL_StretchSurface(src, srcrect, dst, dstrect, SDL_SCALEMODE_NEAREST)
@@ -132,6 +133,8 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
132133

133134
#define PG_GetSurfacePalette SDL_GetSurfacePalette
134135
#define PG_SetPaletteColors SDL_SetPaletteColors
136+
#define PG_SetSurfacePalette SDL_SetSurfacePalette
137+
#define PG_SetSurfaceColorKey SDL_SetSurfaceColorKey
135138
#define PG_SetSurfaceBlendMode SDL_SetSurfaceBlendMode
136139
#define PG_GetSurfaceBlendMode SDL_GetSurfaceBlendMode
137140
#define PG_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod
@@ -248,6 +251,18 @@ PG_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors,
248251
return SDL_SetPaletteColors(palette, colors, firstcolor, ncolors) == 0;
249252
}
250253

254+
static inline bool
255+
PG_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
256+
{
257+
return SDL_SetSurfacePalette(surface, palette) == 0;
258+
}
259+
260+
static inline bool
261+
PG_SetSurfaceColorKey(SDL_Surface *surface, bool enabled, Uint32 key)
262+
{
263+
return SDL_SetColorKey(surface, enabled, key) == 0;
264+
}
265+
251266
static inline bool
252267
PG_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
253268
{
@@ -343,6 +358,12 @@ PG_InitSubSystem(Uint32 flags)
343358

344359
#define PG_SurfaceHasRLE SDL_HasSurfaceRLE
345360

361+
static inline bool
362+
PG_SetSurfaceRLE(SDL_Surface *surface, bool enabled)
363+
{
364+
return SDL_SetSurfaceRLE(surface, enabled) == 0;
365+
}
366+
346367
static inline bool
347368
PG_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect)
348369
{

src_c/image.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,12 +1795,9 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
17951795
}
17961796

17971797
if (h.has_cmap) {
1798-
#if SDL_VERSION_ATLEAST(3, 0, 0)
1799-
if (!SDL_SetSurfacePalette(linebuf, surf_palette))
1800-
#else
1801-
if (SDL_SetSurfacePalette(linebuf, surf_palette) < 0)
1802-
#endif
1798+
if (!PG_SetSurfacePalette(linebuf, surf_palette)) {
18031799
goto error; /* SDL error already set. */
1800+
}
18041801
}
18051802

18061803
if (rle) {

src_c/rotozoom.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,11 @@ 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
}
596-
if (PG_SurfaceHasRLE(src) &&
597-
SDL_SetSurfaceRLE(rz_dst, SDL_TRUE) != 0) {
596+
if (PG_SurfaceHasRLE(src) && !PG_SetSurfaceRLE(rz_dst, SDL_TRUE)) {
598597
SDL_FreeSurface(rz_dst);
599598
return NULL;
600599
}
@@ -649,12 +648,11 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
649648
PG_CreateSurface(dstwidth, dstheight, PG_SURF_FORMATENUM(rz_src));
650649
if (SDL_HasColorKey(src)) {
651650
SDL_GetColorKey(src, &colorkey);
652-
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
651+
if (!PG_SetSurfaceColorKey(rz_dst, SDL_TRUE, colorkey)) {
653652
SDL_FreeSurface(rz_dst);
654653
return NULL;
655654
}
656-
if (PG_SurfaceHasRLE(src) &&
657-
SDL_SetSurfaceRLE(rz_dst, SDL_TRUE) != 0) {
655+
if (PG_SurfaceHasRLE(src) && !PG_SetSurfaceRLE(rz_dst, SDL_TRUE)) {
658656
SDL_FreeSurface(rz_dst);
659657
return NULL;
660658
}

src_c/surface.c

Lines changed: 17 additions & 18 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,22 @@ 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(
1444+
if (success && hascolor) {
1445+
success = PG_SetSurfaceRLE(
14471446
surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
14481447
}
1449-
if (result == 0) {
1450-
result = SDL_SetColorKey(surf, hascolor, color);
1448+
if (success) {
1449+
success = PG_SetSurfaceColorKey(surf, hascolor, color);
14511450
}
14521451
pgSurface_Unprep(self);
14531452

1454-
if (result == -1) {
1453+
if (!success) {
14551454
return RAISE(pgExc_SDLError, SDL_GetError());
14561455
}
14571456

@@ -1496,7 +1495,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
14961495
Uint32 flags = 0;
14971496
PyObject *alpha_obj = NULL, *intobj = NULL;
14981497
Uint8 alpha;
1499-
int result, alphaval = 255;
1498+
int alphaval = 255;
15001499
SDL_Rect sdlrect;
15011500
SDL_Surface *surface;
15021501

@@ -1544,8 +1543,8 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
15441543
}
15451544
}
15461545
pgSurface_Prep(self);
1547-
result =
1548-
SDL_SetSurfaceRLE(surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
1546+
bool success =
1547+
PG_SetSurfaceRLE(surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
15491548
/* HACK HACK HACK */
15501549
if ((surf->flags & SDL_RLEACCEL) && (!(flags & PGS_RLEACCEL))) {
15511550
/* hack to strip SDL_RLEACCEL flag off surface immediately when
@@ -1561,12 +1560,12 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
15611560
SDL_FreeSurface(surface);
15621561
}
15631562
/* HACK HACK HACK */
1564-
if (result == 0) {
1565-
result = !PG_SetSurfaceAlphaMod(surf, alpha);
1563+
if (success) {
1564+
success = PG_SetSurfaceAlphaMod(surf, alpha);
15661565
}
15671566
pgSurface_Unprep(self);
15681567

1569-
if (result != 0) {
1568+
if (!success) {
15701569
return RAISE(pgExc_SDLError, SDL_GetError());
15711570
}
15721571

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

18151814
if (has_colorkey) {
18161815
colorkey = SDL_MapSurfaceRGBA(newsurf, key_r, key_g, key_b, key_a);
1817-
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
1816+
if (!PG_SetSurfaceColorKey(newsurf, SDL_TRUE, colorkey)) {
18181817
PyErr_SetString(pgExc_SDLError, SDL_GetError());
18191818
SDL_FreeSurface(newsurf);
18201819
return NULL;
@@ -1977,7 +1976,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
19771976

19781977
if (has_colorkey) {
19791978
colorkey = SDL_MapRGBA(newsurf->format, key_r, key_g, key_b, key_a);
1980-
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
1979+
if (!PG_SetSurfaceColorKey(newsurf, SDL_TRUE, colorkey)) {
19811980
PyErr_SetString(pgExc_SDLError, SDL_GetError());
19821981
SDL_FreeSurface(newsurf);
19831982
return NULL;
@@ -3237,7 +3236,7 @@ surf_subsurface(PyObject *self, PyObject *args)
32373236
SDL_FreeSurface(sub);
32383237
return NULL;
32393238
}
3240-
if (SDL_SetSurfacePalette(sub, pal) != 0) {
3239+
if (!PG_SetSurfacePalette(sub, pal)) {
32413240
PyErr_SetString(pgExc_SDLError, SDL_GetError());
32423241
SDL_FreePalette(pal);
32433242
SDL_FreeSurface(sub);
@@ -3259,7 +3258,7 @@ surf_subsurface(PyObject *self, PyObject *args)
32593258
}
32603259
if (SDL_HasColorKey(surf)) {
32613260
SDL_GetColorKey(surf, &colorkey);
3262-
if (SDL_SetColorKey(sub, SDL_TRUE, colorkey) != 0) {
3261+
if (!PG_SetSurfaceColorKey(sub, SDL_TRUE, colorkey)) {
32633262
PyErr_SetString(pgExc_SDLError, SDL_GetError());
32643263
SDL_FreeSurface(sub);
32653264
return NULL;

src_c/transform.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,12 @@ 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;
204204
}
205-
if (PG_SurfaceHasRLE(surf) &&
206-
SDL_SetSurfaceRLE(newsurf, SDL_TRUE) != 0) {
205+
if (PG_SurfaceHasRLE(surf) && !PG_SetSurfaceRLE(newsurf, SDL_TRUE)) {
207206
PyErr_SetString(pgExc_SDLError, SDL_GetError());
208207
SDL_FreeSurface(newsurf);
209208
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)