Two players gamepad handling

Hi,
I’m a little confused about the Game Controller API usage. I wanted to learn about it by coding a 2 players Pong game, using 2 wired (usb) Xbox 360 controllers. I planned to use the left sticks of each of the 2 controllers to move each players paddle. I also wanted to make it possible to plug / unplug the controllers while the program was running for a plug and play experience.

So far I have two major problems :

1 - SDL_CONTROLLERDEVICEADDED and SDL_CONTROLLERDEVICEREMOVED seem unreliables. I made a simple test by printf “plugged” and “unplugged” in response to these events.
Two use cases :
a - When I launch the game with a controller plugged in, I receive a SDL_CONTROLLERDEVICEADDED event. Then when I unplug it, I receive a SDL_CONTROLLERDEVICEREMOVED event. So far so good. But after that, I only receive SDL_CONTROLLERDEVICEADDED events. No more unplug notifications.
b - When I lauch the game with no controller plugged in, things are slightly different. I never receive any SDL_CONTROLLERDEVICEREMOVED events. The first time I plug a controller in, I get a SDL_CONTROLLERDEVICEADDED event. The second time I plug it in, I receive 2 SDL_CONTROLLERDEVICEADDED event. Then I receive one SDL_CONTROLLERDEVICEADDED event evey time I plug it back in. What is the correct way (if there is any) to handle pluggin / unplugging each controller an arbitrary number of time during program execution ?

2 - I would like associate each controller with each player so I know which paddle to move when I receive controller button / stick movement events. Ideally, this would conbine with handling pluggin / unplugging controllers in the following way :

No controller connected at program lauch. First controller plugged in → associated with player 1. Second controller plugged in → associated with player 2. First controller unplugged → Second controller still associated with player 2. First controller re-plugged → re-associated with player 1. Second controller unplugged. First controller still associated with player 1. Second controller re-plugged → re-associated with player 2.

Is it actually possible and how to achieve something like this ? Thank you for taking the time to read this message and feel free to ask if you need more details to understand the situation.

1 Like