Skip to content

Commit 486b869

Browse files
committed
Fix gamepad button handling
1 parent d330399 commit 486b869

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/platforms/rcore_desktop_sdl.c

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,7 @@ void PollInputEvents(void)
16931693
CORE.Input.Gamepad.ready[i] = false;
16941694
memset(CORE.Input.Gamepad.name[i], 0, MAX_GAMEPAD_NAME_LENGTH);
16951695
platform.gamepadId[i] = -1;
1696+
break;
16961697
}
16971698
}
16981699
} break;
@@ -1726,8 +1727,15 @@ void PollInputEvents(void)
17261727

17271728
if (button >= 0)
17281729
{
1729-
CORE.Input.Gamepad.currentButtonState[event.jbutton.which][button] = 1;
1730-
CORE.Input.Gamepad.lastButtonPressed = button;
1730+
for (int i = 0; i < MAX_GAMEPADS; i++)
1731+
{
1732+
if (platform.gamepadId[i] == event.jbutton.which)
1733+
{
1734+
CORE.Input.Gamepad.currentButtonState[i][button] = 1;
1735+
CORE.Input.Gamepad.lastButtonPressed = button;
1736+
break;
1737+
}
1738+
}
17311739
}
17321740
} break;
17331741
case SDL_CONTROLLERBUTTONUP:
@@ -1760,8 +1768,15 @@ void PollInputEvents(void)
17601768

17611769
if (button >= 0)
17621770
{
1763-
CORE.Input.Gamepad.currentButtonState[event.jbutton.which][button] = 0;
1764-
if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0;
1771+
for (int i = 0; i < MAX_GAMEPADS; i++)
1772+
{
1773+
if (platform.gamepadId[i] == event.jbutton.which)
1774+
{
1775+
CORE.Input.Gamepad.currentButtonState[i][button] = 0;
1776+
if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0;
1777+
break;
1778+
}
1779+
}
17651780
}
17661781
} break;
17671782
case SDL_CONTROLLERAXISMOTION:
@@ -1781,18 +1796,25 @@ void PollInputEvents(void)
17811796

17821797
if (axis >= 0)
17831798
{
1784-
// SDL axis value range is -32768 to 32767, we normalize it to RayLib's -1.0 to 1.0f range
1785-
float value = event.jaxis.value/(float)32767;
1786-
CORE.Input.Gamepad.axisState[event.jaxis.which][axis] = value;
1787-
1788-
// Register button state for triggers in addition to their axes
1789-
if ((axis == GAMEPAD_AXIS_LEFT_TRIGGER) || (axis == GAMEPAD_AXIS_RIGHT_TRIGGER))
1799+
for (int i = 0; i < MAX_GAMEPADS; i++)
17901800
{
1791-
int button = (axis == GAMEPAD_AXIS_LEFT_TRIGGER)? GAMEPAD_BUTTON_LEFT_TRIGGER_2 : GAMEPAD_BUTTON_RIGHT_TRIGGER_2;
1792-
int pressed = (value > 0.1f);
1793-
CORE.Input.Gamepad.currentButtonState[event.jaxis.which][button] = pressed;
1794-
if (pressed) CORE.Input.Gamepad.lastButtonPressed = button;
1795-
else if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0;
1801+
if (platform.gamepadId[i] == event.jaxis.which)
1802+
{
1803+
// SDL axis value range is -32768 to 32767, we normalize it to RayLib's -1.0 to 1.0f range
1804+
float value = event.jaxis.value/(float)32767;
1805+
CORE.Input.Gamepad.axisState[i][axis] = value;
1806+
1807+
// Register button state for triggers in addition to their axes
1808+
if ((axis == GAMEPAD_AXIS_LEFT_TRIGGER) || (axis == GAMEPAD_AXIS_RIGHT_TRIGGER))
1809+
{
1810+
int button = (axis == GAMEPAD_AXIS_LEFT_TRIGGER)? GAMEPAD_BUTTON_LEFT_TRIGGER_2 : GAMEPAD_BUTTON_RIGHT_TRIGGER_2;
1811+
int pressed = (value > 0.1f);
1812+
CORE.Input.Gamepad.currentButtonState[i][button] = pressed;
1813+
if (pressed) CORE.Input.Gamepad.lastButtonPressed = button;
1814+
else if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0;
1815+
}
1816+
break;
1817+
}
17961818
}
17971819
}
17981820
} break;

0 commit comments

Comments
 (0)