Game Development Reference
In-Depth Information
Using joysticks with XInput
Once again, we first need to add some member variables for our XInput device. They
look a bit different this time, but here they are:
Controller m_Controller1;
Gamepad m_Controller1StateCurrent;
Gamepad m_Controller1StateLast;
In XInput, we use the Controller class to represent a controller. The Gamepad
structure stores the state of the controller. As before, we have one variable to hold our
device, and two more to hold its current and previous state.
Now, we will add a very short new method named InitXInput() . Here is its code:
private void InitXInput()
{
m_Controller1 = new Controller(UserIndex.One);
}
This code sets up one XInput controller for us to use. We pass into its constructor the
value UserIndex.One to indicate that this controller will be used by player 1.
We need to modify the constructor of our user input class to call this new method
now. We also need to add some code to initialize our XInput joystick state variables.
As mentioned earlier, this is necessary to prevent the program from crashing. Here is
what the constructor looks like now with the new bits of code highlighted:
public UserInput()
{
InitDirectInput();
InitXInput();
m_KeyboardStateCurrent = new KeyboardState();
m_KeyboardStateLast = new KeyboardState();
Search WWH ::




Custom Search