Skip to content

Commit aa02bc6

Browse files
ankith26Starbuck5
authored andcommitted
SDL3 runtime fixes
1 parent ef3ac0c commit aa02bc6

File tree

9 files changed

+197
-127
lines changed

9 files changed

+197
-127
lines changed

docs/reST/c_api/surface.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ Header file: src_c/include/pygame.h
5656
by the blit.
5757
5858
The C version of the :py:meth:`pygame.Surface.blit` method.
59-
Return ``1`` on success, ``0`` on an exception.
59+
Return ``0`` on success, ``1`` on an exception.

src_c/_pygame.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
192192

193193
#define PG_GetSurfaceClipRect SDL_GetSurfaceClipRect
194194

195+
#define PG_GL_SetSwapInterval SDL_GL_SetSwapInterval
196+
195197
#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/
196198
#define PG_ShowCursor() SDL_ShowCursor(SDL_ENABLE)
197199
#define PG_HideCursor() SDL_ShowCursor(SDL_DISABLE)
@@ -400,6 +402,12 @@ PG_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect)
400402
*rect = surface->clip_rect;
401403
return true;
402404
}
405+
406+
static inline bool
407+
PG_GL_SetSwapInterval(int interval)
408+
{
409+
return SDL_GL_SetSwapInterval(interval) == 0;
410+
}
403411
#endif
404412

405413
/* DictProxy is useful for event posting with an arbitrary dict. Maintains

src_c/display.c

Lines changed: 69 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,11 @@ pg_get_init(PyObject *self, PyObject *_null)
279279
static PyObject *
280280
pg_get_active(PyObject *self, PyObject *_null)
281281
{
282-
SDL_WindowFlags flags = SDL_GetWindowFlags(pg_GetDefaultWindow());
282+
SDL_Window *win = pg_GetDefaultWindow();
283+
if (!win) {
284+
Py_RETURN_FALSE;
285+
}
286+
SDL_WindowFlags flags = SDL_GetWindowFlags(win);
283287

284288
#if SDL_VERSION_ATLEAST(3, 0, 0)
285289
return PyBool_FromLong(!(flags & SDL_WINDOW_HIDDEN) &&
@@ -793,33 +797,43 @@ pg_get_surface(PyObject *self, PyObject *_null)
793797
static PyObject *
794798
pg_gl_set_attribute(PyObject *self, PyObject *arg)
795799
{
796-
int flag, value, result;
800+
int flag, value;
797801
VIDEO_INIT_CHECK();
798802
if (!PyArg_ParseTuple(arg, "ii", &flag, &value)) {
799803
return NULL;
800804
}
801805
if (flag == -1) { /*an undefined/unsupported val, ignore*/
802806
Py_RETURN_NONE;
803807
}
804-
result = SDL_GL_SetAttribute(flag, value);
805-
if (result == -1) {
808+
#if SDL_VERSION_ATLEAST(3, 0, 0)
809+
if (!SDL_GL_SetAttribute(flag, value)) {
806810
return RAISE(pgExc_SDLError, SDL_GetError());
807811
}
812+
#else
813+
if (SDL_GL_SetAttribute(flag, value) == -1) {
814+
return RAISE(pgExc_SDLError, SDL_GetError());
815+
}
816+
#endif
808817
Py_RETURN_NONE;
809818
}
810819

