Add Trigger & Joystick Reading.

This commit is contained in:
2025-05-23 14:44:03 -05:00
parent e07ac37699
commit 6f993eab34
3 changed files with 87 additions and 14 deletions

View File

@@ -5,17 +5,22 @@
#pragma comment(lib, "XInput.lib")
// TODO: Track device state of N devices
// TODO: Keep prev_state, so we can fire Events when buttons are pressed/releaed, etc.
#define MAX_DEVICES 4
bool device_exists[MAX_DEVICES];
XINPUT_STATE cur_state[MAX_DEVICES];
XINPUT_STATE prev_state[MAX_DEVICES];
bool jstick::Initialize()
{
// TODO: Anything.
return true;
}
bool jstick::Initialize() { return true; }
bool jstick::Cleanup() { return true; }
void jstick::JoystickServiceUpdate() { }
/// XInput treats the DPad as buttons.
bool jstick::GetDPadIsAxisOrButtons() { return false; }
@@ -25,7 +30,6 @@ void jstick::ReadEventLoop()
prev_state[0] = cur_state[0];
DWORD dwUserIndex = (DWORD)0;
XINPUT_STATE state;
@@ -69,6 +73,63 @@ bool jstick::JoystickDetected(int hwid)
}
}
Vector2 jstick::GetLeftThumbstickAxis(float deadzone, int hwid)
{
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
DWORD result = XInputGetState(hwid, &state);
if (result != ERROR_SUCCESS) {
return {0, 0};
}
return Vector2(state.Gamepad.sThumbLX, state.Gamepad.sThumbLY);
}
short jstick::GetLeftTrigger(int hwid)
{
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
DWORD result = XInputGetState(hwid, &state);
if (result != ERROR_SUCCESS) {
return 0;
}
return state.Gamepad.bLeftTrigger;
}
short jstick::GetRightTrigger(int hwid)
{
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
DWORD result = XInputGetState(hwid, &state);
if (result != ERROR_SUCCESS) {
return 0;
}
return state.Gamepad.bRightTrigger;
}
Vector2 jstick::GetRightThumbstickAxis(float deadzone, int hwid)
{
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
DWORD result = XInputGetState(hwid, &state);
if (result != ERROR_SUCCESS) {
return {0, 0};
}
return Vector2(state.Gamepad.sThumbRX, state.Gamepad.sThumbRY);
}
bool jstick::IsButtonDown(const XBoxButton& btn, int hwid)
{
XINPUT_STATE state;