-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconfig.cpp
200 lines (175 loc) · 8.24 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#include <stdio.h>
#include <math.h>
struct Joystick
{
static const unsigned int inputCount = 64;
float currentInputs[inputCount];
float previousInputs[inputCount];
IDirectInputDevice8* device;
};
struct Input
{
static const unsigned int keyCount = 256;
unsigned int joystickCount;
Joystick* joysticks;
IDirectInput8* dinput;
bool currentKeyboard[keyCount];
bool previousKeyboard[keyCount];
};
enum InputActions { action_moveUp, action_moveDown, action_moveLeft, action_moveRight, action_shoot, action_jump, action_count };
const char* inputActionNames[] = {"move up", "move down", "move left", "move right", "shoot", "jump"};
struct InputBinding
{
bool joystickBound;
unsigned int joystickIndex;
unsigned int joystickInputIndex;
bool keyBound;
unsigned int keyIndex;
};
struct Vec2 { float x, y; };
BOOL CALLBACK DirectInputEnumDevicesCallback(LPCDIDEVICEINSTANCE instance, LPVOID userData)
{
Input* input = (Input*)userData;
IDirectInputDevice8* device;
input->dinput->CreateDevice(instance->guidInstance, &device, NULL);
device->SetCooperativeLevel(GetActiveWindow(), DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
device->SetDataFormat(&c_dfDIJoystick);
device->Acquire();
input->joystickCount += 1;
input->joysticks = (Joystick*)realloc(input->joysticks, input->joystickCount * sizeof(Joystick));
Joystick joystick = {0};
joystick.device = device;
input->joysticks[input->joystickCount - 1] = joystick;
return DIENUM_CONTINUE;
}
void updateInput(Input* input)
{
// Update joysticks
for (unsigned int joystickIndex=0; joystickIndex<input->joystickCount; ++joystickIndex)
{
DIJOYSTATE state;
if (input->joysticks[joystickIndex].device->GetDeviceState(sizeof(state), &state) == DI_OK)
{
memcpy(input->joysticks[joystickIndex].previousInputs, input->joysticks[joystickIndex].currentInputs, Joystick::inputCount * sizeof(float));
// Convert axes to [0, 1] range, with + and - directions mapped to separate inputs
input->joysticks[joystickIndex].currentInputs[0] = fmaxf( state.lX / (float)SHRT_MAX - 1, 0);
input->joysticks[joystickIndex].currentInputs[1] = fmaxf(-state.lX / (float)SHRT_MAX + 1, 0);
input->joysticks[joystickIndex].currentInputs[2] = fmaxf( state.lY / (float)SHRT_MAX - 1, 0);
input->joysticks[joystickIndex].currentInputs[3] = fmaxf(-state.lY / (float)SHRT_MAX + 1, 0);
input->joysticks[joystickIndex].currentInputs[4] = fmaxf( state.lZ / (float)SHRT_MAX - 1, 0);
input->joysticks[joystickIndex].currentInputs[5] = fmaxf(-state.lZ / (float)SHRT_MAX + 1, 0);
input->joysticks[joystickIndex].currentInputs[6] = fmaxf( state.lRx / (float)SHRT_MAX - 1, 0);
input->joysticks[joystickIndex].currentInputs[7] = fmaxf(-state.lRx / (float)SHRT_MAX + 1, 0);
input->joysticks[joystickIndex].currentInputs[8] = fmaxf( state.lRy / (float)SHRT_MAX - 1, 0);
input->joysticks[joystickIndex].currentInputs[9] = fmaxf(-state.lRy / (float)SHRT_MAX + 1, 0);
input->joysticks[joystickIndex].currentInputs[10] = fmaxf( state.lRz / (float)SHRT_MAX - 1, 0);
input->joysticks[joystickIndex].currentInputs[11] = fmaxf(-state.lRz / (float)SHRT_MAX + 1, 0);
input->joysticks[joystickIndex].currentInputs[12] = fmaxf( state.rglSlider[0] / (float)SHRT_MAX - 1, 0);
input->joysticks[joystickIndex].currentInputs[13] = fmaxf(-state.rglSlider[0] / (float)SHRT_MAX + 1, 0);
input->joysticks[joystickIndex].currentInputs[14] = fmaxf( state.rglSlider[1] / (float)SHRT_MAX - 1, 0);
input->joysticks[joystickIndex].currentInputs[15] = fmaxf(-state.rglSlider[1] / (float)SHRT_MAX + 1, 0);
// Convert each hat direction to be 0 or 1
DWORD hat = state.rgdwPOV[0];
input->joysticks[joystickIndex].currentInputs[16] = (hat==0 || hat==4500 || hat==31500)? 1.0f : 0.0f;
input->joysticks[joystickIndex].currentInputs[17] = (hat==4500 || hat==9000 || hat==13500)? 1.0f : 0.0f;
input->joysticks[joystickIndex].currentInputs[18] = (hat==13500 || hat==18000 || hat==22500)? 1.0f : 0.0f;
input->joysticks[joystickIndex].currentInputs[19] = (hat==22500 || hat==27000 || hat==31500)? 1.0f : 0.0f;
// Convert each button to be 1 when pressed, 0 when not pressed
for (unsigned int buttonIndex = 0; buttonIndex < 32; ++buttonIndex) {
input->joysticks[joystickIndex].currentInputs[20 + buttonIndex] = state.rgbButtons[buttonIndex]? 1.0f : 0.0f;
}
}
}
// Update Keyboard
memcpy(input->previousKeyboard, input->currentKeyboard, Input::keyCount * sizeof(bool));
for (unsigned int i=8 /*skip mouse buttons*/; i<Input::keyCount; ++i) {
input->currentKeyboard[i] = GetAsyncKeyState(i) & 0x8000 ? true : false;
}
}
bool bindInput(InputBinding* binding, Input input)
{
for (unsigned int i=0; i<Input::keyCount; ++i) {
if (input.currentKeyboard[i] && !input.previousKeyboard[i]) {
binding->keyIndex = i;
binding->keyBound = true;
return true;
}
}
for (unsigned int joystickIndex=0; joystickIndex<input.joystickCount; ++joystickIndex) {
for (unsigned int i=0; i<Joystick::inputCount; ++i) {
if (input.joysticks[joystickIndex].currentInputs[i] > 0.5f && input.joysticks[joystickIndex].previousInputs[i] < 0.5f) {
binding->joystickIndex = joystickIndex;
binding->joystickInputIndex = i;
binding->joystickBound = true;
return true;
}
}
}
return false;
}
bool isInputActive(InputBinding binding, Input input)
{
return (binding.keyBound && input.currentKeyboard[binding.keyIndex])
|| (binding.joystickBound && input.joysticks[binding.joystickIndex].currentInputs[binding.joystickInputIndex] > 0.5f);
}
Vec2 get2dAnalogInput(InputBinding up, InputBinding down, InputBinding left, InputBinding right, Input input)
{
// Ignore joystick input if any of the keys are pressed.
// Mixing the two can cause drifting from small values in analog sticks.
float upValue = up.keyBound && input.currentKeyboard[up.keyIndex] ? 1.0f : 0.0f;
float downValue = down.keyBound && input.currentKeyboard[down.keyIndex] ? 1.0f : 0.0f;
float leftValue = left.keyBound && input.currentKeyboard[left.keyIndex] ? 1.0f : 0.0f;
float rightValue = right.keyBound && input.currentKeyboard[right.keyIndex]? 1.0f : 0.0f;
if (upValue==0 && downValue==0 && leftValue==0 && rightValue==0)
{
if (up.joystickBound) upValue = input.joysticks[up.joystickIndex].currentInputs[up.joystickInputIndex];
if (down.joystickBound) downValue = input.joysticks[down.joystickIndex].currentInputs[down.joystickInputIndex];
if (left.joystickBound) leftValue = input.joysticks[left.joystickIndex].currentInputs[left.joystickInputIndex];
if (right.joystickBound) rightValue = input.joysticks[right.joystickIndex].currentInputs[right.joystickInputIndex];
}
Vec2 result = {rightValue-leftValue, upValue-downValue};
return result;
}
int main()
{
Input input = {0};
HINSTANCE hInstance = GetModuleHandle(0);
DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&input.dinput, 0);
input.dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, DirectInputEnumDevicesCallback, (void*)&input, DIEDFL_ALLDEVICES);
// Get current inputs so previous inputs will be accurate on first frame
updateInput(&input);
puts("Configure inputs for keyboard and joysticks. Press Esc to undo");
InputBinding bindings[action_count] = {0};
unsigned int editBindingIndex = 0;
while (1)
{
updateInput(&input);
if (editBindingIndex < action_count)
{
printf("\rEdit binding for %s ", inputActionNames[editBindingIndex]);
if (input.currentKeyboard[VK_ESCAPE]) {
if (!input.previousKeyboard[VK_ESCAPE] && editBindingIndex > 0) {
--editBindingIndex;
}
}
else if (bindInput(&bindings[editBindingIndex], input)) {
++editBindingIndex;
}
}
else
{
if (input.currentKeyboard[VK_ESCAPE]) {
editBindingIndex = 0;
}
printf("\rPlay game: ");
Vec2 movement = get2dAnalogInput(bindings[action_moveUp], bindings[action_moveDown], bindings[action_moveLeft], bindings[action_moveRight], input);
printf("movement: x% 1.2f y% 1.2f ", movement.x, movement.y);
if (isInputActive(bindings[action_shoot], input)) printf("BLAM ");
if (isInputActive(bindings[action_jump], input)) printf("boing!");
printf(" ");
}
Sleep(16);
}
}