Using a gamepad for development?Last weekend I had the chance to spend some time playing with two really interesting gaming consoles: the XBox 360 and Nintendo's Wii. Needless to say I was really surprised by Wii's gaming capabilities, and its totally new (IMHO) approach to digital entertaiment.
And by the way I liked the 360 too. I love racing games, and I was playing Gothan Racing, which I suppose is not the best of breed, but it has better graphics than the ones rendered by my silent (and very CPU limited) living room's PC.
After using both the XBox's gamepad and the Wii remote for a while, the following idea came to my mind: what about using a gamepad (or even the Wii's remote) for software development? Yes, the idea sounds crazy, but adding new peripherals to the programmer's toolset would be great provided them would give an added value and some fun. Obviously I don't see myself using a gamepad to scroll my editor up and down, but I would definitely use a Wii-like remote during a slideshow demo. And, who knows, maybe there are some other areas where new input devices can help. Would you imagine next Code Complete front cover introducing a gamepad? :-P
Then I started trying to control Plastic's 3D Version Tree with a spare gamepad I have at home...
Step 1: how do you capture joystick input using C#? I have to admit it has been a long way since the days I used to write C++ 3D code with OpenGL, DirectX, DirectInput and the like, so I was a bit lost... Anyway, what about trying with the Tao libraries (the ones we are currently using to interface with OpenGL libraries both under Windows and Linux systems)? Yes, using SDL it was possible. So I googled some samples (unfortunately C# samples capturing joystick input were not available with Tao, so I looked for C code ones) and wrote the following small WinForms application.
Retrieving gamepad's input using SDL is pretty simple: you just have to initialize the joystick and then poll for some data (I didn't fiddle with events... yet).
mJoy = Sdl.SDL_JoystickOpen(0);
mJoystick = (Sdl.SDL_Joystick)
And getting joystick's input:
progressBar0X.Value = offset + Sdl.SDL_JoystickGetAxis(mJoy, 0);
progressBar0Y.Value = offset + Sdl.SDL_JoystickGetAxis(mJoy, 1);
progressBar1X.Value = offset + Sdl.SDL_JoystickGetAxis(mJoy, 2);
progressBar1Y.Value = offset + Sdl.SDL_JoystickGetAxis(mJoy, 3);
The full source code is available here.
Once it was running smoothly the next step was modifying the 3D version tree.
And it was pretty simple too. Just replacing mouse code with joystick code and adding a reference to Tao.SDL.dll. By the way, don't forget to download the original SDL.dll library and place it correctly.
The following video shows both the gamepad and the 3D version tree in action.