From 5628d5006d028f519aac386a2536694832065edf Mon Sep 17 00:00:00 2001 From: 0xFishbyte <0xfishbyte@proton.me> Date: Thu, 2 Jul 2026 21:05:24 +0300 Subject: [PATCH] [rcore] Fix gamepad code using outdated functions --- src/platforms/rcore_desktop_sdl.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/platforms/rcore_desktop_sdl.c b/src/platforms/rcore_desktop_sdl.c index c96820475..703274aa0 100644 --- a/src/platforms/rcore_desktop_sdl.c +++ b/src/platforms/rcore_desktop_sdl.c @@ -2164,11 +2164,20 @@ int InitPlatform(void) platform.gamepadId[i] = -1; // Set all gamepad initial instance ids as invalid to not conflict with instance id zero } +#if defined(USING_VERSION_SDL3) + int numJoysticks; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks); +#else int numJoysticks = SDL_NumJoysticks(); +#endif for (int i = 0; (i < numJoysticks) && (i < MAX_GAMEPADS); i++) { +#if defined(USING_VERSION_SDL3) + platform.gamepad[i] = SDL_OpenGamepad(joysticks[i]); +#else platform.gamepad[i] = SDL_GameControllerOpen(i); +#endif platform.gamepadId[i] = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(platform.gamepad[i])); if (platform.gamepad[i]) @@ -2177,12 +2186,28 @@ int InitPlatform(void) CORE.Input.Gamepad.axisCount[i] = SDL_JoystickNumAxes(SDL_GameControllerGetJoystick(platform.gamepad[i])); CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_LEFT_TRIGGER] = -1.0f; CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f; - strncpy(CORE.Input.Gamepad.name[i], SDL_GameControllerNameForIndex(i), MAX_GAMEPAD_NAME_LENGTH - 1); +#if defined(USING_VERSION_SDL3) + const char *joystickName = SDL_GetJoystickNameForID(joysticks[i]); +#else + const char *joystickName = SDL_GameControllerNameForIndex(i); +#endif + strncpy(CORE.Input.Gamepad.name[i], joystickName, MAX_GAMEPAD_NAME_LENGTH - 1); CORE.Input.Gamepad.name[i][MAX_GAMEPAD_NAME_LENGTH - 1] = '\0'; } else TRACELOG(LOG_WARNING, "PLATFORM: Unable to open game controller [ERROR: %s]", SDL_GetError()); } + if (numJoysticks > 0) { + TRACELOG(LOG_INFO, "PLATFORM: Available joysticks:"); + } + + for (int i = 0; (i < numJoysticks) && (i < MAX_GAMEPADS); i++) { + if (CORE.Input.Gamepad.ready[i] == false) { + continue; + } + TRACELOG(LOG_INFO, " - [ID %d] %s", i, CORE.Input.Gamepad.name[i]); + } + // Disable mouse events being interpreted as touch events // NOTE: This is wanted because there are SDL_FINGER* events available which provide unique data // Due to the way PollInputEvents() and rgestures.h are currently implemented, setting this won't break SUPPORT_MOUSE_GESTURES