How to know the controls' button?

Hi, I was thinking about make a simple “gui” of the game controls, but, I don’t know how to map the buttons. Forexample: suposse that the player connect a game controller like SNES, how to can I know the button maps in my SDL2 game? Is there something related to this?

Sorry, I cannot explain better because I never implemented something like this, it was something that “came” in my mind.

You can use GameController API to automatically map buttons and other manipulators. The problem, however, is that these types of joystick mappers use a generic set and layout of joysticks:

SDL_GameController is an abstraction for gamepads (“controllers”) similar to the xbox360-pad: They have a DPAD, two analog sticks, 4 buttons on the right (often called A, B, X, Y), shoulder buttons (two of which might be axes) and 3 buttons in the middle (Start, Back and usually some kind of logo-button).

which is incompatible with the layout of the very many gamepads (e.g. Sega Genesis controller — the first random controller on my desk). That’s why automatic mapping is a bit pointless, because you can’t determine the physical layout of the buttons from the code level. It is convenient but flawed, especially since if you want to give the user the ability to remap buttons (because you should), you’ll have to write a configurator for that anyway.

IMO the best solution is to use the Joystick API and write the mapper yourself. Plenty of users will want to change the mapping anyway (at least check how it looks like), so relying on an automatic one isn’t necessary. The more input configuration options you want to give the user, the more code you will have to write, so the less sense in using ready-made mappers.

However, it must be honestly admitted that creating a mapper yourself requires some work. I know because I worked on one myself and the code came out a lot (I didn’t count, but probably about 2k LoC in C-style manner). It doesn’t support auto-assigning buttons (because it’s pointless), but at least the user will be able to map the input the way he wants, for any joystick (or combination of multiple devices for a single player), and I’ll be sure that the mapper will support any joystick — not only the typical ones, but also those adapted for users with various disabilities and even self-built ones (e.g. using Arduino, which is very easy to do nowadays).

So you must first determine what you care about and what you are able to program.

1 Like