Skip to content

Commit 28f5a32

Browse files
authored
Merge pull request #3294 from Starbuck5/add-sdl3-format-enum
Introduce PG_SURF_FORMATENUM macro (SDL3 compat)
2 parents 3ec0832 + 4fcf6e0 commit 28f5a32

10 files changed

+30
-45
lines changed

src_c/_camera.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ surf_colorspace(PyObject *self, PyObject *arg)
109109
surf = pgSurface_AsSurface(surfobj);
110110

111111
if (!surfobj2) {
112-
newsurf = PG_CreateSurface(surf->w, surf->h, surf->format->format);
112+
newsurf = PG_CreateSurface(surf->w, surf->h, PG_SURF_FORMATENUM(surf));
113113
if (!newsurf) {
114114
return NULL;
115115
}

src_c/_pygame.h

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ PG_UnlockMutex(SDL_mutex *mutex)
106106
#define PG_SURF_BytesPerPixel(surf) SDL_BYTESPERPIXEL(surf->format)
107107
#define PG_FORMAT_BitsPerPixel(format) format->bits_per_pixel
108108
#define PG_FORMAT_BytesPerPixel(format) format->bytes_per_pixel
109+
#define PG_SURF_FORMATENUM(surf) surf->format
109110

110111
/* Mask to test if surface flags are in a fullscreen window. */
111112
#define PG_WINDOW_FULLSCREEN_INCLUSIVE SDL_WINDOW_FULLSCREEN
@@ -173,6 +174,7 @@ PG_UnlockMutex(SDL_mutex *mutex)
173174
#define PG_SURF_BytesPerPixel(surf) surf->format->BytesPerPixel
174175
#define PG_FORMAT_BitsPerPixel(format) format->BitsPerPixel
175176
#define PG_FORMAT_BytesPerPixel(format) format->BytesPerPixel
177+
#define PG_SURF_FORMATENUM(surf) surf->format->format
176178

177179
/* Mask to test if surface flags are in a fullscreen window.
178180
* SDL_WINDOW_FULLSCREEN_DESKTOP works here because it also contains

src_c/alphablit.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ SoftBlitPyGame(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
174174
&info);
175175
}
176176
else if (SDL_ISPIXELFORMAT_ALPHA(
177-
dst->format->format) &&
177+
PG_SURF_FORMATENUM(dst)) &&
178178
info.dst_blend !=
179179
SDL_BLENDMODE_NONE) {
180180
alphablit_alpha_avx2_argb_no_surf_alpha(
@@ -193,7 +193,7 @@ SoftBlitPyGame(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
193193
&info);
194194
}
195195
else if (SDL_ISPIXELFORMAT_ALPHA(
196-
dst->format->format) &&
196+
PG_SURF_FORMATENUM(dst)) &&
197197
info.dst_blend !=
198198
SDL_BLENDMODE_NONE) {
199199
alphablit_alpha_sse2_argb_no_surf_alpha(

src_c/base.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -2172,11 +2172,7 @@ static PG_PixelFormatEnum
21722172
pg_GetDefaultConvertFormat(void)
21732173
{
21742174
if (pg_default_screen) {
2175-
#if SDL_VERSION_ATLEAST(3, 0, 0)
2176-
return pg_default_screen->surf->format;
2177-
#else
2178-
return pg_default_screen->surf->format->format;
2179-
#endif
2175+
return PG_SURF_FORMATENUM(pg_default_screen->surf);
21802176
}
21812177
return pg_default_convert_format;
21822178
}

src_c/mask.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -2111,13 +2111,7 @@ draw_to_surface(SDL_Surface *surf, bitmask_t *bitmask, int x_dest, int y_dest,
21112111
static int
21122112
check_surface_pixel_format(SDL_Surface *surf, SDL_Surface *check_surf)
21132113
{
2114-
if ((PG_SURF_BytesPerPixel(surf) != PG_SURF_BytesPerPixel(check_surf)) ||
2115-
(PG_SURF_BitsPerPixel(surf) != PG_SURF_BitsPerPixel(check_surf)) ||
2116-
(surf->format->format != check_surf->format->format)) {
2117-
return 0;
2118-
}
2119-
2120-
return 1;
2114+
return PG_SURF_FORMATENUM(surf) == PG_SURF_FORMATENUM(check_surf);
21212115
}
21222116

21232117
/* Draws a mask on a surface.

src_c/pixelarray_methods.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ _make_surface(pgPixelArrayObject *array, PyObject *args)
131131
* create a new surface with the array dimensions */
132132
if (!same_dims) {
133133
if (!(temp_surf = PG_CreateSurface((int)dim0, (int)dim1,
134-
surf->format->format)))
134+
PG_SURF_FORMATENUM(surf))))
135135
return RAISE(pgExc_SDLError, SDL_GetError());
136136
}
137137

