Skip to content

Commit 1b5a14e

Browse files
[rcore_desktop_sdl] fix: handle monitor ID correctly on SDL3 (raysan5#5290)
SDL3 uses ID when dealing with monitors, unlike SDL2 which uses Index for the same thing. This problem was already fixed in multiple places by use of preprocessor branches, so I did the very same thing. Please, notice that this is a pretty bad solution to this problem, and I only did it to keep it consistent with the rest of the code. The more about why it's not correct is mentioned here: raysan5#5256 (comment) Hopefully, someone will refactor it someday :) Fixes: raysan5#5256
1 parent 99ed814 commit 1b5a14e

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/platforms/rcore_desktop_sdl.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,11 @@ void SetWindowPosition(int x, int y)
837837
void SetWindowMonitor(int monitor)
838838
{
839839
const int monitorCount = SDL_GetNumVideoDisplays();
840+
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
841+
if ((monitor > 0) && (monitor <= monitorCount))
842+
#else
840843
if ((monitor >= 0) && (monitor < monitorCount))
844+
#endif
841845
{
842846
// NOTE:
843847
// 1. SDL started supporting moving exclusive fullscreen windows between displays on SDL3,
@@ -961,7 +965,11 @@ int GetCurrentMonitor(void)
961965
Vector2 GetMonitorPosition(int monitor)
962966
{
963967
const int monitorCount = SDL_GetNumVideoDisplays();
968+
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
969+
if ((monitor > 0) && (monitor <= monitorCount))
970+
#else
964971
if ((monitor >= 0) && (monitor < monitorCount))
972+
#endif
965973
{
966974
SDL_Rect displayBounds;
967975

@@ -985,7 +993,11 @@ int GetMonitorWidth(int monitor)
985993
int width = 0;
986994

987995
const int monitorCount = SDL_GetNumVideoDisplays();
996+
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
997+
if ((monitor > 0) && (monitor <= monitorCount))
998+
#else
988999
if ((monitor >= 0) && (monitor < monitorCount))
1000+
#endif
9891001
{
9901002
SDL_DisplayMode mode;
9911003
SDL_GetCurrentDisplayMode(monitor, &mode);
@@ -1002,7 +1014,11 @@ int GetMonitorHeight(int monitor)
10021014
int height = 0;
10031015

10041016
const int monitorCount = SDL_GetNumVideoDisplays();
1017+
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1018+
if ((monitor > 0) && (monitor <= monitorCount))
1019+
#else
10051020
if ((monitor >= 0) && (monitor < monitorCount))
1021+
#endif
10061022
{
10071023
SDL_DisplayMode mode;
10081024
SDL_GetCurrentDisplayMode(monitor, &mode);
@@ -1019,7 +1035,11 @@ int GetMonitorPhysicalWidth(int monitor)
10191035
int width = 0;
10201036

10211037
const int monitorCount = SDL_GetNumVideoDisplays();
1038+
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1039+
if ((monitor > 0) && (monitor <= monitorCount))
1040+
#else
10221041
if ((monitor >= 0) && (monitor < monitorCount))
1042+
#endif
10231043
{
10241044
float ddpi = 0.0f;
10251045
SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL);
@@ -1039,7 +1059,11 @@ int GetMonitorPhysicalHeight(int monitor)
10391059
int height = 0;
10401060

10411061
const int monitorCount = SDL_GetNumVideoDisplays();
1062+
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1063+
if ((monitor > 0) && (monitor <= monitorCount))
1064+
#else
10421065
if ((monitor >= 0) && (monitor < monitorCount))
1066+
#endif
10431067
{
10441068
float ddpi = 0.0f;
10451069
SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL);
@@ -1059,7 +1083,11 @@ int GetMonitorRefreshRate(int monitor)
10591083
int refresh = 0;
10601084

10611085
const int monitorCount = SDL_GetNumVideoDisplays();
1086+
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1087+
if ((monitor > 0) && (monitor <= monitorCount))
1088+
#else
10621089
if ((monitor >= 0) && (monitor < monitorCount))
1090+
#endif
10631091
{
10641092
SDL_DisplayMode mode;
10651093
SDL_GetCurrentDisplayMode(monitor, &mode);
@@ -1075,7 +1103,14 @@ const char *GetMonitorName(int monitor)
10751103
{
10761104
const int monitorCount = SDL_GetNumVideoDisplays();
10771105

1078-
if ((monitor >= 0) && (monitor < monitorCount)) return SDL_GetDisplayName(monitor);
1106+
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1107+
if ((monitor > 0) && (monitor <= monitorCount))
1108+
#else
1109+
if ((monitor >= 0) && (monitor < monitorCount))
1110+
#endif
1111+
{
1112+
return SDL_GetDisplayName(monitor);
1113+
}
10791114
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
10801115

10811116
return "";

0 commit comments

Comments
 (0)