Skip to content

Commit 4f587cc

Browse files
authored
Merge pull request #3171 from Starbuck5/start-base-sdl3
Start getting base module ready for SDL3
2 parents acd5553 + cc260b3 commit 4f587cc

File tree

6 files changed

+55
-66
lines changed

6 files changed

+55
-66
lines changed

src_c/_pygame.h

Lines changed: 9 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

@@ -115,6 +117,8 @@ PG_UnlockMutex(SDL_mutex *mutex)
115117
#define PG_FIND_VNUM_MINOR(ver) SDL_VERSIONNUM_MINOR(ver)
116118
#define PG_FIND_VNUM_MICRO(ver) SDL_VERSIONNUM_MICRO(ver)
117119

120+
#define PG_INIT_TIMER 0
121+
118122
#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/
119123
#define PG_ShowCursor() SDL_ShowCursor(SDL_ENABLE)
120124
#define PG_HideCursor() SDL_ShowCursor(SDL_DISABLE)
@@ -144,6 +148,8 @@ PG_UnlockMutex(SDL_mutex *mutex)
144148
#define PG_ConvertSurfaceFormat(src, pixel_format) \
145149
SDL_ConvertSurfaceFormat(src, pixel_format, 0)
146150

151+
#define PG_PixelFormatEnum SDL_PixelFormatEnum
152+
147153
#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \
148154
SDL_SoftStretch(src, srcrect, dst, dstrect)
149155

@@ -180,6 +186,8 @@ PG_UnlockMutex(SDL_mutex *mutex)
180186
#define PG_FIND_VNUM_MINOR(ver) ver.minor
181187
#define PG_FIND_VNUM_MICRO(ver) ver.patch
182188

189+
#define PG_INIT_TIMER SDL_INIT_TIMER
190+
183191
#if SDL_VERSION_ATLEAST(2, 0, 14)
184192
#define PG_SurfaceHasRLE SDL_HasSurfaceRLE
185193
#else

src_c/base.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int pg_is_init = 0;
8181
static int pg_sdl_was_init = 0;
8282
SDL_Window *pg_default_window = NULL;
8383
pgSurfaceObject *pg_default_screen = NULL;
84-
static char *pg_env_blend_alpha_SDL2 = NULL;
84+
static int pg_env_blend_alpha_SDL2 = 0;
8585

8686
static void
8787
pg_install_parachute(void);
@@ -170,7 +170,7 @@ static pgSurfaceObject *
170170
pg_GetDefaultWindowSurface(void);
171171
static void
172172
pg_SetDefaultWindowSurface(pgSurfaceObject *);
173-
static char *
173+
static int
174174
pg_EnvShouldBlendAlphaSDL2(void);
175175

176176
/* compare compiled to linked, raise python error on incompatibility */
@@ -341,13 +341,13 @@ pg_init(PyObject *self, PyObject *_null)
341341

342342
/*nice to initialize timer, so startup time will reflec pg_init() time*/
343343
#if defined(WITH_THREAD) && !defined(MS_WIN32) && defined(SDL_INIT_EVENTTHREAD)
344-
pg_sdl_was_init = SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_TIMER |
344+
pg_sdl_was_init = SDL_Init(SDL_INIT_EVENTTHREAD | PG_INIT_TIMER |
345345
PG_INIT_NOPARACHUTE) == 0;
346346
#else
347-
pg_sdl_was_init = SDL_Init(SDL_INIT_TIMER | PG_INIT_NOPARACHUTE) == 0;
347+
pg_sdl_was_init = SDL_Init(PG_INIT_TIMER | PG_INIT_NOPARACHUTE) == 0;
348348
#endif
349349

350-
pg_env_blend_alpha_SDL2 = SDL_getenv("PYGAME_BLEND_ALPHA_SDL2");
350+
pg_env_blend_alpha_SDL2 = SDL_getenv("PYGAME_BLEND_ALPHA_SDL2") != NULL;
351351

352352
/* initialize all pygame modules */
353353
for (i = 0; modnames[i]; i++) {
@@ -2075,28 +2075,28 @@ 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

2099-
static char *
2099+
static int
21002100
pg_EnvShouldBlendAlphaSDL2(void)
21012101
{
21022102
return pg_env_blend_alpha_SDL2;

src_c/include/_pygame.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ typedef struct pg_bufferinfo_s {
181181
(*(void (*)(pgSurfaceObject *))PYGAMEAPI_GET_SLOT(base, 22))
182182

183183
#define pg_EnvShouldBlendAlphaSDL2 \
184-
(*(char *(*)(void))PYGAMEAPI_GET_SLOT(base, 23))
184+
(*(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)