Using a gamepad for development?

Monday, April 09, 2007 4 Comments

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).

IntPtr mJoy;
Tao.Sdl.Sdl.SDL_Joystick mJoystick;
...
Sdl.SDL_Init(Tao.Sdl.Sdl.SDL_INIT_JOYSTICK);
mJoy = Sdl.SDL_JoystickOpen(0);
mJoystick = (Sdl.SDL_Joystick)
System.Runtime.InteropServices.Marshal.PtrToStructure(
mJoy, typeof(Sdl.SDL_Joystick));

And getting joystick's input:

Sdl.SDL_JoystickUpdate();

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.
Enjoy!

4 comments:

  1. Sounds a bit freaking, but I myself can't see neither why a gaming controller can't be a good aid in other fields. 3D mouses and the like are being used nowadays for non-gaming purposes (I think I've seen a demo of Google Earth controlled with one). Rejecting using a different controller just because it was first thought for gaming is an error in my opinion.
    What you said about using the Wiimote for presentations is something I already had in mind for my graduation project presentation, by the way! :P

    PS: The last 5-10 secs of the video are a good inspiration for a possible easter egg in Plastic. You like racing games; sure you've already guessed how the game might look ;) "Racersitory" might be a good name!

    ReplyDelete
  2. Fun & great!

    Now you can make you own Tron-like game running inside your branches and merges.
    Your game tracks will evolve while your repository is growing!
    And even better if you support you wheel race controller.

    ;)

    ReplyDelete
  3. Have just read about this:
    http://blogs.msdn.com/coding4fun/archive/2007/03/14/1879033.aspx

    In case you're interested on this in the future...

    ReplyDelete
  4. What about force feedback support?

    ReplyDelete