Skip to content

Commit 9400289

Browse files
committed
Fix: Ordered monitor indexing for SDL3
Formatting + fix missing ifdef
1 parent f36533c commit 9400289

File tree

1 file changed

+73
-16
lines changed

1 file changed

+73
-16
lines changed

src/platforms/rcore_desktop_sdl.c

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191

9292
#define SCANCODE_MAPPED_NUM 232
9393

94+
#if defined(USING_VERSION_SDL3)
95+
#define MAX_MONITORS 8
96+
#endif
97+
9498
//----------------------------------------------------------------------------------
9599
// Types and Structures Definition
96100
//----------------------------------------------------------------------------------
@@ -100,6 +104,9 @@ typedef struct {
100104

101105
SDL_GameController *gamepad[MAX_GAMEPADS];
102106
SDL_JoystickID gamepadId[MAX_GAMEPADS]; // Joystick instance ids, they do not start from 0
107+
#if defined(USING_VERSION_SDL3)
108+
SDL_DisplayID displayId[MAX_MONITORS];
109+
#endif
103110
SDL_Cursor *cursor;
104111
} PlatformData;
105112

@@ -825,11 +832,13 @@ void SetWindowMonitor(int monitor)
825832
{
826833
const int monitorCount = SDL_GetNumVideoDisplays();
827834
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
828-
if (SDL_GetDisplayProperties(monitor) != 0) // Returns 0 on failure, so a value other than zero indicates that the monitor id is valid
835+
if ((monitor >=0 ) && (monitor < monitorCount) && (monitor < MAX_MONITORS))
836+
{
837+
monitor = platform.displayId[monitor];
829838
#else
830839
if ((monitor >= 0) && (monitor < monitorCount))
831-
#endif
832840
{
841+
#endif
833842
// NOTE 1: SDL started supporting moving exclusive fullscreen windows between displays on SDL3,
834843
// see commit https://github.com/libsdl-org/SDL/commit/3f5ef7dd422057edbcf3e736107e34be4b75d9ba
835844
// NOTE 2: A workaround for SDL2 is leaving fullscreen, moving the window, then entering full screen again
@@ -943,6 +952,18 @@ int GetCurrentMonitor(void)
943952
// Be aware that this returns an ID in SDL3 and a Index in SDL2
944953
currentMonitor = SDL_GetWindowDisplayIndex(platform.window);
945954

955+
#if defined(USING_VERSION_SDL3)
956+
int monitorCount = GetMonitorCount();
957+
for (int i = 0; (i < monitorCount) && (i < MAX_MONITORS); i++)
958+
{
959+
if (platform.displayId[i] == currentMonitor)
960+
{
961+
currentMonitor = i;
962+
break;
963+
}
964+
}
965+
#endif
966+
946967
return currentMonitor;
947968
}
948969

@@ -951,11 +972,13 @@ Vector2 GetMonitorPosition(int monitor)
951972
{
952973
const int monitorCount = SDL_GetNumVideoDisplays();
953974
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
954-
if (SDL_GetDisplayProperties(monitor) != 0) // Returns 0 on failure, so a value other than zero indicates that the monitor id is valid
975+
if ((monitor >=0 ) && (monitor < monitorCount) && (monitor < MAX_MONITORS))
976+
{
977+
monitor = platform.displayId[monitor];
955978
#else
956979
if ((monitor >= 0) && (monitor < monitorCount))
957-
#endif
958980
{
981+
#endif
959982
SDL_Rect displayBounds;
960983

961984
#if defined(USING_VERSION_SDL3)
@@ -979,11 +1002,13 @@ int GetMonitorWidth(int monitor)
9791002

9801003
const int monitorCount = SDL_GetNumVideoDisplays();
9811004
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
982-
if (SDL_GetDisplayProperties(monitor) != 0) // Returns 0 on failure, so a value other than zero indicates that the monitor id is valid
1005+
if ((monitor >=0 ) && (monitor < monitorCount) && (monitor < MAX_MONITORS))
1006+
{
1007+
monitor = platform.displayId[monitor];
9831008
#else
9841009
if ((monitor >= 0) && (monitor < monitorCount))
985-
#endif
9861010
{
1011+
#endif
9871012
SDL_DisplayMode mode;
9881013
SDL_GetCurrentDisplayMode(monitor, &mode);
9891014
width = mode.w;
@@ -1000,11 +1025,13 @@ int GetMonitorHeight(int monitor)
10001025

10011026
const int monitorCount = SDL_GetNumVideoDisplays();
10021027
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1003-
if (SDL_GetDisplayProperties(monitor) != 0) // Returns 0 on failure, so a value other than zero indicates that the monitor id is valid
1028+
if ((monitor >= 0) && (monitor < monitorCount) && (monitor < MAX_MONITORS))
1029+
{
1030+
monitor = platform.displayId[monitor];
10041031
#else
10051032
if ((monitor >= 0) && (monitor < monitorCount))
1006-
#endif
10071033
{
1034+
#endif
10081035
SDL_DisplayMode mode;
10091036
SDL_GetCurrentDisplayMode(monitor, &mode);
10101037
height = mode.h;
@@ -1021,11 +1048,13 @@ int GetMonitorPhysicalWidth(int monitor)
10211048

10221049
const int monitorCount = SDL_GetNumVideoDisplays();
10231050
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1024-
if (SDL_GetDisplayProperties(monitor) != 0) // Returns 0 on failure, so a value other than zero indicates that the monitor id is valid
1051+
if ((monitor >= 0) && (monitor < monitorCount) && (monitor < MAX_MONITORS))
1052+
{
1053+
monitor = platform.displayId[monitor];
10251054
#else
10261055
if ((monitor >= 0) && (monitor < monitorCount))
1027-
#endif
10281056
{
1057+
#endif
10291058
float ddpi = 0.0f;
10301059
SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL);
10311060
SDL_DisplayMode mode;
@@ -1045,11 +1074,13 @@ int GetMonitorPhysicalHeight(int monitor)
10451074

10461075
const int monitorCount = SDL_GetNumVideoDisplays();
10471076
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1048-
if (SDL_GetDisplayProperties(monitor) != 0) // Returns 0 on failure, so a value other than zero indicates that the monitor id is valid
1077+
if ((monitor >= 0) && (monitor < monitorCount) && (monitor < MAX_MONITORS))
1078+
{
1079+
monitor = platform.displayId[monitor];
10491080
#else
10501081
if ((monitor >= 0) && (monitor < monitorCount))
1051-
#endif
10521082
{
1083+
#endif
10531084
float ddpi = 0.0f;
10541085
SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL);
10551086
SDL_DisplayMode mode;
@@ -1069,11 +1100,13 @@ int GetMonitorRefreshRate(int monitor)
10691100

10701101
const int monitorCount = SDL_GetNumVideoDisplays();
10711102
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1072-
if (SDL_GetDisplayProperties(monitor) != 0) // Returns 0 on failure, so a value other than zero indicates that the monitor id is valid
1103+
if ((monitor >= 0) && (monitor < monitorCount) && (monitor < MAX_MONITORS))
1104+
{
1105+
monitor = platform.displayId[monitor];
10731106
#else
10741107
if ((monitor >= 0) && (monitor < monitorCount))
1075-
#endif
10761108
{
1109+
#endif
10771110
SDL_DisplayMode mode;
10781111
SDL_GetCurrentDisplayMode(monitor, &mode);
10791112
refresh = mode.refresh_rate;
@@ -1089,11 +1122,13 @@ const char *GetMonitorName(int monitor)
10891122
const int monitorCount = SDL_GetNumVideoDisplays();
10901123

10911124
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
1092-
if (SDL_GetDisplayProperties(monitor) != 0) // Returns 0 on failure, so a value other than zero indicates that the monitor id is valid
1125+
if ((monitor >= 0) && (monitor < monitorCount) && (monitor < MAX_MONITORS))
1126+
{
1127+
monitor = platform.displayId[monitor];
10931128
#else
10941129
if ((monitor >= 0) && (monitor < monitorCount))
1095-
#endif
10961130
{
1131+
#endif
10971132
return SDL_GetDisplayName(monitor);
10981133
}
10991134
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
@@ -2087,6 +2122,28 @@ int InitPlatform(void)
20872122

20882123
//----------------------------------------------------------------------------
20892124

2125+
#if defined(USING_VERSION_SDL3)
2126+
// Init monitors
2127+
for (int i = 0; i < MAX_MONITORS; i++)
2128+
{
2129+
platform.displayId[i] = -1;
2130+
}
2131+
2132+
int numMonitors = 0;
2133+
SDL_DisplayID *displays = SDL_GetDisplays(&numMonitors);
2134+
if (numMonitors < 1)
2135+
{
2136+
TRACELOG(LOG_FATAL, "PLATFORM: Failed to find monitors");
2137+
SDL_free(displays);
2138+
return -1;
2139+
}
2140+
2141+
for (int i = 0; (i < numMonitors) && (i < MAX_MONITORS); i++)
2142+
{
2143+
platform.displayId[i] = displays[i];
2144+
}
2145+
#endif
2146+
20902147
// Initialize input events system
20912148
//----------------------------------------------------------------------------
20922149
// Initialize gamepads

0 commit comments

Comments
 (0)