-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputclass.cpp
More file actions
196 lines (161 loc) · 4.05 KB
/
Copy pathinputclass.cpp
File metadata and controls
196 lines (161 loc) · 4.05 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
////////////////////////////////////////////////////////////////////////////////
// Filename: inputclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "inputclass.h"
InputClass::InputClass()
{
}
InputClass::InputClass(const InputClass& other)
{
}
InputClass::~InputClass()
{
}
void InputClass::Initialize(HINSTANCE hInstance, HWND hwnd)
{
HRESULT result;
int i;
errordebug = hwnd;
XMVECTOR EyePosition = XMVectorSet(0.0f, 0.0f, -5.0f, 0.0f);
// Initialize all the keys to being released and not pressed.
for (i = 0; i < 256; i++)
{
m_keys[i] = false;
}
// DirectInput 인터페이스 생성
result = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL);
if (FAILED(result))
{
MessageBox(hwnd, L"Failed to initialize DirectInput interface.", L"Error", MB_OK);
return;
}
// KeyBorad
result = m_directInput->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL);
if (FAILED(result))
{
MessageBox(hwnd, L"Failed to create keyboard device.", L"Error", MB_OK);
return;
}
result = m_keyboard->SetDataFormat(&c_dfDIKeyboard);
if (FAILED(result))
{
MessageBox(hwnd, L"Failed to set keyboard data format.", L"Error", MB_OK);
return;
}
result = m_keyboard->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
if (FAILED(result))
{
MessageBox(hwnd, L"Failed to set keyboard cooperative level.", L"Error", MB_OK);
return;
}
// Mouse
result = m_directInput->CreateDevice(GUID_SysMouse, &m_mouse, NULL);
if (FAILED(result))
{
MessageBox(hwnd, L"Failed to create mouse device.", L"Error", MB_OK);
return;
}
result = m_mouse->SetDataFormat(&c_dfDIMouse);
if (FAILED(result))
{
MessageBox(hwnd, L"Failed to set mouse data format.", L"Error", MB_OK);
return;
}
result = m_mouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
if (FAILED(result))
{
MessageBox(hwnd, L"Failed to set mouse cooperative level.", L"Error", MB_OK);
return;
}
return;
}
void InputClass::Shutdown()
{
if (m_mouse)
{
m_mouse->Unacquire();
m_mouse->Release();
m_mouse = 0;
}
if (m_keyboard)
{
m_keyboard->Unacquire();
m_keyboard->Release();
m_keyboard = nullptr;
}
if (m_directInput)
{
m_directInput->Release();
m_directInput = nullptr;
}
}
void InputClass::KeyDown(unsigned int input)
{
// If a key is pressed then save that state in the key array.
m_keys[input] = true;
return;
}
void InputClass::KeyUp(unsigned int input)
{
// If a key is released then clear that state in the key array.
m_keys[input] = false;
return;
}
bool InputClass::IsKeyDown(unsigned int key)
{
// Return what state the key is in (pressed/not pressed).
return m_keys[key];
}
void InputClass::SetCamera(CameraClass* camera)
{
m_camera = camera;
}
void InputClass::DetectInput(double time)
{
HRESULT result;
DIMOUSESTATE mouseCurrState;
BYTE keyboardState[256];
m_mouse->Acquire();
result = m_keyboard->Acquire();
if (FAILED(result))
{
MessageBox(errordebug, L"1 key", L"Error", MB_OK);
return;
}
m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseCurrState);
result = m_keyboard->GetDeviceState(sizeof(keyboardState), (LPVOID)&keyboardState);
if (FAILED(result))
{
MessageBox(errordebug, L"2key", L"Error", MB_OK);
return;
}
float speed = 10.0f * (float)time;
if ((mouseCurrState.lX != mouseLastState.lX) || (mouseCurrState.lY != mouseLastState.lY))
{
if (m_camera)
{
float yaw = mouseCurrState.lX * 0.05f; // 마우스 X축 이동으로 yaw 변경
float pitch = mouseCurrState.lY * 0.05f; // 마우스 Y축 이동으로 pitch 변경
m_camera->Rotate(yaw, pitch);
}
// 현재 마우스 상태 저장
mouseLastState = mouseCurrState;
}
// 키보드 입력 처리
if (keyboardState[DIK_W] & 0x80 /*or IsKeyDown(87) or IsKeyDown('w')*/)
{
m_camera->moveBackForward += speed * 5.0f;
}
if (keyboardState[DIK_S] & 0x80)
{
m_camera->moveBackForward -= speed *5.0f;
}
if (keyboardState[DIK_A] & 0x80)
{
m_camera->moveLeftRight -= speed* 5.0f;
}
if (keyboardState[DIK_D] & 0x80)
{
m_camera->moveLeftRight += speed *5.0f;
}
}