Skip to content

Commit cc260b3

Browse files
committed
Adjust pg_(Get/Set)DefaultConvertFormat for SDL3
This reworks the functions to work on pixel format enums instead of structs, because in SDL3 the enums are much easier to get than the structs. This required involved changes to surface.c, but I think the previous mask logic is much clearer now that it is expressed using format enums.
1 parent 5827924 commit cc260b3

File tree

6 files changed

+44
-59
lines changed

6 files changed

+44
-59
lines changed

src_c/_pygame.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@
7575
#define PG_CreateSurface SDL_CreateSurface
7676
#define PG_CreateSurfaceFrom SDL_CreateSurfaceFrom
7777
#define PG_ConvertSurface SDL_ConvertSurface
78-
#define PG_ConvertSurfaceFormat SDL_ConvertSurfaceFormat
78+
#define PG_ConvertSurfaceFormat SDL_ConvertSurface
79+
80+
#define PG_PixelFormatEnum SDL_PixelFormat
7981

8082
#define PG_SurfaceHasRLE SDL_SurfaceHasRLE
8183

@@ -146,6 +148,8 @@ PG_UnlockMutex(SDL_mutex *mutex)
146148
#define PG_ConvertSurfaceFormat(src, pixel_format) \
147149
SDL_ConvertSurfaceFormat(src, pixel_format, 0)
148150

151+
#define PG_PixelFormatEnum SDL_PixelFormatEnum
152+
149153
#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \
150154
SDL_SoftStretch(src, srcrect, dst, dstrect)
151155

src_c/base.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,25 +2075,25 @@ pg_SetDefaultWindowSurface(pgSurfaceObject *screen)
20752075
pg_default_screen = screen;
20762076
}
20772077

2078-
SDL_PixelFormat *pg_default_convert_format = NULL;
2078+
PG_PixelFormatEnum pg_default_convert_format = 0;
20792079

2080-
static SDL_PixelFormat *
2080+
static PG_PixelFormatEnum
20812081
pg_GetDefaultConvertFormat(void)
20822082
{
20832083
if (pg_default_screen) {
2084+
#if SDL_VERSION_ATLEAST(3, 0, 0)
20842085
return pg_default_screen->surf->format;
2086+
#else
2087+
return pg_default_screen->surf->format->format;
2088+
#endif
20852089
}
20862090
return pg_default_convert_format;
20872091
}
20882092

2089-
static SDL_PixelFormat *
2090-
pg_SetDefaultConvertFormat(Uint32 format)
2093+
static void
2094+
pg_SetDefaultConvertFormat(PG_PixelFormatEnum format)
20912095
{
2092-
if (pg_default_convert_format != NULL) {
2093-
SDL_FreeFormat(pg_default_convert_format);
2094-
}
2095-
pg_default_convert_format = SDL_AllocFormat(format);
2096-
return pg_default_convert_format; // returns for NULL error checking
2096+
pg_default_convert_format = format;
20972097
}
20982098

20992099
static int