811820
static PyObject *
812821
pg_gl_get_attribute(PyObject *self, PyObject *arg)
813822
{
814-
int flag, value, result;
823+
int flag, value;
815824
VIDEO_INIT_CHECK();
816825
if (!PyArg_ParseTuple(arg, "i", &flag)) {
817826
return NULL;
818827
}
819-
result = SDL_GL_GetAttribute(flag, &value);
820-
if (result == -1) {
828+
#if SDL_VERSION_ATLEAST(3, 0, 0)
829+
if (!SDL_GL_GetAttribute(flag, &value)) {
830+
return RAISE(pgExc_SDLError, SDL_GetError());
831+
}
832+
#else
833+
if (SDL_GL_GetAttribute(flag, &value) == -1) {
821834
return RAISE(pgExc_SDLError, SDL_GetError());
822835
}
836+
#endif
823837
return PyLong_FromLong(value);
824838
}
825839

@@ -1118,12 +1132,12 @@ PG_CreateWindowCompat(const char *title, int x, int y, int w, int h,
11181132
}
11191133

11201134
#if SDL_VERSION_ATLEAST(3, 0, 0)
1121-
/* Returns 0 on success, negative on failure. */
1122-
static int
1135+
/* Returns true on success, false on failure. */
1136+
static bool
11231137
PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
11241138
bool non_desktop_fullscreen)
11251139
{
1126-
int ret = -1;
1140+
bool ret = false;
11271141
SDL_DisplayMode **modes = NULL;
11281142
SDL_DisplayMode *chosen_mode = NULL;
11291143
if (!SDL_SetWindowFullscreen(window, fullscreen)) {
@@ -1152,11 +1166,23 @@ PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
11521166
}
11531167
}
11541168

1155-
ret = 0;
1169+
ret = true;
11561170
end:
11571171
SDL_free(modes);
11581172
return ret;
11591173
}
1174+
#else
1175+
static bool
1176+
PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
1177+
bool non_desktop_fullscreen)
1178+
{
1179+
int flags = 0;
1180+
if (fullscreen) {
1181+
flags = non_desktop_fullscreen ? SDL_WINDOW_FULLSCREEN
1182+
: SDL_WINDOW_FULLSCREEN_DESKTOP;
1183+
}
1184+
return (SDL_SetWindowFullscreen(window, flags) == 0);
1185+
}
11601186
#endif
11611187

11621188
static PyObject *
@@ -1183,7 +1209,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
11831209
char *title = state->title;
11841210
const char *scale_env, *winid_env;
11851211
#if SDL_VERSION_ATLEAST(3, 0, 0)
1186-
int non_desktop_fullscreen = 0;
1212+
int non_desktop_fullscreen = 1;
11871213
#endif
11881214

