Joysticks and Wrapper Classes

  • joystick_you_want_to_map

     Notice: You can get Jumpsmith here

    It’s really important to me that Jumpsmith works with multiple joystick models, and remembers the button configuration of those models. I’m programming with SDL2, which does have its own struct for joystick data. However, I’ve found it is best in the long run to create what is called a “Wrapper Class” which has an SDL joystick struct as an attribute. This way, you can add more methods and attributes to your joysticks as your game develops. Using these wrapper classes, I was able to whip up a button mapping UI in about an hour. Here’s my story:

    A few nights ago, my Logitech Xbox controller started to fail. The OS was suddenly sending the game “Unplugged” and “Plugged In” events for the joystick, even though the USB was firmly plugged into the side of my old trusty laptop. My other gamepads were working fine… it was time to run to the store.


    Jumpsmith only requires 2 buttons to play, but if you want to make levels with a controller, you want 8 buttons. While I was looking around at Video Game Exchange in Augusta, I realized that an SNES controller had the exact right amount of buttons. They had one with a USB connector. Jackpot!

    Every joystick model that is manufactured has its own button configuration, name, and a unique GUID. The button assignments tend to be pretty random. For instance, if you press the A button on a Retrolink NES USB controller, it tells the game that button 1 was just pressed. If you press A on a Yobo SNES USB controller, it tells the game that button 2 was just pressed. On an Xbox contoller, A is button 0. A few months ago, I manually entered all of the data for the 3 joystick models I owned into a little sqlite3 database. With a new model in my arsenal, I figured it was time to suck it up and make a menu that allows players to configure their own joysticks.

    In the Jumpsmith engine, I have a wrapper class for joysticks, and a separate class for joystick models. When a player plugs in a controller, the game compares the joystick’s GUID to the models that it knows. If the GUID is known, the joystick object copies the attributes of the matching model. If the GUID is not found, it defaults to interpreting the new joystick as if it were an Xbox controller, and it creates a new joystick model object.

    The button mapping UI turned out to be really simple, since I had a healthy codebase. It just asks the player to press the button they want to use for jump, sprint, start, select, etc. Then it stores their choices in their joystick object and in the appropriate joystick model object. The models are saved whenever the game is closed.


    Custom button mapping!

    UPDATE: You can get Jumpsmith here

    Jumpsmith is in the late polishing stages right now. I’m planning on selling it on and submitting it to Steam Greenlight in January 2015. If you have any questions or comments, you can reach me at or on twitter @foundtimegames … also, you can leave a comment here.


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    + three = 7