Add gamepad switching handling
This commit is contained in:
parent
d20ee6ce9a
commit
0621e9c993
1 changed files with 13 additions and 1 deletions
14
main.c
14
main.c
|
@ -202,7 +202,14 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
|||
SDL_GamepadDeviceEvent device_event = event->gdevice;
|
||||
SDL_JoystickID joystick_id = device_event.which;
|
||||
SDL_Log("Gamepad ID %d added", joystick_id);
|
||||
state->gamepad = SDL_OpenGamepad(joystick_id); // TODO: store multiple gamepads in state
|
||||
// TODO: handle multiple gamepads in state
|
||||
if (state->gamepad) {
|
||||
SDL_Log("Closing gamepad");
|
||||
SDL_CloseGamepad(state->gamepad);
|
||||
state->gamepad = NULL;
|
||||
}
|
||||
SDL_Log("Opening gamepad");
|
||||
state->gamepad = SDL_OpenGamepad(joystick_id);
|
||||
if (!state->gamepad) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to open gamepad: %s", SDL_GetError());
|
||||
}
|
||||
|
@ -212,6 +219,11 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
|||
SDL_GamepadDeviceEvent device_event = event->gdevice;
|
||||
SDL_JoystickID joystick_id = device_event.which;
|
||||
SDL_Log("Gamepad ID %d removed", joystick_id);
|
||||
if (state->gamepad && SDL_GetGamepadID(state->gamepad) == joystick_id) {
|
||||
SDL_Log("Closing gamepad");
|
||||
SDL_CloseGamepad(state->gamepad);
|
||||
state->gamepad = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SDL_EVENT_GAMEPAD_BUTTON_DOWN: {
|
||||
|
|
Loading…
Reference in a new issue