11891215
char *keywords[] = {"size", "flags", "depth", "display", "vsync", NULL};
@@ -1308,7 +1334,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
13081334
if (flags & PGS_SCALED) {
13091335
#if SDL_VERSION_ATLEAST(3, 0, 0)
13101336
sdl_flags |= SDL_WINDOW_FULLSCREEN;
1311-
non_desktop_fullscreen = 1;
1337+
non_desktop_fullscreen = 0;
13121338
#else
13131339
sdl_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
13141340
#endif
@@ -1318,7 +1344,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
13181344
Borderless fullscreen is preferred when possible */
13191345
#if SDL_VERSION_ATLEAST(3, 0, 0)
13201346
sdl_flags |= SDL_WINDOW_FULLSCREEN;
1321-
non_desktop_fullscreen = 1;
1347+
non_desktop_fullscreen = 0;
13221348
#else
13231349
sdl_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
13241350
#endif
@@ -1437,11 +1463,16 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
14371463
if (flags & PGS_SCALED && !(flags & PGS_FULLSCREEN)) {
14381464
SDL_Rect display_bounds;
14391465
int fractional_scaling = SDL_FALSE;
1440-
1466+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1467+
if (!SDL_GetDisplayUsableBounds(display, &display_bounds)) {
1468+
return RAISE(pgExc_SDLError, SDL_GetError());
1469+
}
1470+
#else
14411471
if (0 !=
14421472
SDL_GetDisplayUsableBounds(display, &display_bounds)) {
14431473
return RAISE(pgExc_SDLError, SDL_GetError());
14441474
}
1475+
#endif
14451476

14461477
if (SDL_GetHintBoolean("SDL_HINT_RENDER_SCALE_QUALITY",
14471478
SDL_FALSE)) {
@@ -1530,9 +1561,9 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
15301561
* changes if the window is fullscreen
15311562
* See https://github.com/pygame/pygame/issues/2711 */
15321563
#if SDL_VERSION_ATLEAST(3, 0, 0)
1533-
if (0 != PG_SetWindowFullscreen(
1534-
win, sdl_flags & SDL_WINDOW_FULLSCREEN,
1535-
non_desktop_fullscreen)) {
1564+
if (!PG_SetWindowFullscreen(win,
1565+
sdl_flags & SDL_WINDOW_FULLSCREEN,
1566+
non_desktop_fullscreen)) {
15361567
return RAISE(pgExc_SDLError, SDL_GetError());
15371568
}
15381569
#else
@@ -1592,7 +1623,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
15921623
vsync to be always on or always off, or vsync is on by default
15931624
for the whole desktop because of wayland GL compositing. */
15941625
if (vsync == -1) {
1595-
if (SDL_GL_SetSwapInterval(-1) != 0) {
1626+
if (!PG_GL_SetSwapInterval(-1)) {
15961627
PyErr_SetString(pgExc_SDLError,
15971628
"adaptive vsync for OpenGL not "
15981629
"available");
@@ -1602,7 +1633,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
16021633
}
16031634
}
16041635
else if (vsync == 1) {
1605-
if (SDL_GL_SetSwapInterval(1) != 0) {
1636+
if (!PG_GL_SetSwapInterval(1)) {
16061637
PyErr_SetString(pgExc_SDLError,
16071638
"regular vsync for OpenGL not "
16081639
"available");
@@ -1611,7 +1642,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
16111642
}
16121643
}
16131644
else {
1614-
SDL_GL_SetSwapInterval(0);
1645+
PG_GL_SetSwapInterval(0);
16151646
}
16161647
}
16171648
else {
@@ -1686,7 +1717,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
16861717
#if SDL_VERSION_ATLEAST(3, 0, 0)
16871718
int has_vsync = 0;
16881719
SDL_GetRenderVSync(pg_renderer, &has_vsync);
1689-
if (vsync && has_vsync) {
1720+
if (vsync && !has_vsync) {
16901721
PyErr_SetString(pgExc_SDLError,
16911722
"could not enable vsync");
16921723
_display_state_cleanup(state);
@@ -3030,7 +3061,6 @@ static PyObject *
30303061
pg_toggle_fullscreen(PyObject *self, PyObject *_null)
30313062
{
30323063
SDL_Window *win = pg_GetDefaultWindow();
3033-
int result;
30343064
SDL_WindowFlags flags;
30353065
int window_w, window_h, w, h, window_display, x, y;
30363066
pgSurfaceObject *display_surface;
@@ -3159,8 +3189,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
31593189
/* TOGGLE FULLSCREEN OFF */
31603190

31613191
if (state->unscaled_render) {
3162-
result = SDL_SetWindowFullscreen(win, 0);
3163-
if (result != 0) {
3192+
if (!PG_SetWindowFullscreen(win, false, false)) {
31643193
return RAISE(pgExc_SDLError, SDL_GetError());
31653194
}
31663195
}
@@ -3174,8 +3203,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
31743203
if (scale < 1) {
31753204
scale = 1;
31763205
}
3177-
result = SDL_SetWindowFullscreen(win, 0);
3178-
if (result != 0) {
3206+
if (!PG_SetWindowFullscreen(win, false, false)) {
31793207
return RAISE(pgExc_SDLError, SDL_GetError());
31803208
}
31813209
SDL_SetWindowSize(win, w * scale, h * scale);
@@ -3215,8 +3243,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32153243
/* this is literally the only place where state->toggle_windowed_w
32163244
* should ever be read. We only use it because with GL, there is no
32173245
* display surface we can query for dimensions. */
3218-
result = SDL_SetWindowFullscreen(win, 0);
3219-
if (result != 0) {
3246+
if (!PG_SetWindowFullscreen(win, false, false)) {
32203247
return RAISE(pgExc_SDLError, SDL_GetError());
32213248
}
32223249
SDL_GL_MakeCurrent(win, state->gl_context);
@@ -3252,8 +3279,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32523279
else if ((flags & SDL_WINDOW_FULLSCREEN_DESKTOP) ==
32533280
SDL_WINDOW_FULLSCREEN_DESKTOP) {
32543281
#endif
3255-
result = SDL_SetWindowFullscreen(win, 0);
3256-
if (result != 0) {
3282+
if (!PG_SetWindowFullscreen(win, false, false)) {
32573283
return RAISE(pgExc_SDLError, SDL_GetError());
32583284
}
32593285
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3282,15 +3308,11 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32823308
if (win == NULL) {
32833309
return RAISE(pgExc_SDLError, SDL_GetError());
32843310
}
3285-
else {
3286-
result = 0;
3287-
}
32883311
display_surface->surf = SDL_GetWindowSurface(win);
32893312
pg_SetDefaultWindow(win);
32903313
}
32913314
else {
3292-
result = SDL_SetWindowFullscreen(win, 0);
3293-
if (result != 0) {
3315+
if (!PG_SetWindowFullscreen(win, false, false)) {
32943316
return RAISE(pgExc_SDLError, SDL_GetError());
32953317
}
32963318
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3325,24 +3347,12 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
33253347
state->fullscreen_backup_y = y;
33263348

33273349
if (state->unscaled_render) {
3328-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3329-
result = PG_SetWindowFullscreen(win, 1, 0);
3330-
#else
3331-
result =
3332-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3333-
#endif
3334-
if (result != 0) {
3350+
if (!PG_SetWindowFullscreen(win, true, false)) {
33353351
return RAISE(pgExc_SDLError, SDL_GetError());
33363352
}
33373353
}
33383354
else if (pg_renderer != NULL) {
3339-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3340-
result = PG_SetWindowFullscreen(win, 1, 0);
3341-
#else
3342-
result =
3343-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3344-
#endif
3345-
if (result != 0) {
3355+
if (!PG_SetWindowFullscreen(win, true, false)) {
33463356
return RAISE(pgExc_SDLError, SDL_GetError());
33473357
}
33483358
if (is_renderer_software && subsystem == SDL_SYSWM_X11) {
@@ -3375,13 +3385,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
33753385
#endif
33763386
}
33773387
else if (state->using_gl) {
3378-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3379-
result = PG_SetWindowFullscreen(win, 1, 0);
3380-
#else
3381-
result =
3382-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3383-
#endif
3384-
if (result != 0) {
3388+
if (!PG_SetWindowFullscreen(win, true, false)) {
33853389
return RAISE(pgExc_SDLError, SDL_GetError());
33863390
}
33873391
SDL_GL_MakeCurrent(win, state->gl_context);
@@ -3406,13 +3410,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34063410
}
34073411
}
34083412
else if (w == display_mode->w && h == display_mode->h) {
3409-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3410-
result = PG_SetWindowFullscreen(win, 1, 0);
3411-
#else
3412-
result =
3413-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3414-
#endif
3415-
if (result != 0) {
3413+
if (!PG_SetWindowFullscreen(win, true, false)) {
34163414
return RAISE(pgExc_SDLError, SDL_GetError());
34173415
}
34183416
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3429,8 +3427,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34293427
return PyLong_FromLong(-1);
34303428
}
34313429
else {
3432-
result = SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN);
3433-
if (result != 0) {
3430+
if (!PG_SetWindowFullscreen(win, true, true)) {
34343431
return RAISE(pgExc_SDLError, SDL_GetError());
34353432
}
34363433
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3442,7 +3439,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34423439
if (win == NULL) {
34433440
return RAISE(pgExc_SDLError, SDL_GetError());
34443441
}
3445-
if (0 != SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN)) {
3442+
if (!PG_SetWindowFullscreen(win, true, true)) {
34463443
return RAISE(pgExc_SDLError, SDL_GetError());
34473444
}
34483445
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3456,7 +3453,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34563453
}
34573454
}
34583455
}
3459-
return PyLong_FromLong(result != 0);
3456+
return PyLong_FromLong(1);
34603457
}
34613458

34623459
/* This API is provisional, and, not finalised, and should not be documented
@@ -3766,7 +3763,11 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs)
37663763

37673764
int clicked_button_id;
37683765

3766+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3767+
if (!SDL_ShowMessageBox(&msgbox_data, &clicked_button_id)) {
3768+
#else
37693769
if (SDL_ShowMessageBox(&msgbox_data, &clicked_button_id)) {
3770+
#endif
37703771
PyErr_SetString(pgExc_SDLError, SDL_GetError());
37713772
goto error;
37723773
}

0 commit comments

Comments
 (0)