Game Development Reference
In-Depth Information
{
IntPtr _joystick;
public XboxController(int player)
{
_joystick = Sdl.SDL_JoystickOpen(player);
}
#region IDisposable Members
public void Dispose()
{
Sdl.SDL_JoystickClose(_joystick);
}
#endregion
}
}
This code creates the joystick using a player index. You might want to create a
game that supports two or more players; in this case, you'd need to create a
controller for each player. The joystick is also disposable so it will release its
reference to the controller once it's destroyed.
The controller can now be created in the test state.
XboxController _controller;
public InputTestState()
{
Sdl.SDL_InitSubSystem(Sdl.SDL_INIT_JOYSTICK);
if (Sdl.SDL_NumJoysticks() > 0)
{
// Start using the joystick code
_useJoystick = true;
_controller = new XboxController(0);
}
}
The first type of control we're going to wrap is the control stick. Control sticks
are great for moving the character and positioning the camera. Control sticks
aren't made perfectly. They will often report that they're being pushed even when
they are centered and the controller is resting on the desk. The solution to this
problem is to ignore all output from the control stick unless it's pushed over a
 
Search WWH ::




Custom Search