-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cpp
170 lines (154 loc) · 3.78 KB
/
Input.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
#include "Input.hpp"
namespace si
{
//Input::Input()
//{
// Keys = new byte[256];
// axs = new long[4];
//}
Input::Input(HWND& _hWnd)
{
axX = 0;
axY = 0;
axZ = 0;
nmX = 0.f;
nmY = 0.f;
Keys = new byte[256];
axs = new float[4];
for (bool& b : Buttons)
{
b = false;
}
hWnd = _hWnd;
// std::cout << "Volam fci prepareInputDevices.\n";
prepareInputDevices();
}
void Input::prepareInputDevices()
{
HRESULT hr;
// for (int i = 8; i < 8; i++)
// Buttons[i] = false;
if (FAILED(hr=DirectInput8Create(GetModuleHandle(NULL), 0x0800, IID_IDirectInput8, ( VOID** )&diiDI, NULL )))
std::cout << hr;
if (FAILED (hr=diiDI->CreateDevice(GUID_SysMouse,&diMouse, NULL)))
std::cout << hr;
if (FAILED (hr=diMouse->SetDataFormat(&c_dfDIMouse2)))
std::cout << hr;
if (FAILED (hr=diMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
std::cout << hr;
//mouse
if (FAILED (hr=diiDI->CreateDevice(GUID_SysKeyboard,&diKeybrd, NULL)))
std::cout << hr;
if (FAILED (hr=diKeybrd->SetDataFormat(&c_dfDIKeyboard)))
std::cout << hr;
if (FAILED (hr=diKeybrd->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
std::cout << hr;
// axs = new long[4];
// Keys = new byte[256];
// std::cout << S_OK;
}
void Input::reset()
{
axs[0] = 0.f;
axs[1] = 0.f;
axs[2] = 0.f;
axs[3] = 0.f;
axs[4] = 0.f;
for ( int i = 0; i < 256; ++i)
{
Keys[i] = 0;
}
}
void Input::releaseDin()
{
if(diKeybrd != NULL){
diKeybrd->Release();
diKeybrd = NULL;
}
if(diMouse != NULL){
diMouse->Release();
diMouse = NULL;
}
if(diiDI != NULL){
diiDI->Release();
diiDI = NULL;
}
}
Input::~Input()
{
#ifdef DESTRUCTORVERBOSE
std::cout << "Destructor Inputu." << std::endl;
#endif // DESTRUCTORVERBOSE
#ifdef DESTRUCTORVERBOSE
std::cout << "delete[] keys." << std::endl;
#endif // DESTRUCTORVERBOSE
delete[] Keys;
#ifdef DESTRUCTORVERBOSE
std::cout << "delete[] axs." << std::endl;
#endif // DESTRUCTORVERBOSE
delete[] axs;
#ifdef DESTRUCTORVERBOSE
std::cout << "release dins." << std::endl;
#endif // DESTRUCTORVERBOSE
releaseDin();
#ifdef DESTRUCTORVERBOSE
std::cout << "ok." << std::endl;
#endif // DESTRUCTORVERBOSE
// delete[] Buttons;
}
std::string& Input::prectiStavVstupu()
{
// axs = new long[4];
// Keys = new byte[256];
HRESULT hr;
DIMOUSESTATE2 ms;
hr = diMouse->Poll();
if (FAILED(hr)) {
hr = diMouse->Acquire();
while(hr == DIERR_INPUTLOST)
hr = diMouse->Acquire();
vysledek = "nic";
return vysledek;
}
diMouse->GetDeviceState(sizeof(DIMOUSESTATE2), &ms);
for (int i = 0; i < 8; i++) {
if (ms.rgbButtons[i])
Buttons[i] = true;
else
Buttons[i] = false;
}
if(true || Buttons[2]) { //timto zpusobuji kontrolu pohybu mysi tlacitkem
axX -= ms.lX; //Mouse
axY -= (ms.lY);//*PI/180;
axZ += ms.lZ;
}
if(Buttons[1]) { // odejit
vysledek = "odejdi";
return vysledek;
}
if(Buttons[0]){
vysledek = "gener";
}
else
vysledek = "nic";
// nmX+=ms.lX;
// nmY+=ms.lY;
//axX += ms.lX;
axs[0] = axX;
axs[1] = axY;
axs[2] = axZ;
axs[3] = nmX;
axs[4] = nmY;
hr = diKeybrd->Poll(); //Klavesnice
if (FAILED(hr)) {
hr = diKeybrd->Acquire();
while(hr == DIERR_INPUTLOST)
hr = diKeybrd->Acquire();
vysledek = "nic";
return vysledek;
}
diKeybrd->GetDeviceState( 256, Keys );
// vysledek = "nic";
return vysledek;
}
}