updateState method

void updateState()

Get the current state of the current controller.

This function is designed to be called once for each pass on a game loop. It can safely be called even if the gamepad is disconnected, at which point neutral values will be returned for all buttons and thumbsticks.

Implementation

void updateState() => using((arena) {
  final pState = arena<XINPUT_STATE>();
  final dwResult = WIN32_ERROR(XInputGetState(controller, pState));
  if (dwResult == ERROR_SUCCESS) {
    final XINPUT_STATE(:dwPacketNumber, Gamepad: gamepad) = pState.ref;
    // The packet number indicates whether there have been any changes in
    // the state of the controller. If the dwPacketNumber member is the same
    // in sequentially returned XINPUT_STATE structures, the controller
    // state has not changed.
    if (dwPacketNumber == _packetNumber) return;

    _packetNumber = dwPacketNumber;
    state = .new(
      true,
      gamepad.wButtons,
      gamepad.bLeftTrigger,
      gamepad.bRightTrigger,
      gamepad.sThumbLX,
      gamepad.sThumbLY,
      gamepad.sThumbRX,
      gamepad.sThumbRY,
    );
  } else if (dwResult == ERROR_DEVICE_NOT_CONNECTED) {
    state = .disconnected();
  } else {
    throw WindowsException(dwResult.toHRESULT());
  }
});