Skip to content

Commit 1f49d26

Browse files
committed
SDL3: display: runtime issues fixes
1 parent 138b114 commit 1f49d26

File tree

1 file changed

+78
-55
lines changed

1 file changed

+78
-55
lines changed

src_c/display.c

Lines changed: 78 additions & 55 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) &&
@@ -460,7 +464,12 @@ pg_GetVideoInfo(pg_VideoInfo *info)
460464
}
461465
else {
462466
#if SDL_VERSION_ATLEAST(3, 0, 0)
463-
if ((mode_ptr = SDL_GetCurrentDisplayMode(0))) {
467+
SDL_DisplayID primary_display = SDL_GetPrimaryDisplay();
468+
if (primary_display == 0) {
469+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
470+
return (pg_VideoInfo *)NULL;
471+
}
472+
if ((mode_ptr = SDL_GetCurrentDisplayMode(primary_display))) {
464473
info->current_w = mode_ptr->w;
465474
info->current_h = mode_ptr->h;
466475
formatenum = mode_ptr->format;
@@ -1118,12 +1127,12 @@ PG_CreateWindowCompat(const char *title, int x, int y, int w, int h,
11181127
}
11191128

11201129
#if SDL_VERSION_ATLEAST(3, 0, 0)
1121-
/* Returns 0 on success, negative on failure. */
1122-
static int
1130+
/* Returns true on success, false on failure. */
1131+
static bool
11231132
PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
11241133
bool non_desktop_fullscreen)
11251134
{
1126-
int ret = -1;
1135+
bool ret = false;
11271136
SDL_DisplayMode **modes = NULL;
11281137
SDL_DisplayMode *chosen_mode = NULL;
11291138
if (!SDL_SetWindowFullscreen(window, fullscreen)) {
@@ -1152,11 +1161,23 @@ PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
11521161
}
11531162
}
11541163

1155-
ret = 0;
1164+
ret = true;
11561165
end:
11571166
SDL_free(modes);
11581167
return ret;
11591168
}
1169+
#else
1170+
static bool
1171+
PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
1172+
bool non_desktop_fullscreen)
1173+
{
1174+
int flags = 0;
1175+
if (fullscreen) {
1176+
flags = non_desktop_fullscreen ? SDL_WINDOW_FULLSCREEN
1177+
: SDL_WINDOW_FULLSCREEN_DESKTOP;
1178+
}
1179+
return (SDL_SetWindowFullscreen(window, flags) == 0) ? true : false;
1180+
}
11601181
#endif
11611182

11621183
static PyObject *
@@ -1235,6 +1256,16 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
12351256
}
12361257
}
12371258

