We can’t exactly change those XInput values in the API, but we can change ours! Here is a potential solution for those experiencing button mapping problems in this tutorial:įor example, XINPUT_GAMEPAD_A (the hexadecimal value) may be mapped to the ‘X’ button on your gamepad instead of the physical ‘A’ button as the name suggests. As mentioned in Part 1, other controllers may differ in their button mapping. Note: These values are effectively our button mapping and I have successfully tested it on a (wired USB) Microsoft Xbox360 controller. accessing the matching XINPUT button value These values are used to index the XINPUT_Buttons array, This can easily be done in the constructor for the ‘ XButtonIDs‘ struct, let’s add this above the constructors of the ‘ Gamepad‘ class in ‘ Gamepad.cpp‘. One last thing before we cover the ‘ ButtonPressed‘ function, we need to set all the numbers in the ‘ XButtonIDs‘ (or ‘ XButtons‘, as we defined it) struct so that they can actually be used to index the ‘ XINPUT_Buttons‘ array. Define the 'XButtonIDs' struct as 'XButtons'
To finish defining the ‘ XButtonIDs‘ struct as ‘ XButtons‘, simply add the following line above the constructors in ‘ Gamepad.cpp‘. Externally define the 'XButtonIDs' struct as 'XButtons' Int m_iGamepadIndex // Gamepad index (eg. XINPUT_STATE m_State // Current gamepad state Void Rumble(float a_fLeftMotor = 0.0f, float a_fRightMotor = 0.0f)
Vibrate the gamepad (0.0f cancel, 1.0f max speed)
XINPUT_STATE GetState() // Return gamepad stateīool Connected() // Return true if gamepad is connected - 'Pressed' - Return true if pressed, false if not - Return true if stick is inside deadzone, false if outsideįloat LeftStick_X() // Return X axis of left stickįloat LeftStick_Y() // Return Y axis of left stickįloat RightStick_X() // Return X axis of right stickįloat RightStick_Y() // Return Y axis of right stickįloat LeftTrigger() // Return value of left triggerįloat RightTrigger() // Return value of right trigger ‘ Gamepad.h‘ should now look similar to this: Let’s externally define the ‘ XButtonIDs‘ struct as ‘ XButtons‘ at the bottom of ‘ Gamepad.h‘ – to make it easily accessible for specifying buttons in button-based functions, and while we’re at it, we’ll add the function prototype for the ‘Get ButtonPressed‘ function. The ‘ XINPUT_Buttons‘ array contains the XInput values (which are really just hexadecimal values) for all the buttons we’re supporting and the order of the values in the array matches the order of the values in the ‘ XButtonIDs‘ struct (you’ll soon see why). Int DPad_Up, DPad_Down, DPad_Left, DPad_Right Before we can get to that, we need to add an array of all the button values we want to use (fortunately XInput provides values for us) and a set of identifiers we can use to index the array.Īdd the following above the ‘ Gamepad‘ class in ‘ Gamepad.h‘: We’ll start with a basic check of ‘Get ButtonPressed‘, a boolean function which returns true if the specified button is pressed or false, if the button is not pressed. XInput does not seem to offer support for the ‘Guide’ (Xbox logo) button, but we’re going to make use of all the others – the A,B,X,Y ‘action’ buttons, the D-Pad, the BACK and START buttons and even the thumbstick buttons (clicking in/down the thumbsticks). The last (but definitely not least) feature we need to add to the ‘ Gamepad‘ class to make it complete, is the ability to obtain input from the various buttons. Return to Part 1 Return to Part 2 Basic Button Input: Before we begin, if you’re reading this and haven’t yet read one or both previous parts of the tutorial, it may be worth your while to go read the part(s) you missed. Welcome to the third and final part of my tutorial on gamepad input using C++ and XInput.