Skip to content

Commit a1acefc

Browse files
committed
sdl3(display): handle renderer API changes
1 parent 90e8f44 commit a1acefc

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

src_c/display.c

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,25 @@ _get_video_window_pos(int *x, int *y, int *center_window)
737737
return 0;
738738
}
739739

740+
static inline int
741+
PG_RenderSetIntegerScale(SDL_Renderer *renderer, int enable)
742+
{
743+
#if SDL_VERSION_ATLEAST(3, 0, 0)
744+
int w = 0, h = 0;
745+
if (!SDL_GetRenderLogicalPresentation(renderer, &w, &h, NULL)) {
746+
return -1;
747+
}
748+
return SDL_SetRenderLogicalPresentation(
749+
renderer, w, h,
750+
enable ? SDL_LOGICAL_PRESENTATION_INTEGER_SCALE
751+
: SDL_LOGICAL_PRESENTATION_LETTERBOX)
752+
? 0
753+
: -1;
754+
#else
755+
return SDL_RenderSetIntegerScale(renderer, enable);
756+
#endif
757+
}
758+
740759
#if SDL_VERSION_ATLEAST(3, 0, 0)
741760
static bool
742761
#else
@@ -796,14 +815,14 @@ pg_ResizeEventWatch(void *userdata, SDL_Event *event)
796815
#else
797816
if (event->window.event == SDL_WINDOWEVENT_MAXIMIZED) {
798817
#endif
799-
SDL_RenderSetIntegerScale(pg_renderer, SDL_FALSE);
818+
PG_RenderSetIntegerScale(pg_renderer, SDL_FALSE);
800819
}
801820
#if SDL_VERSION_ATLEAST(3, 0, 0)
802821
if (event->type == SDL_WINDOWEVENT_RESTORED) {
803822
#else
804823
if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
805824
#endif
806-
SDL_RenderSetIntegerScale(
825+
PG_RenderSetIntegerScale(
807826
pg_renderer, !(SDL_GetHintBoolean(
808827
"SDL_HINT_RENDER_SCALE_QUALITY", SDL_FALSE)));
809828
}
@@ -981,10 +1000,13 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
9811000

9821001
if (scale_env != NULL) {
9831002
flags |= PGS_SCALED;
1003+
/* TODO: figure out SDL3 equivalent */
1004+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
9841005
if (strcmp(scale_env, "photo") == 0) {
9851006
SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY, "best",
9861007
SDL_HINT_NORMAL);
9871008
}
1009+
#endif
9881010
}
9891011

9901012
if (size != NULL) {
@@ -1363,8 +1385,12 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
13631385

13641386
if (flags & PGS_SCALED || state->unscaled_render) {
13651387
if (pg_renderer == NULL) {
1388+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
1389+
/* TODO: check if this behaviour is still to be retained
1390+
* in SDL3 */
13661391
SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY,
13671392
"nearest", SDL_HINT_DEFAULT);
1393+
#endif
13681394

13691395
#if SDL_VERSION_ATLEAST(2, 28, 0)
13701396
/* If the window has a surface associated with it already,
@@ -1375,13 +1401,20 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
13751401
}
13761402
#endif
13771403

1404+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1405+
pg_renderer = SDL_CreateRenderer(win, NULL);
1406+
if (vsync && pg_renderer) {
1407+
SDL_SetRenderVSync(pg_renderer, 1);
1408+
}
1409+
#else
13781410
if (vsync) {
13791411
pg_renderer = SDL_CreateRenderer(
13801412
win, -1, SDL_RENDERER_PRESENTVSYNC);
13811413
}
13821414
else {
13831415
pg_renderer = SDL_CreateRenderer(win, -1, 0);
13841416
}
1417+
#endif
13851418

13861419
if (pg_renderer == NULL) {
13871420
return RAISE(pgExc_SDLError, SDL_GetError());
@@ -1391,12 +1424,24 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
13911424
/* use whole screen with uneven pixels on fullscreen,
13921425
exact scale otherwise.
13931426
we chose the window size for this to work */
1427+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1428+
int enable_intscale =
1429+
!(flags & PGS_FULLSCREEN ||
1430+
SDL_GetHintBoolean(
1431+
"SDL_HINT_RENDER_SCALE_QUALITY", SDL_FALSE));
1432+
SDL_SetRenderLogicalPresentation(
1433+
pg_renderer, w, h,
1434+
enable_intscale
1435+
? SDL_LOGICAL_PRESENTATION_INTEGER_SCALE
1436+
: SDL_LOGICAL_PRESENTATION_LETTERBOX);
1437+
#else
13941438
SDL_RenderSetIntegerScale(
13951439
pg_renderer, !(flags & PGS_FULLSCREEN ||
13961440
SDL_GetHintBoolean(
13971441
"SDL_HINT_RENDER_SCALE_QUALITY",
13981442
SDL_FALSE)));
13991443
SDL_RenderSetLogicalSize(pg_renderer, w, h);
1444+
#endif
14001445
/* this must be called after creating the renderer!*/
14011446
SDL_SetWindowMinimumSize(win, w, h);
14021447
}
@@ -2835,18 +2880,31 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
28352880
/* display surface lost? */
28362881
SDL_DestroyTexture(pg_texture);
28372882
SDL_DestroyRenderer(pg_renderer);
2883+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2884+
pg_renderer = SDL_CreateRenderer(win, SDL_SOFTWARE_RENDERER);
2885+
#else
28382886
pg_renderer =
28392887
SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
2888+
#endif
28402889
pg_texture =
28412890
SDL_CreateTexture(pg_renderer, SDL_PIXELFORMAT_ARGB8888,
28422891
SDL_TEXTUREACCESS_STREAMING, w, h);
28432892
}
2893+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2894+
int enable_intscale = !SDL_GetHintBoolean(
2895+
"SDL_HINT_RENDER_SCALE_QUALITY", SDL_FALSE);
2896+
SDL_SetRenderLogicalPresentation(
2897+
pg_renderer, w, h,
2898+
enable_intscale ? SDL_LOGICAL_PRESENTATION_INTEGER_SCALE
2899+
: SDL_LOGICAL_PRESENTATION_LETTERBOX);
2900+
#else
28442901
SDL_RenderSetLogicalSize(pg_renderer, w, h);
28452902

