From 68d41a07ba2caae04db766d189030242ea079fee Mon Sep 17 00:00:00 2001 From: hiker Date: Sat, 5 Aug 2017 00:34:51 +1000 Subject: [PATCH] Now actually use XInput to read the joystick events for all XInput devices. --- .../source/Irrlicht/CIrrDeviceWin32.cpp | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/irrlicht/source/Irrlicht/CIrrDeviceWin32.cpp b/lib/irrlicht/source/Irrlicht/CIrrDeviceWin32.cpp index 73ff7ca55..6a34a3cec 100644 --- a/lib/irrlicht/source/Irrlicht/CIrrDeviceWin32.cpp +++ b/lib/irrlicht/source/Irrlicht/CIrrDeviceWin32.cpp @@ -20,6 +20,7 @@ #include #include #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_) +#include #ifdef _IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_ #define DIRECTINPUT_VERSION 0x0800 #include @@ -34,7 +35,6 @@ #ifdef _MSC_VER #pragma comment(lib, "winmm.lib") #endif -#include #endif #endif @@ -345,6 +345,31 @@ void pollJoysticks() for(joystick = 0; joystick < ActiveJoysticks.size(); ++joystick) { + SEvent event; + + event.EventType = irr::EET_JOYSTICK_INPUT_EVENT; + event.JoystickEvent.Joystick = (u8)joystick; + + if (ActiveJoysticks[joystick].m_use_xinput) + { + XINPUT_STATE state; + memset(&state, 0, sizeof(state)); + DWORD result = XInputGetState(ActiveJoysticks[joystick].Index, &state); + event.JoystickEvent.ButtonStates = state.Gamepad.wButtons; + // Map the axis as they were previously, so existing configs + // still work as expected. The Y axis needs to be reversed: + // -32768 --> 32767, ..., 32767 --> -32768 + // Inverting the bits with ~ does that (-sThumbLY would map -32768 to -32768!!) + event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_X] = state.Gamepad.sThumbLX; + event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Y] = ~state.Gamepad.sThumbLY; + event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Z] = state.Gamepad.bLeftTrigger * 128; + event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_R] = state.Gamepad.sThumbRX; + event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_U] = ~state.Gamepad.sThumbRY; + event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_V] = state.Gamepad.bRightTrigger * 128; + (void)Device->postEventFromUser(event); + + continue; + } // needs to be reset for each joystick // request ALL values and POV as continuous if possible @@ -353,11 +378,6 @@ void pollJoysticks() if (!FAILED(ActiveJoysticks[joystick].lpdijoy->GetDeviceState(sizeof(info),&info))) { - SEvent event; - - event.EventType = irr::EET_JOYSTICK_INPUT_EVENT; - event.JoystickEvent.Joystick = (u8)joystick; - event.JoystickEvent.POV = (u16)info.rgdwPOV[0]; // set to undefined if no POV value was returned or the value // is out of range