Skip to content

Commit

Permalink
Backends: SDL2: Implement multiple gamepad support
Browse files Browse the repository at this point in the history
This makes the SDL2 backend work correctly with multiple gamepads. Instead of
hard-coding gamepad index 0, we keep a list of all currently available
gamepads. When gamepads are added or removed, we update the list accordingly.
When processing gamepad input, we then iterate over this list and combine the
button and axis state of all gamepads.

Note that it's not necessary to enumerate gamepads during initialization
of the backend, because SDL automatically sends a
`SDL_CONTROLLERDEVICEADDED` event for every gamepad that was already
present during initialization of SDL.

The main motivation for this change is that I can have multiple gamepads
connected to my system and grab any one of them at random, and have ImGui
recognize my inputs. Previously, this was not guaranteed, because the gamepad I
grabbed might not have index 0. It's also possible now to (for example) use the
D-pad on one gamepad and and buttons on another one at the same time, although
that seems less useful in practice :)

Additionally, we now properly close all previously opened gamepads when
shutting down the backend.

# Conflicts:
#	backends/imgui_impl_sdl.cpp
  • Loading branch information
lethal-guitar committed Mar 5, 2021
1 parent 1e0ae0b commit 2d4995a
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions backends/imgui_impl_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,6 @@ static void ImGui_ImplSDL2_MapButton(float* nav_input, Uint8 btn_value)
{
*nav_input = 1.0f;
}
if (*nav_input < 0.0f)
{
*nav_input = 0.0f;
}
}

static void ImGui_ImplSDL2_MapAnalog(float* nav_input, Sint16 axis_value, float v0, float v1)
Expand All @@ -364,7 +360,7 @@ static void ImGui_ImplSDL2_UpdateGamepads()
if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
return;

// Update gamepad inputs
// Update "has gamepad" flag
if (g_GameControllers.size() > 0)
{
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
Expand Down

0 comments on commit 2d4995a

Please sign in to comment.