28462903
/* use exact integer scale in windowed mode */
28472904
SDL_RenderSetIntegerScale(
28482905
pg_renderer, !SDL_GetHintBoolean(
28492906
"SDL_HINT_RENDER_SCALE_QUALITY", SDL_FALSE));
2907+
#endif
28502908
SDL_SetWindowMinimumSize(win, w, h);
28512909
}
28522910
else if (state->using_gl) {
@@ -2977,15 +3035,24 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
29773035
/* display surface lost? only on x11? */
29783036
SDL_DestroyTexture(pg_texture);
29793037
SDL_DestroyRenderer(pg_renderer);
3038+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3039+
pg_renderer = SDL_CreateRenderer(win, SDL_SOFTWARE_RENDERER);
3040+
#else
29803041
pg_renderer =
29813042
SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
3043+
#endif
29823044
pg_texture =
29833045
SDL_CreateTexture(pg_renderer, SDL_PIXELFORMAT_ARGB8888,
29843046
SDL_TEXTUREACCESS_STREAMING, w, h);
29853047
}
29863048

3049+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3050+
SDL_SetRenderLogicalPresentation(
3051+
pg_renderer, w, h, SDL_LOGICAL_PRESENTATION_LETTERBOX);
3052+
#else
29873053
SDL_RenderSetLogicalSize(pg_renderer, w, h);
29883054
SDL_RenderSetIntegerScale(pg_renderer, SDL_FALSE);
3055+
#endif
29893056
}
29903057
else if (state->using_gl) {
29913058
result =
@@ -3115,9 +3182,18 @@ pg_display_resize_event(PyObject *self, PyObject *event)
31153182
}
31163183
}
31173184
else if (pg_renderer != NULL) {
3185+
/* TODO: verify why this block exists and whether SDL3 port is
3186+
* equivalent */
3187+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3188+
SDL_RendererLogicalPresentation mode;
3189+
SDL_GetRenderLogicalPresentation(pg_renderer, &w, &h, &mode);
3190+
SDL_SetWindowSize(win, (w > wnew) ? w : wnew, (h > hnew) ? h : hnew);
3191+
result = SDL_SetRenderLogicalPresentation(pg_renderer, w, h, mode);
3192+
#else
31183193
SDL_RenderGetLogicalSize(pg_renderer, &w, &h);
31193194
SDL_SetWindowSize(win, (w > wnew) ? w : wnew, (h > hnew) ? h : hnew);
31203195
result = SDL_RenderSetLogicalSize(pg_renderer, w, h);
3196+
#endif
31213197
if (result != 0) {
31223198
return RAISE(pgExc_SDLError, SDL_GetError());
31233199
}

0 commit comments

Comments
 (0)