-
Notifications
You must be signed in to change notification settings - Fork 17
/
gameinput.cpp
69 lines (64 loc) · 2.1 KB
/
gameinput.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
#include "GameInput.h"
#include <stdio.h>
struct Joysticks
{
uint32_t deviceCount;
IGameInputDevice** devices;
};
void CALLBACK deviceChangeCallback(GameInputCallbackToken callbackToken, void* context, IGameInputDevice* device, uint64_t timestamp, GameInputDeviceStatus currentStatus, GameInputDeviceStatus previousStatus)
{
Joysticks* joysticks = (Joysticks*)context;
if (currentStatus & GameInputDeviceConnected)
{
for (uint32_t i = 0; i < joysticks->deviceCount; ++i)
{
if (joysticks->devices[i] == device) return;
}
++joysticks->deviceCount;
joysticks->devices = (IGameInputDevice**)realloc(joysticks->devices, joysticks->deviceCount * sizeof(IGameInputDevice*));
joysticks->devices[joysticks->deviceCount-1] = device;
}
}
int main()
{
Joysticks joysticks = {0};
IGameInput* input;
GameInputCreate(&input);
IGameInputDispatcher* dispatcher;
input->CreateDispatcher(&dispatcher);
GameInputCallbackToken callbackId;
input->RegisterDeviceCallback(0, GameInputKindController, GameInputDeviceAnyStatus, GameInputBlockingEnumeration, &joysticks, deviceChangeCallback, &callbackId);
bool buttons[64];
GameInputSwitchPosition switches[64];
float axes[64];
while (1)
{
dispatcher->Dispatch(0);
for (uint32_t i = 0; i < joysticks.deviceCount; ++i)
{
IGameInputReading* reading;
if (SUCCEEDED(input->GetCurrentReading(GameInputKindController, joysticks.devices[i], &reading)))
{
printf("Joystick %d, ", i);
reading->GetControllerAxisState(ARRAYSIZE(axes), axes);
reading->GetControllerSwitchState(ARRAYSIZE(switches), switches);
reading->GetControllerButtonState(ARRAYSIZE(buttons), buttons);
printf("Axes - ");
for (uint32_t i = 0; i < reading->GetControllerAxisCount(); ++i) {
printf("%d:%f ", i, axes[i]);
}
printf("Switches - ");
for (uint32_t i = 0; i < reading->GetControllerSwitchCount(); ++i) {
printf("%d:%d ", i, switches[i]);
}
printf("Buttons - ");
for (uint32_t i = 0; i < reading->GetControllerButtonCount(); ++i) {
if (buttons[i]) printf("%d ", i);
}
puts("");
reading->Release();
}
}
Sleep(16);
}
}