Magic_Game/core/modules/Input.cpp

154 lines
2.8 KiB
C++
Raw Normal View History

2018-09-05 13:17:07 +08:00
#include "..\e2dmodule.h"
2018-09-05 13:33:39 +08:00
#include "..\e2dtool.h"
2018-01-30 16:45:38 +08:00
2018-10-03 18:04:04 +08:00
e2d::Input::Input(HWND hwnd)
: direct_input_(nullptr)
, keyboard_device_(nullptr)
, mouse_device_(nullptr)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
ZeroMemory(key_buffer_, sizeof(key_buffer_));
ZeroMemory(&mouse_state_, sizeof(mouse_state_));
2018-01-30 16:45:38 +08:00
// <20><>ʼ<EFBFBD><CABC><EFBFBD>ӿڶ<D3BF><DAB6><EFBFBD>
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
DirectInput8Create(
HINST_THISCOMPONENT,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
2018-09-04 22:42:34 +08:00
(void**)&direct_input_,
2018-08-28 00:06:10 +08:00
nullptr
)
2018-01-30 16:45:38 +08:00
);
2018-08-15 00:06:03 +08:00
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
direct_input_->CreateDevice(
2018-08-15 00:06:03 +08:00
GUID_SysKeyboard,
2018-09-04 22:42:34 +08:00
&keyboard_device_,
2018-08-15 00:06:03 +08:00
nullptr
)
);
2018-09-04 22:42:34 +08:00
keyboard_device_->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
keyboard_device_->SetDataFormat(&c_dfDIKeyboard);
keyboard_device_->Acquire();
keyboard_device_->Poll();
2018-08-15 00:06:03 +08:00
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
direct_input_->CreateDevice(
2018-08-15 00:06:03 +08:00
GUID_SysMouse,
2018-09-04 22:42:34 +08:00
&mouse_device_,
2018-08-15 00:06:03 +08:00
nullptr
)
);
2018-09-04 22:42:34 +08:00
mouse_device_->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
mouse_device_->SetDataFormat(&c_dfDIMouse);
mouse_device_->Acquire();
mouse_device_->Poll();
}
2018-08-28 00:06:10 +08:00
e2d::Input::~Input()
{
2018-09-04 22:42:34 +08:00
if (keyboard_device_)
keyboard_device_->Unacquire();
if (mouse_device_)
mouse_device_->Unacquire();
2018-08-28 00:06:10 +08:00
2018-09-04 22:42:34 +08:00
SafeRelease(mouse_device_);
SafeRelease(keyboard_device_);
SafeRelease(direct_input_);
2018-08-28 00:06:10 +08:00
}
2018-10-03 18:04:04 +08:00
void e2d::Input::Flush()
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
if (keyboard_device_)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
HRESULT hr = keyboard_device_->Poll();
2018-01-30 16:45:38 +08:00
if (FAILED(hr))
{
2018-09-04 22:42:34 +08:00
hr = keyboard_device_->Acquire();
2018-01-30 16:45:38 +08:00
while (hr == DIERR_INPUTLOST)
2018-09-04 22:42:34 +08:00
hr = keyboard_device_->Acquire();
2018-01-30 16:45:38 +08:00
}
else
{
2018-10-03 18:04:04 +08:00
keyboard_device_->GetDeviceState(
sizeof(key_buffer_),
(void**)&key_buffer_
);
2018-01-30 16:45:38 +08:00
}
}
2018-09-04 22:42:34 +08:00
if (mouse_device_)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
HRESULT hr = mouse_device_->Poll();
2018-01-30 16:45:38 +08:00
if (FAILED(hr))
{
2018-09-04 22:42:34 +08:00
hr = mouse_device_->Acquire();
2018-01-30 16:45:38 +08:00
while (hr == DIERR_INPUTLOST)
2018-09-04 22:42:34 +08:00
hr = mouse_device_->Acquire();
2018-01-30 16:45:38 +08:00
}
else
{
2018-10-03 18:04:04 +08:00
mouse_device_->GetDeviceState(
sizeof(mouse_state_),
(void**)&mouse_state_
);
2018-01-30 16:45:38 +08:00
}
}
}
2018-09-04 22:42:34 +08:00
bool e2d::Input::IsDown(KeyCode key)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
if (key_buffer_[static_cast<int>(key)] & 0x80)
2018-01-30 16:45:38 +08:00
return true;
return false;
}
2018-09-04 22:42:34 +08:00
bool e2d::Input::IsDown(MouseCode code)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
if (mouse_state_.rgbButtons[static_cast<int>(code)] & 0x80)
2018-01-30 16:45:38 +08:00
return true;
return false;
}
2018-09-04 22:42:34 +08:00
float e2d::Input::GetMouseX()
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
return GetMousePos().x;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
float e2d::Input::GetMouseY()
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
return GetMousePos().y;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
e2d::Point e2d::Input::GetMousePos()
2018-01-30 16:45:38 +08:00
{
2018-10-03 18:04:04 +08:00
HDC hdc = ::GetDC(0);
int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
2018-07-17 22:16:56 +08:00
POINT mousePos;
2018-07-28 22:22:58 +08:00
::GetCursorPos(&mousePos);
2018-10-03 18:04:04 +08:00
::ScreenToClient(Game::GetInstance()->GetHWnd(), &mousePos);
return Point(mousePos.x * 96.f / dpi_x, mousePos.y * 96.f / dpi_y);
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
float e2d::Input::GetMouseDeltaX()
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
return (float)mouse_state_.lX;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
float e2d::Input::GetMouseDeltaY()
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
return (float)mouse_state_.lY;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
float e2d::Input::GetMouseDeltaZ()
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
return (float)mouse_state_.lZ;
}