Only call SDL_NumJoysticks() once per loop

It does non-trivial work when using sdl2-compat.
This commit is contained in:
Cameron Gutman 2024-11-12 23:52:31 -06:00
parent 707dd3cb83
commit 15e337fff8
2 changed files with 8 additions and 4 deletions

View File

@ -55,7 +55,8 @@ void SdlGamepadKeyNavigation::enable()
SDL_FlushEvent(SDL_CONTROLLERDEVICEADDED); SDL_FlushEvent(SDL_CONTROLLERDEVICEADDED);
// Open all currently attached game controllers // 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)) { if (SDL_IsGameController(i)) {
SDL_GameController* gc = SDL_GameControllerOpen(i); SDL_GameController* gc = SDL_GameControllerOpen(i);
if (gc != nullptr) { if (gc != nullptr) {
@ -289,7 +290,8 @@ int SdlGamepadKeyNavigation::getConnectedGamepads()
Q_ASSERT(m_Enabled); Q_ASSERT(m_Enabled);
int count = 0; 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)) { if (SDL_IsGameController(i)) {
count++; count++;
} }

View File

@ -913,7 +913,8 @@ QString SdlInputHandler::getUnmappedGamepads()
MappingManager mappingManager; MappingManager mappingManager;
mappingManager.applyMappings(); 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)) { if (!SDL_IsGameController(i)) {
char guidStr[33]; char guidStr[33];
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i), SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
@ -973,7 +974,8 @@ int SdlInputHandler::getAttachedGamepadMask()
} }
count = mask = 0; 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)) { if (SDL_IsGameController(i)) {
char guidStr[33]; char guidStr[33];
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i), SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),