src_c/pixelcopy.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ make_surface(PyObject *self, PyObject *arg)
11701170
pgBuffer_Release(&pg_view);
11711171
return RAISE(pgExc_SDLError, SDL_GetError());
11721172
}
1173-
if (SDL_ISPIXELFORMAT_INDEXED(surf->format->format)) {
1173+
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) {
11741174
/* Give the surface something other than an all white palette.
11751175
* */
11761176
if (SDL_SetPaletteColors(surf->format->palette, default_palette_colors,

src_c/rotozoom.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
584584
/*
585585
* Target surface is 32bit with source RGBA/ABGR ordering
586586
*/
587-
rz_dst = PG_CreateSurface(dstwidth, dstheight, rz_src->format->format);
587+
rz_dst =
588+
PG_CreateSurface(dstwidth, dstheight, PG_SURF_FORMATENUM(rz_src));
588589
if (SDL_HasColorKey(src)) {
589590
SDL_GetColorKey(src, &colorkey);
590591
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
@@ -643,7 +644,8 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
643644
* Target surface is 32bit with source RGBA/ABGR ordering
644645
*/
645646

646-
rz_dst = PG_CreateSurface(dstwidth, dstheight, rz_src->format->format);
647+
rz_dst =
648+
PG_CreateSurface(dstwidth, dstheight, PG_SURF_FORMATENUM(rz_src));
647649
if (SDL_HasColorKey(src)) {
648650
SDL_GetColorKey(src, &colorkey);
649651
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {

src_c/surface.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
686686
}
687687
}
688688

689-
if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
689+
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surface))) {
690690
/* Give the surface something other than an all white palette.
691691
* */
692692
if (SDL_SetPaletteColors(surface->format->palette,
@@ -936,7 +936,7 @@ surf_unmap_rgb(PyObject *self, PyObject *arg)
936936
}
937937
SURF_INIT_CHECK(surf)
938938

939-
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format))
939+
if (SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(surf)))
940940
SDL_GetRGBA(col, surf->format, rgba, rgba + 1, rgba + 2, rgba + 3);
941941
else {
942942
SDL_GetRGB(col, surf->format, rgba, rgba + 1, rgba + 2);
@@ -1109,7 +1109,7 @@ surf_set_palette(PyObject *self, PyObject *seq)
11091109

11101110
pal = surf->format->palette;
11111111

1112-
if (!SDL_ISPIXELFORMAT_INDEXED(surf->format->format))
1112+
if (!SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf)))
11131113
return RAISE(pgExc_SDLError, "Surface colors are not indexed\n");
11141114

11151115
if (!pal)
@@ -1164,7 +1164,7 @@ surf_set_palette_at(PyObject *self, PyObject *args)
11641164
return NULL;
11651165
}
11661166

1167-
if (!SDL_ISPIXELFORMAT_INDEXED(surf->format->format))
1167+
if (!SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf)))
11681168
return RAISE(pgExc_SDLError, "Surface colors are not indexed\n");
11691169

11701170
pal = surf->format->palette;
@@ -1248,7 +1248,7 @@ surf_get_colorkey(pgSurfaceObject *self, PyObject *_null)
12481248

12491249
SDL_GetColorKey(surf, &mapped_color);
12501250

1251-
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format))
1251+
if (SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(surf)))
12521252
SDL_GetRGBA(mapped_color, surf->format, &r, &g, &b, &a);
12531253
else
12541254
SDL_GetRGB(mapped_color, surf->format, &r, &g, &b);
@@ -1314,7 +1314,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
13141314
sdlrect.h = 0;
13151315
sdlrect.w = 0;
13161316

1317-
surface = PG_CreateSurface(1, 1, surf->format->format);
1317+
surface = PG_CreateSurface(1, 1, PG_SURF_FORMATENUM(surf));
13181318

13191319
SDL_LowerBlit(surf, &sdlrect, surface, &sdlrect);
13201320
SDL_FreeSurface(surface);
@@ -1411,7 +1411,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
14111411

