Magic_Game/core/Base/Input.cpp

173 lines
3.1 KiB
C++
Raw Normal View History

2018-09-05 13:17:07 +08:00
#include "..\e2dmodule.h"
#include "..\e2dutil.h"
2018-04-21 21:24:46 +08:00
#include "..\e2dmanager.h"
2018-01-30 16:45:38 +08:00
#pragma comment(lib, "dinput8.lib")
2018-09-04 22:42:34 +08:00
e2d::Input * e2d::Input::instance_ = nullptr;
2018-08-28 00:06:10 +08:00
2018-09-04 22:42:34 +08:00
e2d::Input * e2d::Input::GetInstance()
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if (!instance_)
instance_ = new (std::nothrow) Input;
return instance_;
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Input::DestroyInstance()
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if (instance_)
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
delete instance_;
instance_ = nullptr;
2018-08-28 00:06:10 +08:00
}
}
2018-07-04 15:33:09 +08:00
e2d::Input::Input()
2018-09-04 22:42:34 +08:00
: direct_input_(false)
, keyboard_device_(false)
, mouse_device_(false)
2018-01-30 16:45:38 +08:00
{
::CoInitialize(nullptr);
2018-07-04 15:33:09 +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-09-04 22:42:34 +08:00
HWND hwnd = Window::GetInstance()->GetHWnd();
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
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Input::Update()
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-09-04 22:42:34 +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-09-04 22:42:34 +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-09-04 22:42:34 +08:00
auto window = Window::GetInstance();
float dpi = window->GetDpi();
2018-07-17 22:16:56 +08:00
POINT mousePos;
2018-07-28 22:22:58 +08:00
::GetCursorPos(&mousePos);
2018-09-04 22:42:34 +08:00
::ScreenToClient(window->GetHWnd(), &mousePos);
2018-07-28 22:22:58 +08:00
return Point(mousePos.x * 96.f / dpi, mousePos.y * 96.f / dpi);
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;
}