3 Commits

Author SHA1 Message Date
1b5a14e516 [rcore_desktop_sdl] fix: handle monitor ID correctly on SDL3 (#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:
https://github.com/raysan5/raylib/issues/5256#issuecomment-3429156919
Hopefully, someone will refactor it someday :)

Fixes: https://github.com/raysan5/raylib/issues/5256
2025-10-21 22:07:04 +02:00
Ray
99ed814615 Update rcore_desktop_win32.c 2025-10-21 20:07:42 +02:00
Ray
77b9214575 Update rcore_desktop_sdl.c 2025-10-21 20:07:32 +02:00
2 changed files with 54 additions and 18 deletions

View File

@ -104,7 +104,6 @@ typedef struct {
SDL_GameController *gamepad[MAX_GAMEPADS]; SDL_GameController *gamepad[MAX_GAMEPADS];
SDL_JoystickID gamepadId[MAX_GAMEPADS]; // Joystick instance ids, they do not start from 0 SDL_JoystickID gamepadId[MAX_GAMEPADS]; // Joystick instance ids, they do not start from 0
SDL_Cursor *cursor; SDL_Cursor *cursor;
bool cursorRelative;
} PlatformData; } PlatformData;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -838,7 +837,11 @@ void SetWindowPosition(int x, int y)
void SetWindowMonitor(int monitor) void SetWindowMonitor(int monitor)
{ {
const int monitorCount = SDL_GetNumVideoDisplays(); const int monitorCount = SDL_GetNumVideoDisplays();
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount)) if ((monitor >= 0) && (monitor < monitorCount))
#endif
{ {
// NOTE: // NOTE:
// 1. SDL started supporting moving exclusive fullscreen windows between displays on SDL3, // 1. SDL started supporting moving exclusive fullscreen windows between displays on SDL3,
@ -962,7 +965,11 @@ int GetCurrentMonitor(void)
Vector2 GetMonitorPosition(int monitor) Vector2 GetMonitorPosition(int monitor)
{ {
const int monitorCount = SDL_GetNumVideoDisplays(); const int monitorCount = SDL_GetNumVideoDisplays();
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount)) if ((monitor >= 0) && (monitor < monitorCount))
#endif
{ {
SDL_Rect displayBounds; SDL_Rect displayBounds;
@ -986,7 +993,11 @@ int GetMonitorWidth(int monitor)
int width = 0; int width = 0;
const int monitorCount = SDL_GetNumVideoDisplays(); const int monitorCount = SDL_GetNumVideoDisplays();
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount)) if ((monitor >= 0) && (monitor < monitorCount))
#endif
{ {
SDL_DisplayMode mode; SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(monitor, &mode); SDL_GetCurrentDisplayMode(monitor, &mode);
@ -1003,7 +1014,11 @@ int GetMonitorHeight(int monitor)
int height = 0; int height = 0;
const int monitorCount = SDL_GetNumVideoDisplays(); const int monitorCount = SDL_GetNumVideoDisplays();
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount)) if ((monitor >= 0) && (monitor < monitorCount))
#endif
{ {
SDL_DisplayMode mode; SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(monitor, &mode); SDL_GetCurrentDisplayMode(monitor, &mode);
@ -1020,7 +1035,11 @@ int GetMonitorPhysicalWidth(int monitor)
int width = 0; int width = 0;
const int monitorCount = SDL_GetNumVideoDisplays(); const int monitorCount = SDL_GetNumVideoDisplays();
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount)) if ((monitor >= 0) && (monitor < monitorCount))
#endif
{ {
float ddpi = 0.0f; float ddpi = 0.0f;
SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL); SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL);
@ -1040,7 +1059,11 @@ int GetMonitorPhysicalHeight(int monitor)
int height = 0; int height = 0;
const int monitorCount = SDL_GetNumVideoDisplays(); const int monitorCount = SDL_GetNumVideoDisplays();
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount)) if ((monitor >= 0) && (monitor < monitorCount))
#endif
{ {
float ddpi = 0.0f; float ddpi = 0.0f;
SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL); SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL);
@ -1060,7 +1083,11 @@ int GetMonitorRefreshRate(int monitor)
int refresh = 0; int refresh = 0;
const int monitorCount = SDL_GetNumVideoDisplays(); const int monitorCount = SDL_GetNumVideoDisplays();
#if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount)) if ((monitor >= 0) && (monitor < monitorCount))
#endif
{ {
SDL_DisplayMode mode; SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(monitor, &mode); SDL_GetCurrentDisplayMode(monitor, &mode);
@ -1076,7 +1103,14 @@ const char *GetMonitorName(int monitor)
{ {
const int monitorCount = SDL_GetNumVideoDisplays(); const int monitorCount = SDL_GetNumVideoDisplays();
if ((monitor >= 0) && (monitor < monitorCount)) return SDL_GetDisplayName(monitor); #if defined(USING_VERSION_SDL3) // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount))
#endif
{
return SDL_GetDisplayName(monitor);
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor"); else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
return ""; return "";
@ -1209,13 +1243,13 @@ void EnableCursor(void)
SDL_SetRelativeMouseMode(SDL_FALSE); SDL_SetRelativeMouseMode(SDL_FALSE);
#if defined(USING_VERSION_SDL3) #if defined(USING_VERSION_SDL3)
// SDL_ShowCursor() has been split into three functions: SDL_ShowCursor(), SDL_HideCursor(), and SDL_CursorVisible() // NOTE: SDL_ShowCursor() has been split into three functions:
// SDL_ShowCursor(), SDL_HideCursor(), and SDL_CursorVisible()
SDL_ShowCursor(); SDL_ShowCursor();
#else #else
SDL_ShowCursor(SDL_ENABLE); SDL_ShowCursor(SDL_ENABLE);
#endif #endif
platform.cursorRelative = false;
CORE.Input.Mouse.cursorLocked = false; CORE.Input.Mouse.cursorLocked = false;
} }
@ -1224,7 +1258,6 @@ void DisableCursor(void)
{ {
SDL_SetRelativeMouseMode(SDL_TRUE); SDL_SetRelativeMouseMode(SDL_TRUE);
platform.cursorRelative = true;
CORE.Input.Mouse.cursorLocked = true; CORE.Input.Mouse.cursorLocked = true;
} }
@ -1332,7 +1365,7 @@ void PollInputEvents(void)
CORE.Input.Mouse.currentWheelMove.y = 0; CORE.Input.Mouse.currentWheelMove.y = 0;
// Register previous mouse position // Register previous mouse position
if (platform.cursorRelative) CORE.Input.Mouse.currentPosition = (Vector2){ 0.0f, 0.0f }; if (CORE.Input.Mouse.cursorLocked) CORE.Input.Mouse.currentPosition = (Vector2){ 0.0f, 0.0f };
else CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition; else CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
// Reset last gamepad button/axis registered state // Reset last gamepad button/axis registered state
@ -1634,7 +1667,7 @@ void PollInputEvents(void)
} break; } break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
{ {
if (platform.cursorRelative) if (CORE.Input.Mouse.cursorLocked)
{ {
CORE.Input.Mouse.currentPosition.x = (float)event.motion.xrel; CORE.Input.Mouse.currentPosition.x = (float)event.motion.xrel;
CORE.Input.Mouse.currentPosition.y = (float)event.motion.yrel; CORE.Input.Mouse.currentPosition.y = (float)event.motion.yrel;

View File

@ -1136,17 +1136,17 @@ Image GetClipboardImage(void)
// Show mouse cursor // Show mouse cursor
void ShowCursor(void) void ShowCursor(void)
{ {
CORE.Input.Mouse.cursorHidden = false;
SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW)); SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
CORE.Input.Mouse.cursorHidden = false;
} }
// Hides mouse cursor // Hides mouse cursor
void HideCursor(void) void HideCursor(void)
{ {
// NOTE: we use SetCursor instead of ShowCursor because it makes it easy // NOTE: We use SetCursor() instead of ShowCursor() because
// to only hide the cursor while it's inside the client area // it makes it easy to only hide the cursor while it's inside the client area
CORE.Input.Mouse.cursorHidden = true;
SetCursor(NULL); SetCursor(NULL);
CORE.Input.Mouse.cursorHidden = true;
} }
// Enables cursor (unlock cursor) // Enables cursor (unlock cursor)
@ -1346,7 +1346,7 @@ void PollInputEvents(void)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Initialize modern OpenGL context // Initialize modern OpenGL context
// NOTE: We need to create a dummy context first to query requried extensions // NOTE: We need to create a dummy context first to query required extensions
HGLRC InitOpenGL(HWND hwnd, HDC hdc) HGLRC InitOpenGL(HWND hwnd, HDC hdc)
{ {
// First, create a dummy context to get WGL extensions // First, create a dummy context to get WGL extensions
@ -1621,8 +1621,7 @@ int InitPlatform(void)
CORE.Window.ready = true; CORE.Window.ready = true;
// TODO: Should this function be called before or after drawing context is created? --> After swInit() called! // Update flags (in case of deferred state change required)
//UpdateWindowSize(UPDATE_WINDOW_FIRST, platform.hwnd, platform.appScreenWidth, platform.appScreenHeight, platform.desiredFlags);
UpdateFlags(platform.hwnd, platform.desiredFlags, platform.appScreenWidth, platform.appScreenHeight); UpdateFlags(platform.hwnd, platform.desiredFlags, platform.appScreenWidth, platform.appScreenHeight);
CORE.Window.render.width = CORE.Window.screen.width; CORE.Window.render.width = CORE.Window.screen.width;
@ -1887,6 +1886,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpara
} break; } break;
case WM_SETCURSOR: case WM_SETCURSOR:
{ {
// Called when mouse moves, enters/leaves window...
if (LOWORD(lparam) == HTCLIENT) if (LOWORD(lparam) == HTCLIENT)
{ {
SetCursor(CORE.Input.Mouse.cursorHidden? NULL : LoadCursorW(NULL, (LPCWSTR)IDC_ARROW)); SetCursor(CORE.Input.Mouse.cursorHidden? NULL : LoadCursorW(NULL, (LPCWSTR)IDC_ARROW));
@ -1972,6 +1972,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpara
return result; return result;
} }
// Handle keyboard input event
static void HandleKey(WPARAM wparam, LPARAM lparam, char state) static void HandleKey(WPARAM wparam, LPARAM lparam, char state)
{ {
KeyboardKey key = GetKeyFromWparam(wparam); KeyboardKey key = GetKeyFromWparam(wparam);
@ -1991,12 +1992,15 @@ static void HandleKey(WPARAM wparam, LPARAM lparam, char state)
// TODO: Add key to the queue as well? // TODO: Add key to the queue as well?
} }
// Handle mouse button input event
static void HandleMouseButton(int button, char state) static void HandleMouseButton(int button, char state)
{ {
// Register current mouse button state
CORE.Input.Mouse.currentButtonState[button] = state; CORE.Input.Mouse.currentButtonState[button] = state;
CORE.Input.Touch.currentTouchState[button] = state; CORE.Input.Touch.currentTouchState[button] = state;
} }
// Handle raw input event
static void HandleRawInput(LPARAM lparam) static void HandleRawInput(LPARAM lparam)
{ {
RAWINPUT input = { 0 }; RAWINPUT input = { 0 };
@ -2020,6 +2024,7 @@ static void HandleRawInput(LPARAM lparam)
//if (CORE.Input.Mouse.currentPosition.y != 0) abort(); //if (CORE.Input.Mouse.currentPosition.y != 0) abort();
} }
// Handle window resizing event
static void HandleWindowResize(HWND hwnd, int *width, int *height) static void HandleWindowResize(HWND hwnd, int *width, int *height)
{ {
if (CORE.Window.flags & FLAG_WINDOW_MINIMIZED) return; if (CORE.Window.flags & FLAG_WINDOW_MINIMIZED) return;
@ -2051,10 +2056,8 @@ static void HandleWindowResize(HWND hwnd, int *width, int *height)
*height = screenHeight; *height = screenHeight;
} }
CORE.Window.screenScale = MatrixScale( CORE.Window.screenScale = MatrixScale( (float)CORE.Window.render.width/CORE.Window.screen.width,
(float)CORE.Window.render.width/CORE.Window.screen.width, (float)CORE.Window.render.height/CORE.Window.screen.height, 1.0f);
(float)CORE.Window.render.height/CORE.Window.screen.height,
1.0f);
} }
// Update window style // Update window style