14121412
if ((has_colorkey = SDL_HasColorKey(surf))) {
14131413
SDL_GetColorKey(surf, &colorkey);
1414-
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format))
1414+
if (SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(surf)))
14151415
SDL_GetRGBA(colorkey, surf->format, &key_r, &key_g, &key_b,
14161416
&key_a);
14171417
else
@@ -1535,7 +1535,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
15351535
if (SDL_ISPIXELFORMAT_INDEXED(SDL_MasksToPixelFormatEnum(
15361536
PG_FORMAT_BitsPerPixel((&format)), format.Rmask,
15371537
format.Gmask, format.Bmask, format.Amask))) {
1538-
if (SDL_ISPIXELFORMAT_INDEXED(surf->format->format)) {
1538+
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) {
15391539
SDL_SetPixelFormatPalette(&format, surf->format->palette);
15401540
}
15411541
else {
@@ -2768,7 +2768,7 @@ surf_subsurface(PyObject *self, PyObject *args)
27682768
return RAISE(pgExc_SDLError, SDL_GetError());
27692769

27702770
/* copy the colormap if we need it */
2771-
if (SDL_ISPIXELFORMAT_INDEXED(surf->format->format) &&
2771+
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf)) &&
27722772
surf->format->palette) {
27732773
SDL_Color *colors = surf->format->palette->colors;
27742774
int ncolors = surf->format->palette->ncolors;
@@ -4011,7 +4011,7 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
40114011
}
40124012
/* can't blit alpha to 8bit, crashes SDL */
40134013
else if (PG_SURF_BytesPerPixel(dst) == 1 &&
4014-
(SDL_ISPIXELFORMAT_ALPHA(src->format->format) ||
4014+
(SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(src)) ||
40154015
((SDL_GetSurfaceAlphaMod(src, &alpha) == 0 && alpha != 255)))) {
40164016
/* Py_BEGIN_ALLOW_THREADS */
40174017
if (PG_SURF_BytesPerPixel(src) == 1) {
@@ -4057,7 +4057,7 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
40574057
(PG_SURF_BytesPerPixel(dst) == 4 ||
40584058
PG_SURF_BytesPerPixel(dst) == 2) &&
40594059
_PgSurface_SrcAlpha(src) &&
4060-
(SDL_ISPIXELFORMAT_ALPHA(src->format->format)) &&
4060+
(SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(src))) &&
40614061
!PG_SurfaceHasRLE(src) && !PG_SurfaceHasRLE(dst) &&
40624062
!(src->flags & SDL_RLEACCEL) && !(dst->flags & SDL_RLEACCEL)) {
40634063
/* If we have a 32bit source surface with per pixel alpha

src_c/transform.c

+6-15
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ struct _module_state {
4949

5050
#define GETSTATE(m) ((struct _module_state *)PyModule_GetState(m))
5151

52-
#ifdef SCALE_MMX_SUPPORT
53-
#include <SDL_cpuinfo.h>
54-
#endif /* SCALE_MMX_SUPPORT */
55-
5652
void
5753
scale2x(SDL_Surface *src, SDL_Surface *dst);
5854
extern SDL_Surface *
@@ -97,7 +93,7 @@ _PgSurface_SrcAlpha(SDL_Surface *surf);
9793
static int
9894
_PgSurface_SrcAlpha(SDL_Surface *surf)
9995
{
100-
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format)) {
96+
if (SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(surf))) {
10197
SDL_BlendMode mode;
10298
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0) {
10399
return -1;
@@ -129,12 +125,12 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
129125
return (SDL_Surface *)(RAISE(
130126
PyExc_ValueError, "unsupported Surface bit depth for transform"));
131127

132-
newsurf = PG_CreateSurface(width, height, surf->format->format);
128+
newsurf = PG_CreateSurface(width, height, PG_SURF_FORMATENUM(surf));
133129
if (!newsurf)
134130
return (SDL_Surface *)(RAISE(pgExc_SDLError, SDL_GetError()));
135131

136132
/* Copy palette, colorkey, etc info */
137-
if (SDL_ISPIXELFORMAT_INDEXED(surf->format->format)) {
133+
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) {
138134
if (SDL_SetPaletteColors(newsurf->format->palette,
139135
surf->format->palette->colors, 0,
140136
surf->format->palette->ncolors) != 0) {
@@ -451,7 +447,7 @@ scale_to(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int width,
451447
* For example, RGBA and RGBX surfaces are compatible in this way. */
452448
if (retsurf->format->Amask != src->format->Amask) {
453449
modsurf = PG_CreateSurfaceFrom(retsurf->w, retsurf->h,
454-
src->format->format,
450+
PG_SURF_FORMATENUM(src),
455451
retsurf->pixels, retsurf->pitch);
456452
}
457453
}
@@ -2238,8 +2234,7 @@ solid_overlay(pgSurfaceObject *srcobj, Uint32 color, pgSurfaceObject *dstobj,
22382234
"Destination surface must be the same size as source surface."));
22392235
}
22402236

2241-
if (fmt->BytesPerPixel != newsurf->format->BytesPerPixel ||
2242-
fmt->format != newsurf->format->format) {
2237+
if (PG_SURF_FORMATENUM(src) != PG_SURF_FORMATENUM(newsurf)) {
22432238
return (SDL_Surface *)(RAISE(
22442239
PyExc_ValueError,
22452240
"Source and destination surfaces need the same format."));
@@ -2682,11 +2677,7 @@ surf_hsl(PyObject *self, PyObject *args, PyObject *kwargs)
26822677
PyExc_ValueError,
26832678
"Destination surface must be the same size as source surface.");
26842679
}
2685-
if (src->format->Rmask != dst->format->Rmask ||
2686-
src->format->Gmask != dst->format->Gmask ||
2687-
src->format->Bmask != dst->format->Bmask ||
2688-
src->format->Amask != dst->format->Amask ||
2689-
PG_SURF_BytesPerPixel(src) != PG_SURF_BytesPerPixel(dst)) {
2680+
if (PG_SURF_FORMATENUM(src) != PG_SURF_FORMATENUM(dst)) {
26902681
return RAISE(PyExc_ValueError,
26912682
"Source and destination surfaces need the same format.");
26922683
}

0 commit comments

Comments
 (0)