-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg_util_input.cpp
More file actions
175 lines (151 loc) · 4.01 KB
/
Copy pathg_util_input.cpp
File metadata and controls
175 lines (151 loc) · 4.01 KB
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
//g_util_input.cpp
#include "g_util.h"
DIMOUSESTATE mouseState;
// DirectInput initialization for keyboard and mouse
bool g_util_input::initDI(void)
{
HRESULT hr; //DirectX result variable
//Create the main DirectInput object
hr= (DirectInput8Create(hinstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&lpdi, NULL));
if(FAILED(hr))
{
MessageBox(hwnd, "Could not create main DInput object", TITLE, MB_OK);
return false;
}
//Create the keyboard's device object
hr= (lpdi->CreateDevice(GUID_SysKeyboard, &lpdi_keyboard, NULL));
if(FAILED(hr))
{
MessageBox(hwnd, "Could not create keyboard's object",TITLE,MB_OK);
shutdownDI();
return false;
}
//Set the keyboard's data format
hr= (lpdi_keyboard->SetDataFormat(&c_dfDIKeyboard));
if(FAILED(hr))
{
MessageBox(hwnd, "Could not set keyboard's data format",TITLE,MB_OK);
shutdownDI();
return false;
}
//Set the keyboard's cooperation level with your computer's
//environment.
hr= (lpdi_keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND |
DISCL_NONEXCLUSIVE));
if(FAILED(hr))
{
MessageBox(hwnd, "Could not set keyboard's cooperation level",
TITLE, MB_OK);
shutdownDI();
return false;
}
//And finally, acquire the keyboard for use.
lpdi_keyboard->Acquire();
//Its time to get mousey
hr= lpdi->CreateDevice(GUID_SysMouse, &lpdiMouse, NULL);
if(FAILED(hr))
{
MessageBox(hwnd, "Could not set create the mouse device", "ERROR", MB_OK);
// S3Dlog.Output(" Could not set create the mouse device");
shutdownDI();
return FALSE;
}
//Set the mouse's data format
hr= lpdiMouse->SetDataFormat(&c_dfDIMouse);
if(FAILED(hr))
{
MessageBox(hwnd, "Could not set the mouse's data format", "ERROR", MB_OK);
// S3Dlog.Output(" Could not set the mouse's data format");
shutdownDI();
return FALSE;
}
//Set the mouse's cooperative level with that of windows
hr= lpdiMouse->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
if(FAILED(hr))
{
MessageBox(hwnd, "Could not set the mouse's behavior", "ERROR", MB_OK);
// S3Dlog.Output(" Could not set the mouse's behavior");
shutdownDI();
return FALSE;
}
//Acquire the mouse
hr= lpdiMouse->Acquire();
if(FAILED(hr))
{
MessageBox(hwnd, "Could not acquire the mouse", "ERROR", MB_OK);
// S3Dlog.Output(" Could not acquire the mouse");
shutdownDI();
}
//Set the mouse's device state
hr= lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseState);
if(FAILED(hr))
{
MessageBox(hwnd, "The mouse has been lost in initialization", "ERROR", MB_OK);
// S3Dlog.Output(" The mouse has been lost in initialization");
shutdownDI();
}
return true;
}
void g_util_input::shutdownDI(void)
{
if(lpdiMouse!=NULL)
{
//Unacquire the mouse
lpdiMouse->Unacquire();
//Throw the mouse away
lpdiMouse->Release();
lpdiMouse= NULL;
}
//First, since we create the keyboard last, we need to 'destroy'
//it first.
if(lpdi_keyboard!=NULL)
{
//Unacquire the keyboard
lpdi_keyboard->Unacquire();
//Let the keyboard go faster than your ex-girlfriend's phone number
lpdi_keyboard->Release();
lpdi_keyboard=NULL;
}
//Now the main DirectInput object
if(lpdi!=NULL)
{
//Kill, burn, and destroy the main object
lpdi->Release();
lpdi=NULL;
}
}
void g_util_input::updateDI(void)
{
HRESULT hr;
//First, check to see if the keyboard is still working/functioning
hr= (lpdi_keyboard->GetDeviceState(sizeof(UCHAR[256]),(LPVOID)&key_buffer));
if(FAILED(hr))
{
if(hr==DIERR_INPUTLOST)
{
//Try to re-acquire the keyboard
hr= (lpdi_keyboard->Acquire());
if(FAILED(hr))
{
MessageBox(hwnd, "Keyboard has been lost", TITLE, MB_OK);
shutdownDI();
}
}
}
//Now check the mouse
hr= (lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&mouseState));
if(FAILED(hr))
{
if(hr==DIERR_INPUTLOST)
{
//Try to re-acquire the keyboard
hr= (lpdiMouse->Acquire());
if(FAILED(hr))
{
MessageBox(hwnd, "The mouse has been lost", "ERROR", MB_OK);
shutdownDI();
}
}
}
}