Skip to content

Commit f98e444

Browse files
committed
SDL3: display+window: runtime fixes
1 parent ee899f3 commit f98e444

File tree

2 files changed

+115
-16
lines changed

2 files changed

+115
-16
lines changed

src_c/display.c

Lines changed: 92 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ _display_state_cleanup(_DisplayState *state)
9999
#endif
100100
}
101101

102+
/* TODO: Check if doing SDL_PIXELFORMAT_XRGB8888 in SDL2 codepath is fine.
103+
* For now do it only on SDL3 codepath to fix SCALED issues */
104+
#if SDL_VERSION_ATLEAST(3, 0, 0)
105+
#define PG_TEXTURE_FMT SDL_PIXELFORMAT_XRGB8888
106+
#else
107+
#define PG_TEXTURE_FMT SDL_PIXELFORMAT_ARGB8888
108+
#endif
109+
102110
// prevent this code block from being linked twice
103111
// (this code block is copied by window.c)
104112
#ifndef BUILD_STATIC
@@ -464,7 +472,12 @@ pg_GetVideoInfo(pg_VideoInfo *info)
464472
}
465473
else {
466474
#if SDL_VERSION_ATLEAST(3, 0, 0)
467-
if ((mode_ptr = SDL_GetCurrentDisplayMode(0))) {
475+
SDL_DisplayID primary_display = SDL_GetPrimaryDisplay();
476+
if (primary_display == 0) {
477+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
478+
return (pg_VideoInfo *)NULL;
479+
}
480+
if ((mode_ptr = SDL_GetCurrentDisplayMode(primary_display))) {
468481
info->current_w = mode_ptr->w;
469482
info->current_h = mode_ptr->h;
470483
formatenum = mode_ptr->format;
@@ -942,9 +955,8 @@ pg_ResizeEventWatch(void *userdata, SDL_Event *event)
942955

943956
SDL_DestroyTexture(pg_texture);
944957

945-
pg_texture =
946-
SDL_CreateTexture(pg_renderer, SDL_PIXELFORMAT_ARGB8888,
947-
SDL_TEXTUREACCESS_STREAMING, w, h);
958+
pg_texture = SDL_CreateTexture(pg_renderer, PG_TEXTURE_FMT,
959+
SDL_TEXTUREACCESS_STREAMING, w, h);
948960
}
949961
return 0;
950962
}
@@ -1166,6 +1178,7 @@ PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
11661178
}
11671179
}
11681180

1181+
SDL_SyncWindow(window);
11691182
ret = true;
11701183
end:
11711184
SDL_free(modes);
@@ -1261,6 +1274,16 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
12611274
}
12621275
}
12631276

1277+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1278+
/* In SDL2, display == 0 meant primary display, so compat code for it */
1279+
if (display == 0) {
1280+
display = SDL_GetPrimaryDisplay();
1281+
if (display == 0) {
1282+
return RAISE(pgExc_SDLError, SDL_GetError());
1283+
}
1284+
}
1285+
#endif
1286+
12641287
if ((vsync == -1) && ((flags & PGS_OPENGL) == 0)) {
12651288
return RAISE(PyExc_ValueError,
12661289
"requested adaptive vsync without OpenGL");
@@ -1751,9 +1774,9 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
17511774
}
17521775
#endif
17531776

1754-
pg_texture = SDL_CreateTexture(
1755-
pg_renderer, SDL_PIXELFORMAT_ARGB8888,
1756-
SDL_TEXTUREACCESS_STREAMING, w, h);
1777+
pg_texture =
1778+
SDL_CreateTexture(pg_renderer, PG_TEXTURE_FMT,
1779+
SDL_TEXTUREACCESS_STREAMING, w, h);
17571780
}
17581781
surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_XRGB8888);
17591782
newownedsurf = surf;
@@ -1875,6 +1898,9 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
18751898
}
18761899
}
18771900
}
1901+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1902+
SDL_SyncWindow(win);
1903+
#endif
18781904

18791905
/*return the window's surface (screen)*/
18801906
Py_INCREF(surface);
@@ -1973,6 +1999,9 @@ pg_set_window_position(PyObject *self, PyObject *arg)
19731999
if (win) {
19742000
/* Will raise errors with SDL 3, deal with it during the porting */
19752001
SDL_SetWindowPosition(win, x, y);
2002+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2003+
SDL_SyncWindow(win);
2004+
#endif
19762005
}
19772006

