Getting Joystick Input


The joystick device is the odd duck of the interfaces. Since a joystick or game controller sitting on one or more joystick ports on a player s system could be any of the vast array of such items available, attaching to a joystick becomes more involved. Before we can create and configure a joystick device, we need to discover what joysticks are available and which of them we want to use. The InputObject class that we use in creating the keyboard and mouse devices also has a method for enumerating all of the joysticks configured on the system. The code to enumerate joysticks looks like Listing 2 “1.

Listing 2.1: Joystick Enumeration
start example
 // Enumerate joysticks in the system.  foreach(DeviceInstance instance in Manager.GetDevices(DeviceClass.GameControl,         EnumDevicesFlags.AttachedOnly))  {     // Create the device. Just pick the first one.     JoystickDev = new Device( instance.InstanceGuid );     break;  } 
end example
 
Note

Notice that we are selecting the first joystick found. The alternatives would be to present a list of the joysticks found to the user for selection or to compare the capabilities of the joystick against supplied requirements to locate the best fit.

The cooperative level and the data format are specified just like they are for the mouse:

 JoystickDev.SetCooperativeLevel(winHandle, CooperativeLevelFlags.Exclusive   CooperativeLevelFlags.Foreground);  JoystickDev.SetDataFormat(DeviceDataFormat.Joystick); 

Two possible data formats are available for the joystick. The first format ( Joystick ) is the simpler of the two. In this case, simpler is definitely a relative term . This data structure includes 3 axes, rotation around 2 axes, 2 sliders, four point of view (POV) inputs, and 32 buttons ! The POV inputs are normally wired to a four-direction control on the top of a joystick. It can be used for changing camera direction or selecting weapons. The second format is geared toward force-feedback devices and adds velocities, accelerations, and forces for each of the axes. Needless to say, we are going to use only the first structure for our example.

The m_joystick variable is an instance of the JoystickState structure associated with the Joystick data format. You will also see that we are capturing the actual number of POV inputs out of the device capabilities structure for future reference.

From here on out, the joystick device works just like the other two. We acquire, poll, and unacquire the device the same way. We access the joystick state information using this line of code:

 m_joystick = JoystickDev.CurrentJoystickState; 

Unlike the mouse data, the joystick axis data is an absolute value rather than being relative to the last poll.




Introduction to 3D Game Engine Design Using DirectX 9 and C#
Introduction to 3D Game Engine Design Using DirectX 9 and C#
ISBN: 1590590813
EAN: 2147483647
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net