forked from abartlett139/Omni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cpp
222 lines (198 loc) · 5.39 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "Input.h"
#include <string>
#define KEYDOWN(name,key) (name[key] & 0x80)
///------------------------------------------------------------------Input------------------------------------------------------------------------
Input::Input(HINSTANCE hinst, HWND hWnd)
{
m_pInput = NULL;
m_hWnd = hWnd;
DirectInput8Create(hinst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pInput, NULL);
}
Input::~Input()
{
if (m_pInput != NULL)
m_pInput->Release();
}
Keyboard * Input::CreateKeyboard()
{
Keyboard* l_keyboard = NULL;
if (m_pInput)
{
l_keyboard = new Keyboard(m_pInput, m_hWnd);
}
return l_keyboard;
}
Mouse * Input::CreateMouse(LPDIRECT3DDEVICE9 pDevice, bool Exclusive)
{
Mouse* l_mouse = NULL;
if (m_pInput)
{
l_mouse = new Mouse(pDevice, m_pInput, m_hWnd, Exclusive);
}
return l_mouse;
}
///----------------------------------------------------------------------Keyboard--------------------------------------------------------------------
Keyboard::Keyboard(LPDIRECTINPUT8 pInput, HWND hWnd)
{
HRESULT Result = E_FAIL;
m_pInputDevice = NULL;
Result = pInput->CreateDevice(GUID_SysKeyboard, &m_pInputDevice, NULL);
if (SUCCEEDED(Result))
{
Result = m_pInputDevice->SetDataFormat(&c_dfDIKeyboard);
if (FAILED(Result))
{
SafeRelease(m_pInputDevice);
return;
}
Result = m_pInputDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
if (FAILED(Result))
{
SafeRelease(m_pInputDevice);
return;
}
memset(m_KeyState, 0, 256 * sizeof(char));
}
}
Keyboard::~Keyboard()
{
m_pInputDevice->Unacquire();
SafeRelease(m_pInputDevice);
}
bool Keyboard::IsKeyPressed(int Key)
{
if (m_pInputDevice)
{
if (m_KeyState[Key] & 0x80)
return true;
}
return false;
}
HRESULT Keyboard::Update()
{
HRESULT Result = E_FAIL;
if (m_pInputDevice)
{
Result = m_pInputDevice->Acquire();
if (FAILED(Result))
return Result;
Result = m_pInputDevice->GetDeviceState(sizeof(m_KeyState), (LPVOID)&m_KeyState);
if (FAILED(Result))
return Result;
}
return S_OK;
}
///----------------------------------------------------------------Mouse-------------------------------------------------------------------------------
Mouse::Mouse( LPDIRECT3DDEVICE9 pDevice, LPDIRECTINPUT8 pInput, HWND hWnd,
bool Exclusive/*, D3DDISPLAYMODE Mode*/ ): m_pInputDevice( NULL )
{
Device = NULL;
//Initial cursor position
m_iX = 0;
m_iY = 0;
if( pInput&&pDevice )
{
HRESULT Result = E_FAIL;
Device = pDevice;
Result = pInput->CreateDevice( GUID_SysMouse, &m_pInputDevice, NULL );
if( FAILED( Result ) )
return;
Result = m_pInputDevice->SetDataFormat( &c_dfDIMouse );
if( FAILED( Result ) )
{
SafeRelease( m_pInputDevice );
return;
}
if( Exclusive )
{
Result = m_pInputDevice->SetCooperativeLevel( hWnd, DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND );
}
else
{
Result = m_pInputDevice->SetCooperativeLevel( hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND );
}
if( FAILED( Result ) )
{
SafeRelease( m_pInputDevice );
return;
}
D3DXIMAGE_INFO ImageInfo;
//load image iformation
D3DXGetImageInfoFromFile( "textures/cursor.png", &ImageInfo );
Device->CreateOffscreenPlainSurface( ImageInfo.Height, ImageInfo.Width, ImageInfo.Format, D3DPOOL_DEFAULT, &m_Cursor, NULL );
D3DXLoadSurfaceFromFile( m_Cursor, NULL, NULL, "textures/cursor.png", NULL, D3DX_FILTER_NONE, D3DCOLOR_XRGB( 255, 255, 255 ), &ImageInfo );
Device->SetCursorProperties( 0, 0, m_Cursor );
Device->SetCursorPosition( 0, 0, D3DCURSOR_IMMEDIATE_UPDATE );
m_Changed = false;
m_Buttons = false;
}
}
Mouse::~Mouse( )
{
if( m_pInputDevice )
{
m_pInputDevice->Unacquire( );
SafeRelease( m_pInputDevice );
}
SafeRelease(m_Cursor);
//delete m_Surf;
}
HRESULT Mouse::Update( )
{
HRESULT Result = E_FAIL;
//long OldX, OldY;
bool Pressed = this->IsButtonPressed(0);
m_Changed = false;
if (m_pInputDevice)
{
if( SUCCEEDED( m_pInputDevice->Acquire( ) ) )
{
Result = m_pInputDevice->GetDeviceState( sizeof( DIMOUSESTATE ), (void*)&m_State );
if( FAILED( Result ) )
return Result;
}
else
return Result;
Result = m_pInputDevice->Poll();
if (FAILED(Result))
return Result;
if (this->IsButtonPressed(0) != Pressed)
m_Buttons = true;
else
m_Buttons = false;
m_iX += m_State.lX;
m_iY += m_State.lY;
}
return Result;
}
LONG Mouse::GetXPos( )
{
return m_iX;
}
LONG Mouse::GetYPos( )
{
return m_iY;
}
bool Mouse::IsButtonPressed( int Button )
{
if (KEYDOWN(m_State.rgbButtons, Button))
return true;
else
return false;
}
HRESULT Mouse::SetMouseCursor( char * FilePath, UINT x, UINT y, int Type )
{
return E_NOTIMPL;
}
void Mouse::SetCursorImage( int Type )
{
Device->SetCursorProperties(m_iX, m_iX, m_Cursor);
}
void Mouse::SetCursorPosition( int x, int y )
{
Device->SetCursorPosition(x, y, 0);
}
HRESULT Mouse::SetCursorVisible( bool Show )
{
return Device->ShowCursor(Show);
}