Skip to content

Commit

Permalink
Only call SDL_NumJoysticks() once per loop
Browse files Browse the repository at this point in the history
It does non-trivial work when using sdl2-compat.
  • Loading branch information
cgutman committed Nov 13, 2024
1 parent 707dd3c commit 15e337f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions app/gui/sdlgamepadkeynavigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ void SdlGamepadKeyNavigation::enable()
SDL_FlushEvent(SDL_CONTROLLERDEVICEADDED);

// Open all currently attached game controllers
for (int i = 0; i < SDL_NumJoysticks(); i++) {
int numJoysticks = SDL_NumJoysticks();
for (int i = 0; i < numJoysticks; i++) {
if (SDL_IsGameController(i)) {
SDL_GameController* gc = SDL_GameControllerOpen(i);
if (gc != nullptr) {
Expand Down Expand Up @@ -289,7 +290,8 @@ int SdlGamepadKeyNavigation::getConnectedGamepads()
Q_ASSERT(m_Enabled);

int count = 0;
for (int i = 0; i < SDL_NumJoysticks(); i++) {
int numJoysticks = SDL_NumJoysticks();
for (int i = 0; i < numJoysticks; i++) {
if (SDL_IsGameController(i)) {
count++;
}
Expand Down
6 changes: 4 additions & 2 deletions app/streaming/input/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,8 @@ QString SdlInputHandler::getUnmappedGamepads()
MappingManager mappingManager;
mappingManager.applyMappings();

for (int i = 0; i < SDL_NumJoysticks(); i++) {
int numJoysticks = SDL_NumJoysticks();
for (int i = 0; i < numJoysticks; i++) {
if (!SDL_IsGameController(i)) {
char guidStr[33];
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
Expand Down Expand Up @@ -973,7 +974,8 @@ int SdlInputHandler::getAttachedGamepadMask()
}

count = mask = 0;
for (int i = 0; i < SDL_NumJoysticks(); i++) {
int numJoysticks = SDL_NumJoysticks();
for (int i = 0; i < numJoysticks; i++) {
if (SDL_IsGameController(i)) {
char guidStr[33];
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
Expand Down

0 comments on commit 15e337f

Please sign in to comment.