Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Input/XInput/XInputDevice.cpp
1172 views
1
2
using namespace RSDK;
3
4
bool32 RSDK::SKU::disabledXInputDevices[PLAYER_COUNT];
5
6
void RSDK::SKU::InputDeviceXInput::UpdateInput()
7
{
8
XINPUT_STATE *inputState = &this->inputState[this->activeState];
9
if (disabledXInputDevices[this->controllerID]) {
10
this->disabled = true;
11
}
12
else {
13
this->disabled = false;
14
if (!XInputGetState(this->controllerID, inputState)) {
15
this->activeState ^= 1;
16
17
int32 changedButtons = ~this->inputState[this->activeState].Gamepad.wButtons
18
& (this->inputState[this->activeState].Gamepad.wButtons ^ inputState->Gamepad.wButtons);
19
20
if (changedButtons)
21
this->inactiveTimer[0] = 0;
22
else
23
++this->inactiveTimer[0];
24
25
if ((changedButtons & KEYMASK_A) || (changedButtons & KEYMASK_START))
26
this->inactiveTimer[1] = 0;
27
else
28
++this->inactiveTimer[1];
29
30
this->anyPress = changedButtons || ((inputState->Gamepad.sThumbLX ^ this->inputState[this->activeState].Gamepad.sThumbLX) & 0xC000) != 0
31
|| ((inputState->Gamepad.sThumbLY ^ this->inputState[this->activeState].Gamepad.sThumbLY) & 0xC000) != 0
32
|| ((inputState->Gamepad.sThumbRX ^ this->inputState[this->activeState].Gamepad.sThumbRX) & 0xC000) != 0
33
|| ((inputState->Gamepad.sThumbRY ^ this->inputState[this->activeState].Gamepad.sThumbRY) & 0xC000) != 0;
34
35
XINPUT_GAMEPAD *gamePad = &this->inputState[this->activeState].Gamepad;
36
37
this->stateUp = (gamePad->wButtons & KEYMASK_UP) != 0;
38
this->stateDown = (gamePad->wButtons & KEYMASK_DOWN) != 0;
39
this->stateLeft = (gamePad->wButtons & KEYMASK_LEFT) != 0;
40
this->stateRight = (gamePad->wButtons & KEYMASK_RIGHT) != 0;
41
this->stateA = (gamePad->wButtons & KEYMASK_A) != 0;
42
this->stateB = (gamePad->wButtons & KEYMASK_B) != 0;
43
this->stateX = (gamePad->wButtons & KEYMASK_X) != 0;
44
this->stateY = (gamePad->wButtons & KEYMASK_Y) != 0;
45
this->stateStart = (gamePad->wButtons & KEYMASK_START) != 0;
46
this->stateSelect = (gamePad->wButtons & KEYMASK_SELECT) != 0;
47
this->stateBumper_L = (gamePad->wButtons & KEYMASK_BUMPERL) != 0;
48
this->stateBumper_R = (gamePad->wButtons & KEYMASK_BUMPERR) != 0;
49
this->stateStick_L = (gamePad->wButtons & KEYMASK_STICKL) != 0;
50
this->stateStick_R = (gamePad->wButtons & KEYMASK_STICKR) != 0;
51
52
this->hDelta_L = gamePad->sThumbLX;
53
this->vDelta_L = gamePad->sThumbLY;
54
55
float div = sqrtf((this->hDelta_L * this->hDelta_L) + (this->vDelta_L * this->vDelta_L));
56
this->hDelta_L = this->hDelta_L / div;
57
this->vDelta_L = this->vDelta_L / div;
58
if (div <= 7864.0) {
59
this->hDelta_L = 0.0;
60
this->vDelta_L = 0.0;
61
}
62
else {
63
this->hDelta_L = this->hDelta_L * ((fminf(32767.0, div) - 7864.0) / 24903.0);
64
this->vDelta_L = this->vDelta_L * ((fminf(32767.0, div) - 7864.0) / 24903.0);
65
}
66
67
this->hDelta_R = gamePad->sThumbRX;
68
this->vDelta_R = gamePad->sThumbRY;
69
70
div = sqrtf((this->hDelta_R * this->hDelta_R) + (this->vDelta_R * this->vDelta_R));
71
this->hDelta_R = this->hDelta_R / div;
72
this->vDelta_R = this->vDelta_R / div;
73
if (div <= 7864.0) {
74
this->hDelta_R = 0.0;
75
this->vDelta_R = 0.0;
76
}
77
else {
78
this->hDelta_R = this->hDelta_R * ((fminf(32767.0, div) - 7864.0) / 24903.0);
79
this->vDelta_R = this->vDelta_R * ((fminf(32767.0, div) - 7864.0) / 24903.0);
80
}
81
82
this->deltaBumper_L = this->stateBumper_L ? 1.0 : 0.0;
83
84
this->deltaTrigger_L = gamePad->bLeftTrigger;
85
if (this->deltaTrigger_L <= 30.0)
86
this->deltaTrigger_L = 0.0;
87
else
88
this->deltaTrigger_L = (this->deltaTrigger_L - 30.0) / 225.0;
89
90
this->deltaBumper_R = this->stateBumper_R ? 1.0 : 0.0;
91
92
this->deltaTrigger_R = gamePad->bRightTrigger;
93
if (this->deltaTrigger_R <= 30.0)
94
this->deltaTrigger_R = 0.0;
95
else
96
this->deltaTrigger_R = (this->deltaTrigger_R - 30.0) / 225.0;
97
}
98
99
this->ProcessInput(CONT_ANY);
100
}
101
}
102
103
void RSDK::SKU::InputDeviceXInput::ProcessInput(int32 controllerID)
104
{
105
controller[controllerID].keyUp.press |= this->stateUp;
106
controller[controllerID].keyDown.press |= this->stateDown;
107
controller[controllerID].keyLeft.press |= this->stateLeft;
108
controller[controllerID].keyRight.press |= this->stateRight;
109
controller[controllerID].keyA.press |= this->stateA;
110
controller[controllerID].keyB.press |= this->stateB;
111
controller[controllerID].keyX.press |= this->stateX;
112
controller[controllerID].keyY.press |= this->stateY;
113
controller[controllerID].keyStart.press |= this->stateStart;
114
controller[controllerID].keySelect.press |= this->stateSelect;
115
116
#if RETRO_REV02
117
stickL[controllerID].keyStick.press |= this->stateStick_L;
118
stickL[controllerID].hDelta = this->hDelta_L;
119
stickL[controllerID].vDelta = this->vDelta_L;
120
stickL[controllerID].keyUp.press |= this->vDelta_L > INPUT_DEADZONE;
121
stickL[controllerID].keyDown.press |= this->vDelta_L < -INPUT_DEADZONE;
122
stickL[controllerID].keyLeft.press |= this->hDelta_L < -INPUT_DEADZONE;
123
stickL[controllerID].keyRight.press |= this->hDelta_L > INPUT_DEADZONE;
124
125
stickR[controllerID].keyStick.press |= this->stateStick_R;
126
stickR[controllerID].hDelta = this->hDelta_R;
127
stickR[controllerID].vDelta = this->vDelta_R;
128
stickR[controllerID].keyUp.press |= this->vDelta_R > INPUT_DEADZONE;
129
stickR[controllerID].keyDown.press |= this->vDelta_R < -INPUT_DEADZONE;
130
stickR[controllerID].keyLeft.press |= this->hDelta_R < -INPUT_DEADZONE;
131
stickR[controllerID].keyRight.press |= this->hDelta_R > INPUT_DEADZONE;
132
133
triggerL[controllerID].keyBumper.press |= this->stateBumper_L;
134
triggerL[controllerID].bumperDelta = this->deltaBumper_L;
135
triggerL[controllerID].triggerDelta = this->deltaTrigger_L;
136
137
triggerR[controllerID].keyBumper.press |= this->stateBumper_R;
138
triggerR[controllerID].bumperDelta = this->deltaBumper_R;
139
triggerR[controllerID].triggerDelta = this->deltaTrigger_R;
140
#else
141
controller[controllerID].keyStickL.press |= this->stateStick_L;
142
stickL[controllerID].hDeltaL = this->hDelta_L;
143
stickL[controllerID].vDeltaL = this->vDelta_L;
144
145
stickL[controllerID].keyUp.press |= this->vDelta_L > INPUT_DEADZONE;
146
stickL[controllerID].keyDown.press |= this->vDelta_L < -INPUT_DEADZONE;
147
stickL[controllerID].keyLeft.press |= this->hDelta_L < -INPUT_DEADZONE;
148
stickL[controllerID].keyRight.press |= this->hDelta_L > INPUT_DEADZONE;
149
150
controller[controllerID].keyStickR.press |= this->stateStick_R;
151
stickL[controllerID].hDeltaL = this->hDelta_R;
152
stickL[controllerID].vDeltaL = this->vDelta_R;
153
154
controller[controllerID].keyBumperL.press |= this->stateBumper_L;
155
stickL[controllerID].deadzone = this->deltaBumper_L;
156
stickL[controllerID].triggerDeltaL = this->deltaTrigger_L;
157
158
controller[controllerID].keyBumperR.press |= this->stateBumper_R;
159
stickL[controllerID].triggerDeltaR = this->deltaTrigger_R;
160
#endif
161
}
162
163
RSDK::SKU::InputDeviceXInput *RSDK::SKU::InitXInputDevice(uint32 id)
164
{
165
if (inputDeviceCount == INPUTDEVICE_COUNT)
166
return NULL;
167
168
if (inputDeviceList[inputDeviceCount] && inputDeviceList[inputDeviceCount]->active)
169
return NULL;
170
171
if (inputDeviceList[inputDeviceCount])
172
delete inputDeviceList[inputDeviceCount];
173
174
inputDeviceList[inputDeviceCount] = new InputDeviceXInput();
175
176
InputDeviceXInput *device = (InputDeviceXInput *)inputDeviceList[inputDeviceCount];
177
178
for (int32 i = 0; i < PLAYER_COUNT; ++i) disabledXInputDevices[i] = false;
179
180
device->gamepadType = (DEVICE_API_XINPUT << 16) | (DEVICE_TYPE_CONTROLLER << 8) | (DEVICE_XBOX << 0);
181
device->disabled = false;
182
device->id = id;
183
device->active = true;
184
185
for (int32 i = 0; i < PLAYER_COUNT; ++i) {
186
if (inputSlots[i] == id) {
187
inputSlotDevices[i] = device;
188
device->isAssigned = true;
189
}
190
}
191
192
inputDeviceCount++;
193
return device;
194
}
195
196
void RSDK::SKU::InitXInputAPI()
197
{
198
char idString[16];
199
200
sprintf_s(idString, sizeof(idString), "%s", "XInputDevice0");
201
202
for (int32 i = 0; i < PLAYER_COUNT; ++i) {
203
idString[12] = '0' + i;
204
205
uint32 id;
206
GenerateHashCRC(&id, idString);
207
208
InputDeviceXInput *device = NULL;
209
for (int32 d = 0; d < inputDeviceCount; ++d) {
210
if (inputDeviceList[d] && inputDeviceList[d]->id == id) {
211
device = (InputDeviceXInput *)inputDeviceList[d];
212
break;
213
}
214
}
215
216
XINPUT_STATE pState;
217
int32 deviceState = XInputGetState(i, &pState);
218
if (deviceState != ERROR_SUCCESS) {
219
if (device)
220
RemoveInputDevice(device);
221
}
222
else if (!device) {
223
device = InitXInputDevice(id);
224
if (device)
225
device->controllerID = i;
226
}
227
}
228
}
229
230
void RSDK::SKU::UpdateXInputDevices()
231
{
232
for (int32 i = 0; i < PLAYER_COUNT; ++i) disabledXInputDevices[i] = false;
233
}
234