Game Development Reference
In-Depth Information
want, and a pointer to an XINPUT_STATE structure that will contain the state of the
controller.
The player may not always have four controllers plugged in, though, so we want
to be able to support a controller on any index if it's plugged in. The XIn-
putGetState() method provides us with an error or success message depending
on whether the state could be acquired, and with that we can determine if we're
looking at a connected controller based on whether the result is equal to
ERROR_SUCCESS , as shown:
DWORD result;
XINPUT_STATE state;
for (DWORD i = 0; i < XUSER_MAX_COUNT; ++i)
{
ZeroMemory(&state, sizeof(XINPUT_STATE));
result = XInputGetState(i, &state);
if (result == ERROR_SUCCESS)
Buttons
One of the main components of a GamePad are the buttons. Using XInput these are
pretty simple to work with, and are shown in the code snippet that follows:
auto btnState = state.Gamepad.wButtons;
if (btnState & XINPUT_GAMEPAD_DPAD_UP)
{
_sidePressed = -1;
}
else if (btnState & XINPUT_GAMEPAD_DPAD_DOWN)
{
_sidePressed = 1;
}
The buttons are stored inside the Gamepad object, packed into a single word, mean-
ing that we need to make use of the AND bitwise operation to check if the button has
been pressed. We can make use of some predefined constants to extract the correct
buttons, as outlined in the following table:
Search WWH ::




Custom Search