-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
93 lines (72 loc) · 1.73 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
#include "input.h"
Input::Input() noexcept
: mDevice{ nullptr }
, mMouse{ nullptr }
, mKeyboard{ nullptr }
{
}
Input::~Input()
{
if (mMouse)
{
mMouse->Unacquire();
mMouse->Release();
}
if (mKeyboard)
{
mKeyboard->Unacquire();
mKeyboard->Release();
}
if (mDevice)
{
mDevice->Release();
}
}
bool Input::init(HWND hwnd, HINSTANCE hinstance) noexcept
{
if (DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8W, reinterpret_cast<void**>(&mDevice), nullptr) != DI_OK)
return false;
return (initMouse(hwnd) && initKeyboard(hwnd));
}
bool Input::update() noexcept
{
return (updateMouse() && updateKeyboard());
}
bool Input::initMouse(HWND hwnd) noexcept
{
if (mDevice->CreateDevice(GUID_SysMouse, &mMouse, nullptr) != DI_OK)
return false;
if (mMouse->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) != DI_OK)
return false;
if (mMouse->SetDataFormat(&c_dfDIMouse) != DI_OK)
return false;
const HRESULT hr = mMouse->Acquire();
if (hr != DI_OK && hr != S_FALSE)
return false;
return true;
}
bool Input::updateMouse() noexcept
{
if (mMouse->GetDeviceState(sizeof DIMOUSESTATE, &mouseState) != DI_OK)
return false;
return true;
}
bool Input::initKeyboard(HWND hwnd) noexcept
{
if (mDevice->CreateDevice(GUID_SysKeyboard, &mKeyboard, nullptr) != DI_OK)
return false;
if (mKeyboard->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) != DI_OK)
return false;
if (mKeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK)
return false;
const HRESULT hr = mKeyboard->Acquire();
if (hr != DI_OK && hr != S_FALSE)
return false;
return true;
}
bool Input::updateKeyboard() noexcept
{
if (mKeyboard->GetDeviceState(256, &keyState) != DI_OK)
return false;
return true;
}