Building game with Xbox Controller Support

xbox-controller

Xbox One Wireless Controller can work with your PC if you download one of the following driver packages:

Note To use your Xbox One controller with your console after using it with a PC, you must re-sync the controller to the console. You can do this by using the wireless sync button or a USB cable. For details, see Connect a wireless Xbox One controller to your console.

If you want to use an Xbox 360 Controller with your computer, you can use an Xbox 360 Controller for Windows. Or, you can use an Xbox 360 Wireless Controller together with an Xbox 360 Wireless Gaming Receiver for Windows.

Connect a wireless Xbox One controller to your console
How to assign a profile to an Xbox One Wireless Controller

Getting to Grips with the API 

The main method of work with a gamepad in Windows is to use the C ++ API XInput . https://msdn.microsoft.com/en-us/library/windows/desktop/ee417001(v=vs.85).aspx  Noteworthy is the absence of initialization functions, you simply query the state of the controller:

    1: XINPUT_STATE state; 
    2:  
    3: DWORD result=XInputGetState(0, &state); 
    4:  
    5:  
    6: if (result == ERROR_SUCCESS) 
    7:  
    8: { 
    9:  
   10:   if (state.Gamepad.wButtons & XINPUT_GAMEPAD_A) 
   11:  
   12:   { 
   13:  
   14:    
   15:  
   16:   } 
   17:  
   18: } 

XInputGetState function takes as parameters the index controller (they can be connected to more than one) as well as with the state of the structure where the return value of buttons:

 

    1: { 
    2:  
    3:     DWORD                               dwPacketNumber;  
    4:  
    5:     XINPUT_GAMEPAD                      Gamepad; 
    6:  
    7: } XINPUT_STATE, *PXINPUT_STATE; 
    8:  
    9:  
   10: typedef struct _XINPUT_GAMEPAD 
   11:  
   12: { 
   13:  
   14:     WORD                                wButtons;  
   15:  
   16:     BYTE                                bLeftTrigger;  
   17:  
   18:     BYTE                                bRightTrigger; 
   19:  
   20:     SHORT                               sThumbLX; 
   21:  
   22:     SHORT                               sThumbLY; 
   23:  
   24:     SHORT                               sThumbRX; 
   25:  
   26:     SHORT                               sThumbRY; 
   27:  
   28: } XINPUT_GAMEPAD, *PXINPUT_GAMEPAD; 

Buttons coded bits:

    1: #define XINPUT_GAMEPAD_DPAD_UP          0x0001 
    2: #define XINPUT_GAMEPAD_DPAD_DOWN        0x0002 
    3: #define XINPUT_GAMEPAD_DPAD_LEFT        0x0004 
    4: #define XINPUT_GAMEPAD_DPAD_RIGHT       0x0008 
    5: #define XINPUT_GAMEPAD_START            0x0010 
    6: #define XINPUT_GAMEPAD_BACK             0x0020 
    7: #define XINPUT_GAMEPAD_LEFT_THUMB       0x0040 
    8: #define XINPUT_GAMEPAD_RIGHT_THUMB      0x0080 
    9: #define XINPUT_GAMEPAD_LEFT_SHOULDER    0x0100 
   10: #define XINPUT_GAMEPAD_RIGHT_SHOULDER   0x0200 
   11: #define XINPUT_GAMEPAD_A                0x1000 
   12: #define XINPUT_GAMEPAD_B                0x2000 
   13: #define XINPUT_GAMEPAD_X                0x4000 
   14: #define XINPUT_GAMEPAD_Y                0x8000 

Slightly more complicated is the values ​​of the hammers and two joysticks. The values ​​of X, Y, are within SHRT_MIN-SHRT_MAX (-32768 +32767), and for the hammers _UI8_MAX (255). Usually in games, these values ​​are normalized to -1.0 +1.0. Also for joystick should consider the so-called dead zone. Return values ​​axes at the neutral position can vary from zero, and to disregard them use the default values ​​of "dead zones", which should be computed by the following algorithm:

    1: float magnitude = sqrt(state.Gamepad.sThumbRX*state.Gamepad.sThumbRX 
    2:  
    3: + state.Gamepad.sThumbRY*state.Gamepad.sThumbRY); 
    4:  
    5:  
    6: if (magnitude > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) 
    7:  
    8: { 
    9:  
   10: } 

Typical values ​​of these thresholds are as follows:

    1: #define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE  7849 
    2: #define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689 
    3: #define XINPUT_GAMEPAD_TRIGGER_THRESHOLD    30 

You can look at more detailed examples of work with a gamepad online https://code.msdn.com as well as take advantage of the wrapper which is part of DirectX Toolkit .

 

XInput game controller sample

This sample demonstrates the use of the XInput APIs in a C++ app.

Download C++ (61.0 KB)

Resources

https://msdn.microsoft.com/en-us/library/bb648761.aspx XInput Controller Sample

In addition to the functions directly related to the status polling controller to XInput also includes management features vibrating motor and is connected accessory, such as a voice recorder with a headset or audio to the headset. Support also has a joystick for managed code in the library and XNA Monogame .

headset

Get in touch?

So if your building a game or have built a game for Windows 8 with Controller support please get in touch as I would love to share your experience!