diff --git a/.gitignore b/.gitignore index f54d7c2..c3d79a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Debug Release .vs +.DS_Store diff --git a/OrbiterSteamController/XinputController.cpp b/OrbiterSteamController/XinputController.cpp index 9d46875..7774f88 100644 --- a/OrbiterSteamController/XinputController.cpp +++ b/OrbiterSteamController/XinputController.cpp @@ -1,15 +1,24 @@ #include "XinputController.h" - XinputController::XinputController() { init(); } - XinputController::~XinputController() { } +// Creates "normalized" versions of analog state for use with Orbiter +void XinputController::update() { + updateState(); + normState.LX = max(-1, (double)state.Gamepad.sThumbLX / 32767); // Returns from -1 to 1 + normState.LY = max(-1, (double)state.Gamepad.sThumbLY / 32767); // Returns from -1 to 1 + normState.RX = max(-1, (double)state.Gamepad.sThumbRX / 32767); // Returns from -1 to 1 + normState.RY = max(-1, (double)state.Gamepad.sThumbRY / 32767); // Returns from -1 to 1 + normState.RT = (double)state.Gamepad.bRightTrigger / 255; // Returns from 0 to 1 + normState.LT = (double)state.Gamepad.bLeftTrigger / 255; // Returns from 0 to 1 +} + void XinputController::init() { setControllerNumber(); updateState(); @@ -20,57 +29,70 @@ void XinputController::updateState() { XInputGetState(controllerNumber, &state); } + +// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls) int XinputController::getLX() { updateState(); return state.Gamepad.sThumbLX; } +// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls) int XinputController::getLY() { updateState(); return state.Gamepad.sThumbLY; } +// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls) int XinputController::getRX() { updateState(); return state.Gamepad.sThumbRX; } +// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls) int XinputController::getRY() { updateState(); return state.Gamepad.sThumbRY; } +// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls) int XinputController::getRT() { updateState(); return state.Gamepad.bRightTrigger; } +// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls) int XinputController::getLT() { updateState(); return state.Gamepad.bLeftTrigger; } +// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls) double XinputController::getNormLX() { return max(-1, (double)getLX() / 32767); // Returns from -1 to 1 } +// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls) double XinputController::getNormLY() { return max(-1, (double)getLY() / 32767); // Returns from -1 to 1 } +// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls) double XinputController::getNormRX() { return max(-1, (double)getRX() / 32767); // Returns from -1 to 1 } +// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls) double XinputController::getNormRY() { return max(-1, (double)getRY() / 32767); // Returns from -1 to 1 } +// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls) double XinputController::getNormRT() { //return max(-1, (double)((getRT() - 128) / 127)); // Returns from -1 to 1 return (double)getRT() / 255; // Returns from 0 to 1 } +// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls) double XinputController::getNormLT() { //return max(-1, (double)((getLT() - 128) / 127)); // Returns from -1 to 1 return (double)getLT() / 255; // Returns from 0 to 1 diff --git a/OrbiterSteamController/XinputController.h b/OrbiterSteamController/XinputController.h index 14274ad..a1e1f16 100644 --- a/OrbiterSteamController/XinputController.h +++ b/OrbiterSteamController/XinputController.h @@ -2,23 +2,19 @@ #include #include +struct NormState { + double LX; + double LY; + double RX; + double RY; + double RT; + double LT; +}; + class XinputController { int controllerNumber; - XINPUT_STATE state; - -public: - XinputController(); - ~XinputController(); - - double getNormLX(); - double getNormLY(); - double getNormRX(); - double getNormRY(); - double getNormRT(); - double getNormLT(); -private: void setControllerNumber(); void init(); void updateState(); @@ -28,5 +24,19 @@ class XinputController int getRY(); int getRT(); int getLT(); + +public: + XINPUT_STATE state; + struct NormState normState; + + XinputController(); + ~XinputController(); + void update(); + double getNormLX(); + double getNormLY(); + double getNormRX(); + double getNormRY(); + double getNormRT(); + double getNormLT(); }; diff --git a/OrbiterSteamController/main.cpp b/OrbiterSteamController/main.cpp index 303b2ec..db0aadd 100644 --- a/OrbiterSteamController/main.cpp +++ b/OrbiterSteamController/main.cpp @@ -14,14 +14,15 @@ DLLCLBK void InitModule(HINSTANCE hModule) { DLLCLBK void opcPreStep(double simt, double simdt, double mjd) { #ifdef DEBUG - sprintf(oapiDebugString(), "LX: %f LY: %f LT+RT: %f RX: %f RY: %f", controller.getNormLX(), controller.getNormLY(), controller.getNormRT() + (controller.getNormLT() * -1), controller.getNormRX(), controller.getNormRY()); + sprintf(oapiDebugString(), "LX: %f LY: %f LT+RT: %f RX: %f RY: %f", controller.normState.LX, controller.normState.LY, controller.normState.RT + (controller.normState.LT * -1), controller.normState.RX, controller.normState.RY); #endif VESSEL* vessel = oapiGetFocusInterface(); - vessel->SetControlSurfaceLevel(AIRCTRL_ELEVATOR, controller.getNormLY() * -1, false); - vessel->SetControlSurfaceLevel(AIRCTRL_AILERON, controller.getNormLX(), false); - vessel->SetControlSurfaceLevel(AIRCTRL_RUDDER, controller.getNormRT() + (controller.getNormLT() * -1), false); + controller.update(); + vessel->SetControlSurfaceLevel(AIRCTRL_ELEVATOR, controller.normState.LY * -1, false); + vessel->SetControlSurfaceLevel(AIRCTRL_AILERON, controller.normState.LX, false); + vessel->SetControlSurfaceLevel(AIRCTRL_RUDDER, controller.normState.RT + (controller.normState.LT * -1), false); // An experiment with controlling the view with the RS directly. Not really needed as you can bind either pad to a mouse with the right button held down - //oapiCameraSetCockpitDir((roundf(controller.getNormRX() * 1000) / 1000) * -1, roundf(controller.getNormRY() * 1000) / 1000, false); + //oapiCameraSetCockpitDir((roundf(controller.normState.RX * 1000) / 1000) * -1, roundf(controller.normState.RY * 1000) / 1000, false); }