-
Notifications
You must be signed in to change notification settings - Fork 1
/
DIInputHandler.cpp
171 lines (149 loc) · 3.6 KB
/
DIInputHandler.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
#include "DIInputHandler.h"
DIInputHandler::DIInputHandler(HINSTANCE* _hInstance, HWND* _hWnd)
{
hInstance = _hInstance;
hWnd = _hWnd;
// create the DirectInput interface
DirectInput8Create(*hInstance, // the handle to the application
DIRECTINPUT_VERSION, // the compatible version
IID_IDirectInput8, // the DirectInput interface version
(void**)&din, // the pointer to the interface
NULL); // COM stuff, so we'll set it to NULL
// create the keyboard device
din->CreateDevice(GUID_SysKeyboard, // the default keyboard ID being used
&dinkeyboard, // the pointer to the device interface
NULL); // COM stuff, so we'll set it to NULL
din->CreateDevice(GUID_SysMouse,
&dinmouse,
NULL);
// set the data format to keyboard format
dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
dinmouse->SetDataFormat(&c_dfDIMouse);
// set the control we will have over the keyboard
dinkeyboard->SetCooperativeLevel(*hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
dinmouse->SetCooperativeLevel(*hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
reset();
// Debug Vars
mouseSpeedX = 1.0f;
for ( int i=0; i<NUM_MOUSE_KEYS; i++)
{
m_mouseKeys[i] = KEY_UP;
}
}
DIInputHandler::~DIInputHandler()
{
dinkeyboard->Unacquire(); // make sure the keyboard is unacquired
dinmouse->Unacquire(); // make sure the mouse in unacquired
din->Release(); // close DirectInput before exiting
}
void DIInputHandler::reset()
{
keys[A] = 0;
keys[S] = 0;
keys[D] = 0;
keys[W] = 0;
keys[SPACE] = 0;
keys[LCTRL] = 0;
keys[F1] = 0;
keys[F2] = 0;
keys[F3] = 0;
keys[F4] = 0;
mouse[X] = 0;
mouse[Y] = 0;
}
void DIInputHandler::detectInput(void)
{
// get access if we don't have it already
dinkeyboard->Acquire();
dinmouse->Acquire();
// get the input data
dinkeyboard->GetDeviceState(256, (LPVOID)keystate);
dinmouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mousestate);
}
void DIInputHandler::update()
{
reset();
detectInput();
if(keystate[DIK_W] & 0x80)
keys[W] = true;
if(keystate[DIK_A] & 0x80)
keys[A] = true;
if(keystate[DIK_S] & 0x80)
keys[S] = true;
if(keystate[DIK_D] & 0x80)
keys[D] = true;
if(keystate[DIK_SPACE] & 0x80)
keys[SPACE] = true;
if(keystate[DIK_LSHIFT] & 0x80) //DEBUG: should be LCONTROL
keys[LCTRL] = true;
if(keystate[DIK_F1] & 0x80)
keys[F1] = true;
if(keystate[DIK_F2] & 0x80)
keys[F2] = true;
if(keystate[DIK_F3] & 0x80)
keys[F3] = true;
if(keystate[DIK_F4] & 0x80)
keys[F4] = true;
if(keystate[DIK_ESCAPE] & 0x80)
PostMessage(*hWnd, WM_DESTROY, 0, 0);
for( int i=0; i<NUM_MOUSE_KEYS; i++)
{
if( m_mouseKeys[i] == KEY_UP )
{
if( mousestate.rgbButtons[i] )
m_mouseKeys[i] = KEY_PRESSED;
else
m_mouseKeys[i] = KEY_UP;
}
else if( m_mouseKeys[i] == KEY_PRESSED )
{
if( mousestate.rgbButtons[i] )
m_mouseKeys[i] = KEY_DOWN;
else
m_mouseKeys[i] = KEY_RELEASED;
}
else if( m_mouseKeys[i] == KEY_DOWN )
{
if( mousestate.rgbButtons[i] )
m_mouseKeys[i] = KEY_DOWN;
else
m_mouseKeys[i] = KEY_RELEASED;
}
else if( m_mouseKeys[i] == KEY_RELEASED )
{
if( mousestate.rgbButtons[i] )
m_mouseKeys[i] = KEY_PRESSED;
else
m_mouseKeys[i] = KEY_UP;
}
else
{
// should NEVER happen!
m_mouseKeys[i] = KEY_UP;
}
}
}
bool DIInputHandler::getKey(int key)
{
return keys[key];
}
int DIInputHandler::getMouseKeyState( int p_key )
{
return m_mouseKeys[p_key];
}
long DIInputHandler::getMouse(int axis)
{
//DEBUG: return std values
if(axis == X)
{
return mousestate.lX;
}
else if (axis == Y)
{
return mousestate.lY;
}
else
{
return 0;
}
}