Path: blob/master/RSDKv5/RSDK/Input/XInput/XInputDevice.cpp
1172 views
1using namespace RSDK;23bool32 RSDK::SKU::disabledXInputDevices[PLAYER_COUNT];45void RSDK::SKU::InputDeviceXInput::UpdateInput()6{7XINPUT_STATE *inputState = &this->inputState[this->activeState];8if (disabledXInputDevices[this->controllerID]) {9this->disabled = true;10}11else {12this->disabled = false;13if (!XInputGetState(this->controllerID, inputState)) {14this->activeState ^= 1;1516int32 changedButtons = ~this->inputState[this->activeState].Gamepad.wButtons17& (this->inputState[this->activeState].Gamepad.wButtons ^ inputState->Gamepad.wButtons);1819if (changedButtons)20this->inactiveTimer[0] = 0;21else22++this->inactiveTimer[0];2324if ((changedButtons & KEYMASK_A) || (changedButtons & KEYMASK_START))25this->inactiveTimer[1] = 0;26else27++this->inactiveTimer[1];2829this->anyPress = changedButtons || ((inputState->Gamepad.sThumbLX ^ this->inputState[this->activeState].Gamepad.sThumbLX) & 0xC000) != 030|| ((inputState->Gamepad.sThumbLY ^ this->inputState[this->activeState].Gamepad.sThumbLY) & 0xC000) != 031|| ((inputState->Gamepad.sThumbRX ^ this->inputState[this->activeState].Gamepad.sThumbRX) & 0xC000) != 032|| ((inputState->Gamepad.sThumbRY ^ this->inputState[this->activeState].Gamepad.sThumbRY) & 0xC000) != 0;3334XINPUT_GAMEPAD *gamePad = &this->inputState[this->activeState].Gamepad;3536this->stateUp = (gamePad->wButtons & KEYMASK_UP) != 0;37this->stateDown = (gamePad->wButtons & KEYMASK_DOWN) != 0;38this->stateLeft = (gamePad->wButtons & KEYMASK_LEFT) != 0;39this->stateRight = (gamePad->wButtons & KEYMASK_RIGHT) != 0;40this->stateA = (gamePad->wButtons & KEYMASK_A) != 0;41this->stateB = (gamePad->wButtons & KEYMASK_B) != 0;42this->stateX = (gamePad->wButtons & KEYMASK_X) != 0;43this->stateY = (gamePad->wButtons & KEYMASK_Y) != 0;44this->stateStart = (gamePad->wButtons & KEYMASK_START) != 0;45this->stateSelect = (gamePad->wButtons & KEYMASK_SELECT) != 0;46this->stateBumper_L = (gamePad->wButtons & KEYMASK_BUMPERL) != 0;47this->stateBumper_R = (gamePad->wButtons & KEYMASK_BUMPERR) != 0;48this->stateStick_L = (gamePad->wButtons & KEYMASK_STICKL) != 0;49this->stateStick_R = (gamePad->wButtons & KEYMASK_STICKR) != 0;5051this->hDelta_L = gamePad->sThumbLX;52this->vDelta_L = gamePad->sThumbLY;5354float div = sqrtf((this->hDelta_L * this->hDelta_L) + (this->vDelta_L * this->vDelta_L));55this->hDelta_L = this->hDelta_L / div;56this->vDelta_L = this->vDelta_L / div;57if (div <= 7864.0) {58this->hDelta_L = 0.0;59this->vDelta_L = 0.0;60}61else {62this->hDelta_L = this->hDelta_L * ((fminf(32767.0, div) - 7864.0) / 24903.0);63this->vDelta_L = this->vDelta_L * ((fminf(32767.0, div) - 7864.0) / 24903.0);64}6566this->hDelta_R = gamePad->sThumbRX;67this->vDelta_R = gamePad->sThumbRY;6869div = sqrtf((this->hDelta_R * this->hDelta_R) + (this->vDelta_R * this->vDelta_R));70this->hDelta_R = this->hDelta_R / div;71this->vDelta_R = this->vDelta_R / div;72if (div <= 7864.0) {73this->hDelta_R = 0.0;74this->vDelta_R = 0.0;75}76else {77this->hDelta_R = this->hDelta_R * ((fminf(32767.0, div) - 7864.0) / 24903.0);78this->vDelta_R = this->vDelta_R * ((fminf(32767.0, div) - 7864.0) / 24903.0);79}8081this->deltaBumper_L = this->stateBumper_L ? 1.0 : 0.0;8283this->deltaTrigger_L = gamePad->bLeftTrigger;84if (this->deltaTrigger_L <= 30.0)85this->deltaTrigger_L = 0.0;86else87this->deltaTrigger_L = (this->deltaTrigger_L - 30.0) / 225.0;8889this->deltaBumper_R = this->stateBumper_R ? 1.0 : 0.0;9091this->deltaTrigger_R = gamePad->bRightTrigger;92if (this->deltaTrigger_R <= 30.0)93this->deltaTrigger_R = 0.0;94else95this->deltaTrigger_R = (this->deltaTrigger_R - 30.0) / 225.0;96}9798this->ProcessInput(CONT_ANY);99}100}101102void RSDK::SKU::InputDeviceXInput::ProcessInput(int32 controllerID)103{104controller[controllerID].keyUp.press |= this->stateUp;105controller[controllerID].keyDown.press |= this->stateDown;106controller[controllerID].keyLeft.press |= this->stateLeft;107controller[controllerID].keyRight.press |= this->stateRight;108controller[controllerID].keyA.press |= this->stateA;109controller[controllerID].keyB.press |= this->stateB;110controller[controllerID].keyX.press |= this->stateX;111controller[controllerID].keyY.press |= this->stateY;112controller[controllerID].keyStart.press |= this->stateStart;113controller[controllerID].keySelect.press |= this->stateSelect;114115#if RETRO_REV02116stickL[controllerID].keyStick.press |= this->stateStick_L;117stickL[controllerID].hDelta = this->hDelta_L;118stickL[controllerID].vDelta = this->vDelta_L;119stickL[controllerID].keyUp.press |= this->vDelta_L > INPUT_DEADZONE;120stickL[controllerID].keyDown.press |= this->vDelta_L < -INPUT_DEADZONE;121stickL[controllerID].keyLeft.press |= this->hDelta_L < -INPUT_DEADZONE;122stickL[controllerID].keyRight.press |= this->hDelta_L > INPUT_DEADZONE;123124stickR[controllerID].keyStick.press |= this->stateStick_R;125stickR[controllerID].hDelta = this->hDelta_R;126stickR[controllerID].vDelta = this->vDelta_R;127stickR[controllerID].keyUp.press |= this->vDelta_R > INPUT_DEADZONE;128stickR[controllerID].keyDown.press |= this->vDelta_R < -INPUT_DEADZONE;129stickR[controllerID].keyLeft.press |= this->hDelta_R < -INPUT_DEADZONE;130stickR[controllerID].keyRight.press |= this->hDelta_R > INPUT_DEADZONE;131132triggerL[controllerID].keyBumper.press |= this->stateBumper_L;133triggerL[controllerID].bumperDelta = this->deltaBumper_L;134triggerL[controllerID].triggerDelta = this->deltaTrigger_L;135136triggerR[controllerID].keyBumper.press |= this->stateBumper_R;137triggerR[controllerID].bumperDelta = this->deltaBumper_R;138triggerR[controllerID].triggerDelta = this->deltaTrigger_R;139#else140controller[controllerID].keyStickL.press |= this->stateStick_L;141stickL[controllerID].hDeltaL = this->hDelta_L;142stickL[controllerID].vDeltaL = this->vDelta_L;143144stickL[controllerID].keyUp.press |= this->vDelta_L > INPUT_DEADZONE;145stickL[controllerID].keyDown.press |= this->vDelta_L < -INPUT_DEADZONE;146stickL[controllerID].keyLeft.press |= this->hDelta_L < -INPUT_DEADZONE;147stickL[controllerID].keyRight.press |= this->hDelta_L > INPUT_DEADZONE;148149controller[controllerID].keyStickR.press |= this->stateStick_R;150stickL[controllerID].hDeltaL = this->hDelta_R;151stickL[controllerID].vDeltaL = this->vDelta_R;152153controller[controllerID].keyBumperL.press |= this->stateBumper_L;154stickL[controllerID].deadzone = this->deltaBumper_L;155stickL[controllerID].triggerDeltaL = this->deltaTrigger_L;156157controller[controllerID].keyBumperR.press |= this->stateBumper_R;158stickL[controllerID].triggerDeltaR = this->deltaTrigger_R;159#endif160}161162RSDK::SKU::InputDeviceXInput *RSDK::SKU::InitXInputDevice(uint32 id)163{164if (inputDeviceCount == INPUTDEVICE_COUNT)165return NULL;166167if (inputDeviceList[inputDeviceCount] && inputDeviceList[inputDeviceCount]->active)168return NULL;169170if (inputDeviceList[inputDeviceCount])171delete inputDeviceList[inputDeviceCount];172173inputDeviceList[inputDeviceCount] = new InputDeviceXInput();174175InputDeviceXInput *device = (InputDeviceXInput *)inputDeviceList[inputDeviceCount];176177for (int32 i = 0; i < PLAYER_COUNT; ++i) disabledXInputDevices[i] = false;178179device->gamepadType = (DEVICE_API_XINPUT << 16) | (DEVICE_TYPE_CONTROLLER << 8) | (DEVICE_XBOX << 0);180device->disabled = false;181device->id = id;182device->active = true;183184for (int32 i = 0; i < PLAYER_COUNT; ++i) {185if (inputSlots[i] == id) {186inputSlotDevices[i] = device;187device->isAssigned = true;188}189}190191inputDeviceCount++;192return device;193}194195void RSDK::SKU::InitXInputAPI()196{197char idString[16];198199sprintf_s(idString, sizeof(idString), "%s", "XInputDevice0");200201for (int32 i = 0; i < PLAYER_COUNT; ++i) {202idString[12] = '0' + i;203204uint32 id;205GenerateHashCRC(&id, idString);206207InputDeviceXInput *device = NULL;208for (int32 d = 0; d < inputDeviceCount; ++d) {209if (inputDeviceList[d] && inputDeviceList[d]->id == id) {210device = (InputDeviceXInput *)inputDeviceList[d];211break;212}213}214215XINPUT_STATE pState;216int32 deviceState = XInputGetState(i, &pState);217if (deviceState != ERROR_SUCCESS) {218if (device)219RemoveInputDevice(device);220}221else if (!device) {222device = InitXInputDevice(id);223if (device)224device->controllerID = i;225}226}227}228229void RSDK::SKU::UpdateXInputDevices()230{231for (int32 i = 0; i < PLAYER_COUNT; ++i) disabledXInputDevices[i] = false;232}233234