void CGamePad::UpdateState() { #ifdef ION_CONFIG_WINDOWS if (pXInputGetState) { XINPUT_STATE XInputState; if (pXInputGetState(0, & XInputState)) return; LeftTrigger = GamepadTrigger(XInputState.Gamepad.bLeftTrigger); RightTrigger = GamepadTrigger(XInputState.Gamepad.bRightTrigger); LeftStick.X = GamepadStick(XInputState.Gamepad.sThumbLX); LeftStick.Y = GamepadStick(XInputState.Gamepad.sThumbLY); RightStick.X = GamepadStick(XInputState.Gamepad.sThumbRX); RightStick.Y = GamepadStick(XInputState.Gamepad.sThumbRY); ButtonPressed[(int) EGamePadButton::A] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_A) != 0; ButtonPressed[(int) EGamePadButton::B] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_B) != 0; ButtonPressed[(int) EGamePadButton::X] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_X) != 0; ButtonPressed[(int) EGamePadButton::Y] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_Y) != 0; ButtonPressed[(int) EGamePadButton::LeftShoulder] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) != 0; ButtonPressed[(int) EGamePadButton::RightShoulder] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) != 0; ButtonPressed[(int) EGamePadButton::DPadUp] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) != 0; ButtonPressed[(int) EGamePadButton::DPadDown] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) != 0; ButtonPressed[(int) EGamePadButton::DPadLeft] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) != 0; ButtonPressed[(int) EGamePadButton::DPadRight] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) != 0; ButtonPressed[(int) EGamePadButton::Start] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_START) != 0; ButtonPressed[(int) EGamePadButton::Back] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) != 0; ButtonPressed[(int) EGamePadButton::LeftThumb] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) != 0; ButtonPressed[(int) EGamePadButton::RightThumb] = (XInputState.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) != 0; } #endif }
bool GamepadManager::GetGamepadState(GamepadState* pState) { if (pXInputGetState) { if((NextTryTime == 0) || (GetTickCount() >= NextTryTime)) // If the device is known to be present or if it's time to try testing for it again... { XINPUT_STATE xis; DWORD dwResult = pXInputGetState(0, &xis); // This function is expensive, including if there is no connected device. if(dwResult == ERROR_SUCCESS) { if (xis.dwPacketNumber != LastPadPacketNo) { // State changed. pState->Buttons = xis.Gamepad.wButtons; // Currently matches Xinput pState->LT = GamepadTrigger(xis.Gamepad.bLeftTrigger); pState->RT = GamepadTrigger(xis.Gamepad.bRightTrigger); pState->LX = GamepadStick(xis.Gamepad.sThumbLX); pState->LY = GamepadStick(xis.Gamepad.sThumbLY); pState->RX = GamepadStick(xis.Gamepad.sThumbRX); pState->RY = GamepadStick(xis.Gamepad.sThumbRY); LastPadPacketNo = xis.dwPacketNumber; NextTryTime = 0; return true; } } else if(dwResult == ERROR_DEVICE_NOT_CONNECTED) { // Don't bother wasting time on XInputGetState if one isn't connected, as it's very slow when one isn't connected. // GetTickCount64 is available with Windows Vista+ and doesn't wrap around. // GetTickCount wraps around every 49.7 days since the system started, but we don't need absolute time and it's OK // if we have a false positive which would occur if NextTryTime is set to a value that has wrapped around to zero. NextTryTime = GetTickCount() + 5000; } } } return false; }