GamepadsSDL 3.2
SDL_OpenGamepad
Opens a connected gamepad so you can read its sticks and buttons.
Signature
SDL_Gamepad * SDL_OpenGamepad(SDL_JoystickID instance_id);
How it works
SDL knows the layout of hundreds of controllers and maps them all to one standard pad: two sticks, two triggers, a D-pad and four face buttons named by position (SOUTH, EAST, WEST, NORTH). Your code reads SOUTH and works with an Xbox A, a PlayStation Cross and a Switch B alike.
Open pads when they arrive: SDL sends SDL_EVENT_GAMEPAD_ADDED for every pad already plugged in at startup and for every one plugged in later, with the ID in e.gdevice.which. Requires SDL_INIT_GAMEPAD.
Parameters
instance_idSDL_JoystickID
The device ID, from e.gdevice.which.
Return value
The gamepad, or NULL on failure.
Example
example.cpp
1case SDL_EVENT_GAMEPAD_ADDED:
2 if (!pad) pad = SDL_OpenGamepad(e.gdevice.which);
3 break;
4case SDL_EVENT_GAMEPAD_REMOVED:
5 if (pad && SDL_GetGamepadID(pad) == e.gdevice.which) {
6 SDL_CloseGamepad(pad);
7 pad = nullptr;
8 }
9 break;