Skip to content

Commit 23836d1

Browse files
authored
Merge pull request #205 from cortex-command-community/joystick-fixes
Fix Gamepads getting recognized twice
2 parents 60f8261 + 80d89c3 commit 23836d1

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ LogLoadingWarning.txt
102102
LogConsole.txt
103103
Console.dump.log
104104
Console.input.log
105+
imgui.ini

Source/Managers/UInputMan.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "UInputMan.h"
22
#include "Constants.h"
3-
#include "SDL3/SDL_events.h"
4-
#include "SDL3/SDL_keyboard.h"
5-
#include "SDL3/SDL_mouse.h"
63
#include "SceneMan.h"
74
#include "ActivityMan.h"
85
#include "MetaMan.h"
@@ -14,6 +11,7 @@
1411
#include "Icon.h"
1512
#include "GameActivity.h"
1613
#include "System.h"
14+
1715
#include <SDL3/SDL.h>
1816
#include <string>
1917
#include <unordered_map>
@@ -82,7 +80,7 @@ int UInputMan::Initialize() {
8280

8381
int controllerIndex = 0;
8482
int joystickCount = 0;
85-
SDL_JoystickID* joysticks = SDL_GetJoysticks(&joystickCount);
83+
SDL_JoystickID* joysticks = SDL_GetGamepads(&joystickCount);
8684

8785
for (size_t index = 0; index < std::min(joystickCount, static_cast<int>(Players::MaxPlayerCount)); ++index) {
8886
if (SDL_IsGamepad(joysticks[index])) {
@@ -997,11 +995,12 @@ int UInputMan::Update() {
997995
}
998996
}
999997
break;
1000-
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
1001-
case SDL_EVENT_GAMEPAD_BUTTON_UP:
1002998
case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
1003999
case SDL_EVENT_JOYSTICK_BUTTON_UP:
1004-
if (std::vector<Gamepad>::iterator device = std::find(s_PrevJoystickStates.begin(), s_PrevJoystickStates.end(), (inputEvent.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN || inputEvent.type == SDL_EVENT_GAMEPAD_BUTTON_UP) ? inputEvent.gbutton.which : inputEvent.jbutton.which); device != s_PrevJoystickStates.end()) {
1000+
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
1001+
case SDL_EVENT_GAMEPAD_BUTTON_UP: {
1002+
bool joystickEvent = (inputEvent.type == SDL_EVENT_JOYSTICK_BUTTON_DOWN || inputEvent.type == SDL_EVENT_JOYSTICK_BUTTON_UP);
1003+
if (std::vector<Gamepad>::iterator device = std::find(s_PrevJoystickStates.begin(), s_PrevJoystickStates.end(), joystickEvent ? inputEvent.jbutton.which : inputEvent.gbutton.which); device != s_PrevJoystickStates.end()) {
10051004
int button = -1;
10061005
int down = false;
10071006
if (SDL_IsGamepad(device->m_DeviceIndex)) {
@@ -1020,10 +1019,14 @@ int UInputMan::Update() {
10201019
device->m_Buttons[button] = down;
10211020
}
10221021
break;
1022+
}
10231023
case SDL_EVENT_JOYSTICK_ADDED:
1024+
case SDL_EVENT_GAMEPAD_ADDED: {
10241025
HandleGamepadHotPlug(inputEvent.jdevice.which);
10251026
break;
1027+
}
10261028
case SDL_EVENT_JOYSTICK_REMOVED:
1029+
case SDL_EVENT_GAMEPAD_REMOVED:
10271030
if (std::vector<Gamepad>::iterator prevDevice = std::find(s_PrevJoystickStates.begin(), s_PrevJoystickStates.end(), inputEvent.jdevice.which); prevDevice != s_PrevJoystickStates.end()) {
10281031
g_ConsoleMan.PrintString("INFO: Gamepad " + std::to_string(prevDevice->m_DeviceIndex + 1) + " disconnected!");
10291032
SDL_CloseGamepad(SDL_GetGamepadFromID(prevDevice->m_JoystickID));
@@ -1281,17 +1284,19 @@ void UInputMan::UpdateJoystickDigitalAxis() {
12811284
}
12821285
}
12831286

1284-
void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
1287+
void UInputMan::HandleGamepadHotPlug(SDL_JoystickID deviceIndex) {
12851288
SDL_Joystick* controller = nullptr;
12861289
int controllerIndex = 0;
12871290

12881291
for (controllerIndex = 0; controllerIndex < s_PrevJoystickStates.size(); ++controllerIndex) {
1289-
if (s_PrevJoystickStates[controllerIndex].m_DeviceIndex == deviceIndex || s_PrevJoystickStates[controllerIndex].m_DeviceIndex == -1) {
1292+
if (s_PrevJoystickStates[controllerIndex].m_JoystickID == deviceIndex) {
1293+
return;
1294+
}
1295+
if (s_PrevJoystickStates[controllerIndex].m_JoystickID == -1) {
12901296
if (SDL_IsGamepad(deviceIndex)) {
12911297
SDL_Gamepad* gameController = SDL_OpenGamepad(deviceIndex);
12921298
if (!gameController) {
1293-
std::string connectString = s_PrevJoystickStates[controllerIndex].m_DeviceIndex == deviceIndex ? "reconnect" : "connect";
1294-
g_ConsoleMan.PrintString("ERROR: Failed to " + connectString + " Gamepad " + std::to_string(controllerIndex + 1));
1299+
g_ConsoleMan.PrintString("ERROR: Failed to connect Gamepad!");
12951300
break;
12961301
}
12971302
controller = SDL_GetGamepadJoystick(gameController);
@@ -1300,12 +1305,10 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
13001305
controller = SDL_OpenJoystick(deviceIndex);
13011306
}
13021307
if (!controller) {
1303-
std::string connectString = s_PrevJoystickStates[controllerIndex].m_DeviceIndex == deviceIndex ? "reconnect" : "connect";
1304-
g_ConsoleMan.PrintString("ERROR: Failed to " + connectString + " Gamepad " + std::to_string(controllerIndex + 1));
1308+
g_ConsoleMan.PrintString("ERROR: Failed to connect Gamepad!");
13051309
break;
13061310
}
1307-
std::string connectString = s_PrevJoystickStates[controllerIndex].m_DeviceIndex == deviceIndex ? " reconnected" : " connected";
1308-
g_ConsoleMan.PrintString("INFO: Gamepad " + std::to_string(controllerIndex + 1) + connectString);
1311+
g_ConsoleMan.PrintString("INFO: Gamepad " + std::to_string(controllerIndex + 1) + "connected.");
13091312
break;
13101313
}
13111314
}
@@ -1315,7 +1318,6 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
13151318
}
13161319

13171320
if (controller) {
1318-
SDL_JoystickID id = SDL_GetJoystickID(controller);
13191321
int numAxis = 0;
13201322
int numButtons = 0;
13211323
if (SDL_IsGamepad(deviceIndex)) {
@@ -1325,8 +1327,8 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
13251327
numAxis = SDL_GetNumJoystickAxes(controller);
13261328
numButtons = SDL_GetNumJoystickButtons(controller);
13271329
}
1328-
s_PrevJoystickStates[controllerIndex] = Gamepad(deviceIndex, id, numAxis, numButtons);
1329-
s_ChangedJoystickStates[controllerIndex] = Gamepad(deviceIndex, id, numAxis, numButtons);
1330+
s_PrevJoystickStates[controllerIndex] = Gamepad(controllerIndex, deviceIndex, numAxis, numButtons);
1331+
s_ChangedJoystickStates[controllerIndex] = Gamepad(controllerIndex, deviceIndex, numAxis, numButtons);
13301332
m_NumJoysticks++;
13311333
}
13321334
}

Source/Managers/UInputMan.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "Constants.h"
4+
#include "SDL3/SDL_joystick.h"
45
#include "SDL3/SDL_keycode.h"
56
#include "SDL3/SDL_scancode.h"
67
#include "Singleton.h"
@@ -606,7 +607,7 @@ namespace RTE {
606607

607608
/// Connect a joystick or gamepad device and add it to the joystick list if a slot is available (up to max player count).
608609
/// @param deviceIndex The device index (generated by the connected event or a value up to SDL_NumJoysticks()).
609-
void HandleGamepadHotPlug(int deviceIndex);
610+
void HandleGamepadHotPlug(SDL_JoystickID deviceIndex);
610611
#pragma endregion
611612

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

imgui.ini

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)