When implementing controller support in a game, it’s desirable for gamepads to
just work without a lot of user configuration. Platform APIs are pretty useless
for this; the solution is an API like SDL’s Gamepad API that allows you to
target a large number of gamepads without much effort.
Each operating system has its own API for gamepad input. Windows has XInput, and
Linux has the joystick and evdev APIs. When a gamepad button is pressed,
applications will receive a button id. This is a number, there’s no OS way to
know which button id corresponds with which button. The ids for a button are not
the same on different gamepads and platforms, making it super hard to support
more than a couple of devices.
{% code_tabs “SDL3”, “SDL2” %}
if (SDL_GetJoystickButton(joystick, 8)) {
std::cerr << "no idea what button 8 is" << std::endl;
}
if (SDL_JoystickGetButton(joystick, 8)) {
std::cerr << "no idea what button 8 is" << std::endl;
}
{% endcode_tabs %}
One thing platforms do give you is the name, model, and manufacturer
of the game controller. If you test with a large number of gamepads, you can
create a database from gamepad name to layout. Luckily, SDL has already done
this for you. Instead of a random number, you can use a named button that will
work no matter the gamepad and platform:
{% code_tabs “SDL3”, “SDL2” %}
if (SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH)) {
std::cerr << "The bottom face button (A on Xbox) was pressed!" << std::endl;
}
if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_A)) {
std::cerr << "The bottom face button (A on Xbox) was pressed!" << std::endl;
}
{% endcode_tabs %}
This article was originally written for SDL2’s SDL_GameController API. It
has now been updated for SDL3. If you’re updating from SDL2 to 3, see
SDL’s migration guide.