Friday, 23 January 2015

Configure Game Controllers for Wolf4SDL

In this tutorial, I will show how to alter the source code to configure game controllers for the Wolfenstein 3D port, Wolf4SDL .

Please note that this hack has only been tested on the PS3 controller and will remap buttons only on your controller, not analogue sticks.

Wolf4SDL does come with a default game controller mapping so if you do not have a PS3 controller (or similar), then I suggest that you try your game controller to find what the mapping is and remap if required.

To do this, enable your game controller on your pi, then launch Wolf4SDL. In the launch screen, the joystick option should be lit as in the screenshot above. Press any button on your controller to start the game. In game, make a note of which buttons are mapped to which action. The actions in Wolfenstein 3D are given below, if you're unfamiliar with the game.

Find Game Controller Button Numbers

If you do not know the button numbers of your controller then you need to use the joystick app which you can install with the command:

apt get install joystick

To launch the utility, run the command.

jstest /dev/input/js0

Press a button on your controller and make a note of which number changes in the screen output. Press CTRL+C to exit.

Configure your Game Controller

By default, the left analogue stick on the PS3 controller is already configured for walking forwards and backwards and turning left and right.

The code to remap the buttons on your games controllers are lines 61 to 63 in the file wl_play.cpp. To edit the file, run the following commands.

cd wolf4sdl-master
sudo nano wl_play.cpp 

The existing code is reproduced below. I have added the blue numbers in brackets to make clear what the default game controller mapping is. They do not appear in the original code.

    bt_attack (0), bt_strafe (1), bt_use
(2), bt_run (3), bt_strafeleft (4), bt_straferight (5), bt_esc (6), bt_pause (7),
(8), bt_nextweapon (9), bt_nobutton (10), bt_nobutton (11), bt_nobutton (12), bt_nobutton (13), bt_nobutton (14), bt_nobutton (15),

The code says that button 0 (SELECT on the PS3 controller) fires weapon, button 1 (L3) strafes, button 2 (R3) opens doors and so on.

The default configuration is pretty much unplayable so the mapping below is the one I decided to use with my PS3 controller (It may not be ideal for everyone so feel free to alter it to suit).

Action Wolf4sdl code
PS3 Controller
Pause key bt_pause
START (button 3)
ESC key bt_escape
SELECT (button 0)
Next weapon bt_nextweapon
D-Pad Up (button 4)
Previous weapon
D-Pad Down (button 6)
Fire weapon bt_attack
R2 (button 9)
Strafe (with left stick) bt_strafe
L1 (button 10
Open doors bt_use
X button (button 14)
Run / Fast turn (with left stick) bt_run
R1 (button 11)
Strafe left bt_strafeleft
D-Pad Left (button 7)
Strafe right bt_straferight
D-Pad Right (button 5)

Once I decided the mapping, I rearranged the actions (bt_pause, bt_escape etc.) into numerical order for button 0, button 1 and so on. Any unused buttons, e.g. button 1, was assigned bt_nobutton. As a check, there should have been 6 buttons assigned bt_nobutton in the final version.

For the PS3 controller, the new code I used was as follows:

    bt_esc, bt_nobutton, bt_nobutton, bt_pause, bt_nextweapon, bt_straferight, bt_prevweapon, bt_strafeleft,
    bt_nobutton, bt_attack, bt_strafe, bt_run, bt_nobutton, bt_nobutton, bt_use, bt_nobutton,

I replaced the existing code with the above and saved wl_play.cpp.  Then I recompiled the source code by running the command make.

Other Controllers

As mentioned earlier, this hack works for the PS3 and similar controllers. Please let me know how other controllers work in the comments. It would be great if you could post your code changes. I can update this section for the benefit of others as I receive info.