src_c/include/_pygame.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ typedef struct pg_bufferinfo_s {
184184
(*(int (*)(void))PYGAMEAPI_GET_SLOT(base, 23))
185185

186186
#define pg_GetDefaultConvertFormat \
187-
(*(SDL_PixelFormat * (*)(void)) PYGAMEAPI_GET_SLOT(base, 27))
187+
(*(PG_PixelFormatEnum(*)(void))PYGAMEAPI_GET_SLOT(base, 27))
188188

189189
#define pg_SetDefaultConvertFormat \
190-
(*(SDL_PixelFormat * (*)(Uint32)) PYGAMEAPI_GET_SLOT(base, 28))
190+
(*(void (*)(Uint32))PYGAMEAPI_GET_SLOT(base, 28))
191191

192192
#define import_pygame_base() IMPORT_PYGAME_MODULE(base)
193193
#endif /* ~PYGAMEAPI_BASE_INTERNAL */

src_c/meson.build

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# first the "required" modules
22

3-
# TODO: support SDL3
4-
if sdl_api != 3
53
base = py.extension_module(
64
'base',
75
'base.c',
@@ -10,7 +8,6 @@ base = py.extension_module(
108
install: true,
119
subdir: pg,
1210
)
13-
endif
1411

1512
color = py.extension_module(
1613
'color',

src_c/surface.c

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,66 +1582,54 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
15821582
static SDL_Surface *
15831583
pg_DisplayFormat(SDL_Surface *surface)
15841584
{
1585-
SDL_PixelFormat *default_format = pg_GetDefaultConvertFormat();
1585+
PG_PixelFormatEnum default_format = pg_GetDefaultConvertFormat();
15861586
if (!default_format) {
15871587
SDL_SetError(
15881588
"No convert format has been set, try display.set_mode()"
15891589
" or Window.get_surface().");
15901590
return NULL;
15911591
}
1592-
return PG_ConvertSurface(surface, default_format);
1592+
return PG_ConvertSurfaceFormat(surface, default_format);
15931593
}
15941594

15951595
static SDL_Surface *
15961596
pg_DisplayFormatAlpha(SDL_Surface *surface)
15971597
{
1598-
SDL_PixelFormat *dformat;
1599-
Uint32 pfe;
1600-
Uint32 amask = 0xff000000;
1601-
Uint32 rmask = 0x00ff0000;
1602-
Uint32 gmask = 0x0000ff00;
1603-
Uint32 bmask = 0x000000ff;
1604-
1605-
dformat = pg_GetDefaultConvertFormat();
1598+
PG_PixelFormatEnum pfe = SDL_PIXELFORMAT_ARGB8888;
1599+
PG_PixelFormatEnum dformat = pg_GetDefaultConvertFormat();
16061600
if (!dformat) {
16071601
SDL_SetError(
16081602
"No convert format has been set, try display.set_mode()"
16091603
" or Window.get_surface().");
16101604
return NULL;
16111605
}
16121606

1613-
switch (PG_FORMAT_BytesPerPixel(dformat)) {
1614-
case 2:
1615-
/* same behavior as SDL1 */
1616-
if ((dformat->Rmask == 0x1f) &&
1617-
(dformat->Bmask == 0xf800 || dformat->Bmask == 0x7c00)) {
1618-
rmask = 0xff;
1619-
bmask = 0xff0000;
1620-
}
1607+
switch (dformat) {
1608+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1609+
case SDL_PIXELFORMAT_XBGR1555:
1610+
#else
1611+
case SDL_PIXELFORMAT_BGR555:
1612+
#endif
1613+
case SDL_PIXELFORMAT_ABGR1555:
1614+
case SDL_PIXELFORMAT_BGR565:
1615+
case PG_PIXELFORMAT_XBGR8888:
1616+
case SDL_PIXELFORMAT_ABGR8888:
1617+
pfe = SDL_PIXELFORMAT_ABGR8888;
16211618
break;
1622-
case 3:
1623-
case 4:
1624-
/* keep the format if the high bits are free */
1625-
if ((dformat->Rmask == 0xff) && (dformat->Bmask == 0xff0000)) {
1626-
rmask = 0xff;
1627-
bmask = 0xff0000;
1628-
}
1629-
else if (dformat->Rmask == 0xff00 &&
1630-
(dformat->Bmask == 0xff000000)) {
1631-
amask = 0x000000ff;
1632-
rmask = 0x0000ff00;
1633-
gmask = 0x00ff0000;
1634-
bmask = 0xff000000;
1635-
}
1619+
1620+
case SDL_PIXELFORMAT_BGRX8888:
1621+
case SDL_PIXELFORMAT_BGRA8888:
1622+
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
1623+
case SDL_PIXELFORMAT_BGR24:
1624+
#else
1625+
case SDL_PIXELFORMAT_RGB24:
1626+
#endif
1627+
pfe = SDL_PIXELFORMAT_BGRA8888;
16361628
break;
1637-
default: /* ARGB8888 */
1629+
1630+
default:
16381631
break;
16391632
}
1640-
pfe = SDL_MasksToPixelFormatEnum(32, rmask, gmask, bmask, amask);
1641-
if (pfe == SDL_PIXELFORMAT_UNKNOWN) {
1642-
SDL_SetError("unknown pixel format");
1643-
return NULL;
1644-
}
16451633
return PG_ConvertSurfaceFormat(surface, pfe);
16461634
}
16471635

src_c/window.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,8 @@ window_get_surface(pgWindowObject *self, PyObject *_null)
157157
return RAISE(pgExc_SDLError, SDL_GetError());
158158
}
159159

160-
if (pg_GetDefaultConvertFormat() == NULL) {
161-
if (pg_SetDefaultConvertFormat(_surf->format->format) == NULL) {
162-
/* This is very unlikely, I think only would happen if SDL runs
163-
* out of memory when allocating the format. */
164-
return RAISE(pgExc_SDLError, SDL_GetError());
165-
}
160+
if (pg_GetDefaultConvertFormat() == 0) {
161+
pg_SetDefaultConvertFormat(_surf->format->format);
166162
}
167163

168164
if (self->surf == NULL) {

0 commit comments

Comments
 (0)