19782007
Py_RETURN_NONE;
@@ -1999,7 +2028,16 @@ pg_mode_ok(PyObject *self, PyObject *args, PyObject *kwds)
19992028
&display_index)) {
20002029
return NULL;
20012030
}
2002-
#if !SDL_VERSION_ATLEAST(3, 0, 0)
2031+
2032+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2033+
/* In SDL2, display == 0 meant primary display, so compat code for it */
2034+
if (display_index == 0) {
2035+
display_index = SDL_GetPrimaryDisplay();
2036+
if (display_index == 0) {
2037+
return RAISE(pgExc_SDLError, SDL_GetError());
2038+
}
2039+
}
2040+
#else
20032041
/* Display ID is not bounded by number of displays in SDL3 */
20042042
if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) {
20052043
return RAISE(PyExc_ValueError,
@@ -2026,6 +2064,15 @@ pg_mode_ok(PyObject *self, PyObject *args, PyObject *kwds)
20262064
}
20272065

20282066
#if SDL_VERSION_ATLEAST(3, 0, 0)
2067+
/* Compat with SDL2 behaviour */
2068+
const SDL_DisplayMode *curmode;
2069+
if (!(curmode = SDL_GetCurrentDisplayMode(display_index))) {
2070+
return RAISE(pgExc_SDLError, SDL_GetError());
2071+
}
2072+
if (curmode->w == desired.w && curmode->h == desired.h) {
2073+
return PyLong_FromLong(SDL_BITSPERPIXEL(curmode->format));
2074+
}
2075+
20292076
if (!SDL_GetClosestFullscreenDisplayMode(display_index, desired.w,
20302077
desired.h, desired.refresh_rate,
20312078
1, &closest)) {
@@ -2065,7 +2112,15 @@ pg_list_modes(PyObject *self, PyObject *args, PyObject *kwds)
20652112
return NULL;
20662113
}
20672114

2068-
#if !SDL_VERSION_ATLEAST(3, 0, 0)
2115+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2116+
/* In SDL2, display == 0 meant primary display, so compat code for it */
2117+
if (display_index == 0) {
2118+
display_index = SDL_GetPrimaryDisplay();
2119+
if (display_index == 0) {
2120+
return RAISE(pgExc_SDLError, SDL_GetError());
2121+
}
2122+
}
2123+
#else
20692124
/* Display ID is not bounded by number of displays in SDL3 */
20702125
if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) {
20712126
return RAISE(PyExc_ValueError,
@@ -2077,19 +2132,31 @@ pg_list_modes(PyObject *self, PyObject *args, PyObject *kwds)
20772132
#pragma PG_WARN(Ignoring flags)
20782133

20792134
#if SDL_VERSION_ATLEAST(3, 0, 0)
2135+
const SDL_DisplayMode *curmode;
2136+
if (!(curmode = SDL_GetCurrentDisplayMode(display_index))) {
2137+
return RAISE(pgExc_SDLError, SDL_GetError());
2138+
}
20802139
if (bpp == 0) {
2081-
const SDL_DisplayMode *curmode;
2082-
if (!(curmode = SDL_GetCurrentDisplayMode(display_index))) {
2083-
return RAISE(pgExc_SDLError, SDL_GetError());
2084-
}
20852140
bpp = SDL_BITSPERPIXEL(curmode->format);
20862141
}
20872142

20882143
SDL_DisplayMode **modes =
20892144
SDL_GetFullscreenDisplayModes(display_index, &nummodes);
2090-
if (nummodes < 0) {
2145+
if (!modes || nummodes < 0) {
20912146
return RAISE(pgExc_SDLError, SDL_GetError());
20922147
}
2148+
2149+
/* SDL3 can return empty list here but SDL2 didn't. In that case, use
2150+
* curmode. */
2151+
if (nummodes == 0) {
2152+
SDL_free(modes);
2153+
modes = SDL_malloc(sizeof(SDL_DisplayMode **));
2154+
if (!modes) {
2155+
return PyErr_NoMemory();
2156+
}
2157+
modes[0] = (SDL_DisplayMode *)curmode;
2158+
nummodes = 1;
2159+
}
20932160
#else
20942161
if (bpp == 0) {
20952162
SDL_DisplayMode curmode;
@@ -2726,6 +2793,9 @@ pg_iconify(PyObject *self, PyObject *_null)
27262793
return RAISE(pgExc_SDLError, "No open window");
27272794
}
27282795
SDL_MinimizeWindow(win);
2796+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2797+
SDL_SyncWindow(win);
2798+
#endif
27292799
return PyBool_FromLong(1);
27302800
}
27312801

@@ -3219,7 +3289,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32193289
SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
32203290
#endif
32213291
pg_texture =
3222-
SDL_CreateTexture(pg_renderer, SDL_PIXELFORMAT_ARGB8888,
3292+
SDL_CreateTexture(pg_renderer, PG_TEXTURE_FMT,
32233293
SDL_TEXTUREACCESS_STREAMING, w, h);
32243294
}
32253295
#if SDL_VERSION_ATLEAST(3, 0, 0)
@@ -3372,7 +3442,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
33723442
SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
33733443
#endif
33743444
pg_texture =
3375-
SDL_CreateTexture(pg_renderer, SDL_PIXELFORMAT_ARGB8888,
3445+
SDL_CreateTexture(pg_renderer, PG_TEXTURE_FMT,
33763446
SDL_TEXTUREACCESS_STREAMING, w, h);
33773447
}
33783448

@@ -3453,6 +3523,9 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34533523
}
34543524
}
34553525
}
3526+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3527+
SDL_SyncWindow(win);
3528+
#endif
34563529
return PyLong_FromLong(1);
34573530
}
34583531

