Add Trigger & Joystick Reading.
This commit is contained in:
14
README.md
14
README.md
@@ -1,4 +1,4 @@
|
||||
## Josh Stick API
|
||||
# Josh Stick API
|
||||
|
||||
A single-header, single-namespace, single API for reading controller and joystick device input on Linux and Windows.
|
||||
|
||||
@@ -6,8 +6,14 @@ A single-header, single-namespace, single API for reading controller and joystic
|
||||
* Windows Support via XInput API
|
||||
* Linux Support via jsdev API
|
||||
|
||||
|
||||
## TODO
|
||||
## TODO List
|
||||
|
||||
* Support multiple devices at once.
|
||||
* Note that XInput only supports a maximum of 4 devices.
|
||||
* Note that XInput only supports a maximum of 4 devices.
|
||||
|
||||
|
||||
## Contributing
|
||||
|
||||
## Licensing
|
||||
|
||||
## Other Notes
|
||||
|
14
main.cpp
14
main.cpp
@@ -5,9 +5,7 @@
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int main() {
|
||||
bool success = jstick::Initialize();
|
||||
|
||||
if (!success) {
|
||||
@@ -15,7 +13,6 @@ int main()
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if (!jstick::JoystickDetected()) {
|
||||
std::cerr << "No devices detected!" << std::endl;
|
||||
return -1;
|
||||
@@ -24,6 +21,14 @@ int main()
|
||||
while (jstick::JoystickDetected()) {
|
||||
jstick::ReadEventLoop();
|
||||
|
||||
|
||||
// Vibrate the joystick device by how much the thumbsticks are moved.
|
||||
Vector2 axisL = jstick::GetLeftThumbstickAxis();
|
||||
Vector2 axisR = jstick::GetRightThumbstickAxis();
|
||||
|
||||
jstick::Vibrate(axisL.Magnitude(), axisR.Magnitude());
|
||||
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::Back))
|
||||
std::cout << "Back Button Pressed" << std::endl;
|
||||
|
||||
@@ -69,6 +74,7 @@ int main()
|
||||
std::cout << "DPad Right Pressed" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user