-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserinput.cpp
187 lines (152 loc) · 3.65 KB
/
userinput.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
#define STRICT
#include "UserInput.h"
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput.lib")
UserInput::UserInput()
{
good = false;
pDI = 0;
mouse = 0;
keyboard = 0;
joystick = 0;
if (DirectInputCreate(GetModuleHandle(0), DIRECTINPUT_VERSION, &pDI, 0) != DI_OK)
return;
good = true;
}
UserInput::~UserInput()
{
if (pDI)
{
if (mouse)
{
mouse->Unacquire();
mouse->Release();
}
if (keyboard)
{
keyboard->Unacquire();
keyboard->Release();
}
if (joystick)
{
joystick->Unacquire();
joystick->Release();
}
pDI->Release();
}
}
bool UserInput::isOK()
{
return good;
}
void UserInput::release()
{
delete this;
}
bool UserInput::initMouse(const HWND hwnd)
{
good = false;
if (pDI->CreateDevice(GUID_SysMouse, &mouse, 0) != DI_OK)
return false;
if (mouse->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) != DI_OK)
return false;
if (mouse->SetDataFormat(&c_dfDIMouse) != DI_OK)
return false;
HRESULT hr = mouse->Acquire();
if (hr != DI_OK && hr != S_FALSE)
return false;
good = true;
return true;
}
bool UserInput::updateMouse()
{
good = false;
if (mouse->GetDeviceState(sizeof DIMOUSESTATE, (LPVOID)&mouseState) != DI_OK)
return false;
good = true;
return true;
}
bool UserInput::initKeyboard(const HWND hwnd)
{
good = false;
if (pDI->CreateDevice(GUID_SysKeyboard, &keyboard, 0) != DI_OK)
return false;
if (keyboard->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) != DI_OK)
return false;
if (keyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK)
return false;
HRESULT hr = keyboard->Acquire();
if (hr != DI_OK && hr != S_FALSE)
return false;
good = true;
return true;
}
bool UserInput::updateKeyboard()
{
good = false;
if (keyboard->GetDeviceState(256, (void *)&keyState) != DI_OK)
return false;
good = true;
return true;
}
struct JOYDATA
{
IDirectInput ** pDI;
IDirectInputDevice2 ** joystick;
};
static BOOL CALLBACK EnumJoysticksCallback(LPCDIDEVICEINSTANCE pInst, LPVOID lpvContext)
{
IDirectInputDevice * pDID = 0;
IDirectInput ** pDI = ((JOYDATA *)lpvContext)->pDI;
IDirectInputDevice2 ** joystick = ((JOYDATA *)lpvContext)->joystick;
if ((*pDI)->CreateDevice(pInst->guidInstance, &pDID, 0) != DI_OK)
return DIENUM_CONTINUE;
if (pDID->QueryInterface(IID_IDirectInputDevice2, (void **)joystick) != S_OK)
{
pDID->Release();
return DIENUM_CONTINUE;
}
pDID->Release();
return DIENUM_STOP;
}
bool UserInput::initJoystick(const HWND hwnd, const int minRange, const int maxRange)
{
good = false;
JOYDATA jd = {&pDI, &joystick};
if (pDI->EnumDevices(DIDEVTYPE_JOYSTICK, EnumJoysticksCallback, (void *)&jd, DIEDFL_ATTACHEDONLY) != DI_OK)
return false;
if (!joystick)
return false;
if (joystick->SetDataFormat(&c_dfDIJoystick) != DI_OK)
return false;
if (joystick->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) != DI_OK)
return false;
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof DIPROPRANGE;
diprg.diph.dwHeaderSize = sizeof DIPROPHEADER;
diprg.diph.dwHow = DIPH_BYOFFSET;
diprg.lMin = minRange;
diprg.lMax = maxRange;
diprg.diph.dwObj = DIJOFS_X;
HRESULT hr = joystick->SetProperty(DIPROP_RANGE, &diprg.diph);
if (hr != DI_OK && hr != DI_PROPNOEFFECT)
return false;
diprg.diph.dwObj = DIJOFS_Y;
hr = joystick->SetProperty(DIPROP_RANGE, &diprg.diph);
if (hr != DI_OK && hr != DI_PROPNOEFFECT)
return false;
if (joystick->Acquire() != DI_OK)
return false;
good = true;
return true;
}
bool UserInput::updateJoystick()
{
good = false;
if (joystick->Poll() != DI_OK)
return false;
if (joystick->GetDeviceState(sizeof DIJOYSTATE, (void *)&joyState) != DI_OK)
return false;
good = true;
return true;
}