Game Development Reference
In-Depth Information
Button/Con-
trol
Description
Values
Start
Located in the middle of the controller, often used for
opening menus
Pressed/re-
leased
First of all we need to enable the XInput API, otherwise, the data retrieved from the
controllers will not be meaningful. To do this, we need to add a call to the XInputEn-
able() method at the end of our Game::LoadContent() method. In this method
we will define if we are enabling or disabling XInput using the single Boolean para-
meter. Once this is done, we're ready to work with the controllers.
Unlike the Pointer and Keyboard APIs, XInput requires the application to poll the API
for new information. This means that for every update, we need to get the latest state
of the controllers.
We could add this directly to the Update() method; however, the code to properly
handle multiple controllers can get a little too long for that single method, so let's
break that out into a dedicated GamePad update method and call it from the start of
the Update() method.
The following is the complete UpdateGamePad() method. Don't worry about the
length, we'll break it down.
void Game::UpdateGamePad()
{
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)
{
Search WWH ::




Custom Search