Skip to content

Commit bf13fe1

Browse files
committed
use Gamepad.m_DeviceIndex exclusively for referring to the gamepad name
1 parent 61fc94f commit bf13fe1

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

Source/Managers/UInputMan.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ int UInputMan::Initialize() {
8484
g_ConsoleMan.PrintString("ERROR: Failed to connect gamepad " + std::to_string(index) + " " + std::string(SDL_GetError()));
8585
continue;
8686
}
87-
SDL_SetGamepadPlayerIndex(controller, index);
88-
s_PrevJoystickStates[controllerIndex] = Gamepad(index, joysticks[index], SDL_GAMEPAD_AXIS_COUNT, SDL_GAMEPAD_BUTTON_COUNT);
89-
s_ChangedJoystickStates[controllerIndex] = Gamepad(index, joysticks[index], SDL_GAMEPAD_AXIS_COUNT, SDL_GAMEPAD_BUTTON_COUNT);
87+
SDL_SetGamepadPlayerIndex(controller, controllerIndex);
88+
s_PrevJoystickStates[controllerIndex] = Gamepad(controllerIndex, joysticks[index], SDL_GAMEPAD_AXIS_COUNT, SDL_GAMEPAD_BUTTON_COUNT);
89+
s_ChangedJoystickStates[controllerIndex] = Gamepad(controllerIndex, joysticks[index], SDL_GAMEPAD_AXIS_COUNT, SDL_GAMEPAD_BUTTON_COUNT);
9090
auto playerScheme = std::find_if(m_ControlScheme.begin(), m_ControlScheme.end(), [controllerIndex](auto& scheme) { return scheme.GetDevice() == controllerIndex + InputDevice::DEVICE_GAMEPAD_1; });
9191
playerScheme->SetDeviceID({.gamepad = joysticks[index]});
9292
controllerIndex++;
@@ -97,8 +97,8 @@ int UInputMan::Initialize() {
9797
g_ConsoleMan.PrintString("ERROR: Failed to connect joystick.");
9898
continue;
9999
}
100-
s_PrevJoystickStates[controllerIndex] = Gamepad(index, joysticks[index], SDL_GetNumJoystickAxes(joy), SDL_GetNumJoystickButtons(joy));
101-
s_ChangedJoystickStates[controllerIndex] = Gamepad(index, joysticks[index], SDL_GetNumJoystickAxes(joy), SDL_GetNumJoystickButtons(joy));
100+
s_PrevJoystickStates[controllerIndex] = Gamepad(controllerIndex, joysticks[index], SDL_GetNumJoystickAxes(joy), SDL_GetNumJoystickButtons(joy));
101+
s_ChangedJoystickStates[controllerIndex] = Gamepad(controllerIndex, joysticks[index], SDL_GetNumJoystickAxes(joy), SDL_GetNumJoystickButtons(joy));
102102
auto playerScheme = std::find_if(m_ControlScheme.begin(), m_ControlScheme.end(), [controllerIndex](auto& scheme) { return scheme.GetDevice() == controllerIndex + InputDevice::DEVICE_GAMEPAD_1; });
103103
playerScheme->SetDeviceID({.gamepad = joysticks[index]});
104104
controllerIndex++;
@@ -1284,25 +1284,25 @@ void UInputMan::UpdateJoystickDigitalAxis() {
12841284
}
12851285
}
12861286