1259+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1260+
/* In SDL2, display == 0 meant primary display, so compat code for it */
1261+
if (display == 0) {
1262+
display = SDL_GetPrimaryDisplay();
1263+
if (display == 0) {
1264+
return RAISE(pgExc_SDLError, SDL_GetError());
1265+
}
1266+
}
1267+
#endif
1268+
12381269
if ((vsync == -1) && ((flags & PGS_OPENGL) == 0)) {
12391270
return RAISE(PyExc_ValueError,
12401271
"requested adaptive vsync without OpenGL");
@@ -1437,11 +1468,16 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
14371468
if (flags & PGS_SCALED && !(flags & PGS_FULLSCREEN)) {
14381469
SDL_Rect display_bounds;
14391470
int fractional_scaling = SDL_FALSE;
1440-
1471+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1472+
if (!SDL_GetDisplayUsableBounds(display, &display_bounds)) {
1473+
return RAISE(pgExc_SDLError, SDL_GetError());
1474+
}
1475+
#else
14411476
if (0 !=
14421477
SDL_GetDisplayUsableBounds(display, &display_bounds)) {
14431478
return RAISE(pgExc_SDLError, SDL_GetError());
14441479
}
1480+
#endif
14451481

14461482
if (SDL_GetHintBoolean("SDL_HINT_RENDER_SCALE_QUALITY",
14471483
SDL_FALSE)) {
@@ -1973,7 +2009,16 @@ pg_mode_ok(PyObject *self, PyObject *args, PyObject *kwds)
19732009
&display_index)) {
19742010
return NULL;
19752011
}
1976-
#if !SDL_VERSION_ATLEAST(3, 0, 0)
2012+
2013+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2014+
/* In SDL2, display == 0 meant primary display, so compat code for it */
2015+
if (display_index == 0) {
2016+
display_index = SDL_GetPrimaryDisplay();
2017+
if (display_index == 0) {
2018+
return RAISE(pgExc_SDLError, SDL_GetError());
2019+
}
2020+
}
2021+
#else
19772022
/* Display ID is not bounded by number of displays in SDL3 */
19782023
if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) {
19792024
return RAISE(PyExc_ValueError,
@@ -2039,7 +2084,15 @@ pg_list_modes(PyObject *self, PyObject *args, PyObject *kwds)
20392084
return NULL;
20402085
}
20412086

2042-
#if !SDL_VERSION_ATLEAST(3, 0, 0)
2087+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2088+
/* In SDL2, display == 0 meant primary display, so compat code for it */
2089+
if (display_index == 0) {
2090+
display_index = SDL_GetPrimaryDisplay();
2091+
if (display_index == 0) {
2092+
return RAISE(pgExc_SDLError, SDL_GetError());
2093+
}
2094+
}
2095+
#else
20432096
/* Display ID is not bounded by number of displays in SDL3 */
20442097
if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) {
20452098
return RAISE(PyExc_ValueError,
@@ -3035,7 +3088,6 @@ static PyObject *
30353088
pg_toggle_fullscreen(PyObject *self, PyObject *_null)
30363089
{
30373090
SDL_Window *win = pg_GetDefaultWindow();
3038-
int result;
30393091
SDL_WindowFlags flags;
30403092
int window_w, window_h, w, h, window_display, x, y;
30413093
pgSurfaceObject *display_surface;
@@ -3164,8 +3216,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
31643216
/* TOGGLE FULLSCREEN OFF */
31653217

31663218
if (state->unscaled_render) {
3167-
result = SDL_SetWindowFullscreen(win, 0);
3168-
if (result != 0) {
3219+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
31693220
return RAISE(pgExc_SDLError, SDL_GetError());
31703221
}
31713222
}
@@ -3179,8 +3230,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
31793230
if (scale < 1) {
31803231
scale = 1;
31813232
}
3182-
result = SDL_SetWindowFullscreen(win, 0);
3183-
if (result != 0) {
3233+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
31843234
return RAISE(pgExc_SDLError, SDL_GetError());
31853235
}
31863236
SDL_SetWindowSize(win, w * scale, h * scale);
@@ -3220,8 +3270,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32203270
/* this is literally the only place where state->toggle_windowed_w
32213271
* should ever be read. We only use it because with GL, there is no
32223272
* display surface we can query for dimensions. */
3223-
result = SDL_SetWindowFullscreen(win, 0);
3224-
if (result != 0) {
3273+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
32253274
return RAISE(pgExc_SDLError, SDL_GetError());
32263275
}
32273276
SDL_GL_MakeCurrent(win, state->gl_context);
@@ -3257,8 +3306,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32573306
else if ((flags & SDL_WINDOW_FULLSCREEN_DESKTOP) ==
32583307
SDL_WINDOW_FULLSCREEN_DESKTOP) {
32593308
#endif
3260-
result = SDL_SetWindowFullscreen(win, 0);
3261-
if (result != 0) {
3309+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
32623310
return RAISE(pgExc_SDLError, SDL_GetError());
32633311
}
32643312
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3287,15 +3335,11 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32873335
if (win == NULL) {
32883336
return RAISE(pgExc_SDLError, SDL_GetError());
32893337
}
3290-
else {
3291-
result = 0;
3292-
}
32933338
display_surface->surf = SDL_GetWindowSurface(win);
32943339
pg_SetDefaultWindow(win);
32953340
}
32963341
else {
3297-
result = SDL_SetWindowFullscreen(win, 0);
3298-
if (result != 0) {
3342+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
32993343
return RAISE(pgExc_SDLError, SDL_GetError());
33003344
}
33013345
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3330,24 +3374,12 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
33303374
state->fullscreen_backup_y = y;
33313375

33323376
if (state->unscaled_render) {
3333-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3334-
result = PG_SetWindowFullscreen(win, 1, 0);
3335-
#else
3336-
result =
3337-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3338-
#endif
3339-
if (result != 0) {
3377+
if (!PG_SetWindowFullscreen(win, 1, 0)) {
33403378
return RAISE(pgExc_SDLError, SDL_GetError());
33413379
}
33423380
}
33433381
else if (pg_renderer != NULL) {
3344-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3345-
result = PG_SetWindowFullscreen(win, 1, 0);
3346-
#else
3347-
result =
3348-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3349-
#endif
3350-
if (result != 0) {
3382+
if (!PG_SetWindowFullscreen(win, 1, 0)) {
33513383
return RAISE(pgExc_SDLError, SDL_GetError());
33523384
}
33533385
if (is_renderer_software && subsystem == SDL_SYSWM_X11) {
@@ -3380,13 +3412,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
33803412
#endif
33813413
}
33823414
else if (state->using_gl) {
3383-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3384-
result = PG_SetWindowFullscreen(win, 1, 0);
3385-
#else
3386-
result =
3387-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3388-
#endif
3389-
if (result != 0) {
3415+
if (!PG_SetWindowFullscreen(win, 1, 0)) {
33903416
return RAISE(pgExc_SDLError, SDL_GetError());
33913417
}
33923418
SDL_GL_MakeCurrent(win, state->gl_context);
@@ -3411,13 +3437,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34113437
}
34123438
}
34133439
else if (w == display_mode->w && h == display_mode->h) {
3414-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3415-
result = PG_SetWindowFullscreen(win, 1, 0);
3416-
#else
3417-
result =
3418-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3419-
#endif
3420-
if (result != 0) {
3440+
if (!PG_SetWindowFullscreen(win, 1, 0)) {
34213441
return RAISE(pgExc_SDLError, SDL_GetError());
34223442
}
34233443
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3434,8 +3454,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34343454
return PyLong_FromLong(-1);
34353455
}
34363456
else {
3437-
result = SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN);
3438-
if (result != 0) {
3457+
if (!PG_SetWindowFullscreen(win, 1, 1)) {
34393458
return RAISE(pgExc_SDLError, SDL_GetError());
34403459
}
34413460
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3447,7 +3466,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34473466
if (win == NULL) {
34483467
return RAISE(pgExc_SDLError, SDL_GetError());
34493468
}
3450-
if (0 != SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN)) {
3469+
if (!PG_SetWindowFullscreen(win, 1, 1)) {
34513470
return RAISE(pgExc_SDLError, SDL_GetError());
34523471
}
34533472
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3461,7 +3480,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34613480
}
34623481
}
34633482
}
3464-
return PyLong_FromLong(result != 0);
3483+
return PyLong_FromLong(1);
34653484
}
34663485

34673486
/* This API is provisional, and, not finalised, and should not be documented
@@ -3771,7 +3790,11 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs)
37713790

37723791
int clicked_button_id;
37733792

3793+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3794+
if (!SDL_ShowMessageBox(&msgbox_data, &clicked_button_id)) {
3795+
#else
37743796
if (SDL_ShowMessageBox(&msgbox_data, &clicked_button_id)) {
3797+
#endif
37753798
PyErr_SetString(pgExc_SDLError, SDL_GetError());
37763799
goto error;
37773800
}

0 commit comments

Comments
 (0)