@@ -3531,6 +3604,9 @@ pg_display_resize_event(PyObject *self, PyObject *event)
35313604
/* do not do anything that would invalidate a display surface! */
35323605
return PyLong_FromLong(-1);
35333606
}
3607+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3608+
SDL_SyncWindow(win);
3609+
#endif
35343610
Py_RETURN_FALSE;
35353611
}
35363612

src_c/window.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ window_set_windowed(pgWindowObject *self, PyObject *_null)
305305
if (!SDL_SetWindowFullscreen(self->_win, 0)) {
306306
return RAISE(pgExc_SDLError, SDL_GetError());
307307
}
308+
SDL_SyncWindow(self->_win);
308309
#else
309310
if (SDL_SetWindowFullscreen(self->_win, 0)) {
310311
return RAISE(pgExc_SDLError, SDL_GetError());
@@ -345,6 +346,7 @@ pg_window_set_fullscreen(SDL_Window *window, int desktop)
345346
goto end;
346347
}
347348

349+
SDL_SyncWindow(window);
348350
ret = 1;
349351
end:
350352
SDL_free(modes);
@@ -429,20 +431,29 @@ static PyObject *
429431
window_restore(pgWindowObject *self, PyObject *_null)
430432
{
431433
SDL_RestoreWindow(self->_win);
434+
#if SDL_VERSION_ATLEAST(3, 0, 0)
435+
SDL_SyncWindow(self->_win);
436+
#endif
432437
Py_RETURN_NONE;
433438
}
434439

435440
static PyObject *
436441
window_maximize(pgWindowObject *self, PyObject *_null)
437442
{
438443
SDL_MaximizeWindow(self->_win);
444+
#if SDL_VERSION_ATLEAST(3, 0, 0)
445+
SDL_SyncWindow(self->_win);
446+
#endif
439447
Py_RETURN_NONE;
440448
}
441449

442450
static PyObject *
443451
window_minimize(pgWindowObject *self, PyObject *_null)
444452
{
445453
SDL_MinimizeWindow(self->_win);
454+
#if SDL_VERSION_ATLEAST(3, 0, 0)
455+
SDL_SyncWindow(self->_win);
456+
#endif
446457
Py_RETURN_NONE;
447458
}
448459

@@ -745,6 +756,9 @@ window_set_size(pgWindowObject *self, PyObject *arg, void *v)
745756
}
746757

747758
SDL_SetWindowSize(self->_win, w, h);
759+
#if SDL_VERSION_ATLEAST(3, 0, 0)
760+
SDL_SyncWindow(self->_win);
761+
#endif
748762
if (self->surf) {
749763
/* Ensure that the underlying surf is immediately updated, instead of
750764
* relying on the event callback */
@@ -797,6 +811,9 @@ window_set_minimum_size(pgWindowObject *self, PyObject *arg, void *v)
797811
}
798812

799813
SDL_SetWindowMinimumSize(self->_win, w, h);
814+
#if SDL_VERSION_ATLEAST(3, 0, 0)
815+
SDL_SyncWindow(self->_win);
816+
#endif
800817

801818
return 0;
802819
}
@@ -837,6 +854,9 @@ window_set_maximum_size(pgWindowObject *self, PyObject *arg, void *v)
837854
}
838855

839856
SDL_SetWindowMaximumSize(self->_win, w, h);
857+
#if SDL_VERSION_ATLEAST(3, 0, 0)
858+
SDL_SyncWindow(self->_win);
859+
#endif
840860

841861
return 0;
842862
}
@@ -868,6 +888,9 @@ window_set_position(pgWindowObject *self, PyObject *arg, void *v)
868888
}
869889

870890
SDL_SetWindowPosition(self->_win, x, y);
891+
#if SDL_VERSION_ATLEAST(3, 0, 0)
892+
SDL_SyncWindow(self->_win);
893+
#endif
871894

872895
return 0;
873896
}

0 commit comments

Comments
 (0)