1287-
void UInputMan::HandleGamepadHotPlug(SDL_JoystickID deviceIndex) {
1287+
void UInputMan::HandleGamepadHotPlug(SDL_JoystickID joystickID) {
12881288
SDL_Joystick* controller = nullptr;
12891289
int controllerIndex = 0;
12901290

12911291
for (controllerIndex = 0; controllerIndex < s_PrevJoystickStates.size(); ++controllerIndex) {
1292-
if (s_PrevJoystickStates[controllerIndex].m_JoystickID == deviceIndex) {
1292+
if (s_PrevJoystickStates[controllerIndex].m_JoystickID == joystickID) {
12931293
return;
12941294
}
12951295
if (s_PrevJoystickStates[controllerIndex].m_JoystickID == -1) {
1296-
if (SDL_IsGamepad(deviceIndex)) {
1297-
SDL_Gamepad* gameController = SDL_OpenGamepad(deviceIndex);
1296+
if (SDL_IsGamepad(joystickID)) {
1297+
SDL_Gamepad* gameController = SDL_OpenGamepad(joystickID);
12981298
if (!gameController) {
12991299
g_ConsoleMan.PrintString("ERROR: Failed to connect Gamepad!");
13001300
break;
13011301
}
13021302
controller = SDL_GetGamepadJoystick(gameController);
13031303
SDL_SetGamepadPlayerIndex(gameController, controllerIndex);
13041304
} else {
1305-
controller = SDL_OpenJoystick(deviceIndex);
1305+
controller = SDL_OpenJoystick(joystickID);
13061306
}
13071307
if (!controller) {
13081308
g_ConsoleMan.PrintString("ERROR: Failed to connect Gamepad!");
@@ -1320,15 +1320,15 @@ void UInputMan::HandleGamepadHotPlug(SDL_JoystickID deviceIndex) {
13201320
if (controller) {
13211321
int numAxis = 0;
13221322
int numButtons = 0;
1323-
if (SDL_IsGamepad(deviceIndex)) {
1323+
if (SDL_IsGamepad(joystickID)) {
13241324
numAxis = SDL_GAMEPAD_AXIS_COUNT;
13251325
numButtons = SDL_GAMEPAD_BUTTON_COUNT;
13261326
} else {
13271327
numAxis = SDL_GetNumJoystickAxes(controller);
13281328
numButtons = SDL_GetNumJoystickButtons(controller);
13291329
}
1330-
s_PrevJoystickStates[controllerIndex] = Gamepad(controllerIndex, deviceIndex, numAxis, numButtons);
1331-
s_ChangedJoystickStates[controllerIndex] = Gamepad(controllerIndex, deviceIndex, numAxis, numButtons);
1330+
s_PrevJoystickStates[controllerIndex] = Gamepad(controllerIndex, joystickID, numAxis, numButtons);
1331+
s_ChangedJoystickStates[controllerIndex] = Gamepad(controllerIndex, joystickID, numAxis, numButtons);
13321332
m_NumJoysticks++;
13331333
}
13341334
}

Source/Managers/UInputMan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,8 @@ namespace RTE {
599599
void UpdateJoystickDigitalAxis();
600600

601601
/// Connect a joystick or gamepad device and add it to the joystick list if a slot is available (up to max player count).
602-
/// @param deviceIndex The device index (generated by the connected event or a value up to SDL_NumJoysticks()).
603-
void HandleGamepadHotPlug(SDL_JoystickID deviceIndex);
602+
/// @param joystickID The SDL_JoystickID of the added Gamepad, usually from the corresponding device event.
603+
void HandleGamepadHotPlug(SDL_JoystickID joystickID);
604604
#pragma endregion
605605

606606
/// Clears all the member variables of this UInputMan, effectively resetting the members of this abstraction level only.

Source/System/Gamepad.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace RTE {
77

88
/// Structure for storing SDL_Gamepad or SDL_Joystick states.
99
struct Gamepad {
10-
int m_DeviceIndex = -1; //!< The SDL device index.
10+
int m_DeviceIndex = -1; //!< The internal device index.
1111
SDL_JoystickID m_JoystickID = -1; //!< The joystick ID for event handling.
1212
std::vector<int> m_Axis; //!< Array of analog axis states.
1313
std::vector<int> m_DigitalAxis; //!< Array of digital axis states. Should be updated when analog axis crosses half value 8192.

0 commit comments

Comments
 (0)