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() {
  final pState = calloc<XINPUT_STATE>();
  try {
    final dwResult = XInputGetState(controller, pState);
    if (dwResult == WIN32_ERROR.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 = GamepadState(
        true,
        gamepad.wButtons,
        gamepad.bLeftTrigger,
        gamepad.bRightTrigger,
        gamepad.sThumbLX,
        gamepad.sThumbLY,
        gamepad.sThumbRX,
        gamepad.sThumbRY,
      );
    } else if (dwResult == WIN32_ERROR.ERROR_DEVICE_NOT_CONNECTED) {
      state = GamepadState.disconnected();
    } else {
      throw WindowsException(dwResult);
    }
  } finally {
    free(pState);
  }
}