Controller mapping

Controller mapping

Postby bzt » 10 Mar 2022, 17:41

Hi,

I've seen @dulsi's topic on the new forum (I still can't post there, sorry).

dulsi {l Wrote}:Does anyone know if SDL2 uses equivalent mappings of axis/buttons on Xbox or other controllers?
No, they are using different mappings. Even worse, the same controller has different mappings depending on the OS.

dulsi {l Wrote}:Obviously I want to make a configuration screen but if I don't have time I'd like to know if it is usable by others.
FYI, this isn't mentioned on the linked discourse page, neither in the SDL_GameControllerAddMapping documentation, but you don't have to mess around and generate the mapping string yourself.

First, there's a ready-to-use tool in SDL, called controllermap.c.

Second, there's a community maintained database of game controller mapping strings here (with links to some 3rd party string generator tools as well). Just download gamecontrollerdb.txt and pass that to SDL_GameControllerAddMappingsFromFile as-is. It is well-tested and gives support for many many game controllers out-of-the-box to your game.

If you don't want an additional file, then you can embed that db string into your game using the "FromRW" variant in conjunction with "SDL_RWFromMem".
{l Code}: {l Select All Code}
char gamecontrollerdb[] = " /* paste the contents of gamecontrollerdb.txt here */ ";
SDL_GameControllerAddMappingsFromRW(SDL_RWFromMem(gamecontrollerdb, sizeof(gamecontrollerdb)-1), 0);

Cheers,
bzt
User avatar
bzt
 
Posts: 332
Joined: 23 May 2021, 21:46

Re: Controller mapping

Postby dulsi » 11 Mar 2022, 13:09

I did stumble on the gamecontrollerdb.txt. My PS4 controller was recognized without adding that. I guess SDL must have some default mapping for the major brands.

EDIT: PS4 was recognized. "with" should have been "without". Text corrected.
Last edited by dulsi on 11 Mar 2022, 16:08, edited 1 time in total.
dulsi
 
Posts: 570
Joined: 18 Feb 2016, 15:24

Re: Controller mapping

Postby bzt » 11 Mar 2022, 14:15

dulsi {l Wrote}:I did stumble on the gamecontrollerdb.txt. My PS4 controller was recognized with adding that.
Hmm, PS4 should be supported without the gamecontrollerdb.txt.
dulsi {l Wrote}:I guess SDL must have some default mapping for the major brands.
Yes, it does. However the default mapping list is very limited, and it highly depends on how you compile SDL. My guess is, the default SDL shared library on your distro was compiled without it, that's why you needed the gamecontrollerdb.txt for the PS4 controller too.

Anyway, the best approach is to load the database on your own, that will always work, no matter the platform, and if the default table is compiled in or not. The txt has a lot more mappings than the default anyway, for Linux, it has more than 450, and for Windows, more than 710 controller mappings.

Just a sidenote, a neat trick if you're using stb_image.h to decode PNG images, then you can compress the database file with gzip, and uncompress using stb_image. That way the whole database will only increase your executable's size about 33Kb, no more.
{l Code}: {l Select All Code}
cat gamecontrollerdb.txt | gzip >gamecontrollerdb             # compress as a stream, that creates a shorter, exactly 14 bytes long header ;-)
ld -r -b binary -o gamecontrollerdb.o gamecontrollerdb        # convert binary to object so that we can access it from C as extern char array
and
{l Code}: {l Select All Code}
/* you can use a normal char array too of course */
extern char _binary_gamecontrollerdb_start, _binary_gamecontrollerdb_end;

/* use stb_image's internal PNG decoder to uncompress. The + 14 is to skip over gzip header */
ptr = (char*)stbi_zlib_decode_malloc_guesssize_headerflag(
    (const char*)&_binary_gamecontrollerdb_start + 14,
    &_binary_gamecontrollerdb_end - &_binary_gamecontrollerdb_start - 14,
    4096, (int*)&len, 0);
if(ptr) {
    SDL_GameControllerAddMappingsFromRW(SDL_RWFromMem(ptr, len), 0);
    free(ptr);
}
Of course you can link with zlib and use uncompress2 too, but why I like this hack because it does not need any additional dependency :-)

Cheers,
bzt
User avatar
bzt
 
Posts: 332
Joined: 23 May 2021, 21:46

Who is online

Users browsing this forum: No registered users and 1 guest