Magic_Game/core/Base/Input.cpp

223 lines
4.2 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dbase.h"
#include "..\e2dtool.h"
#include "..\e2dmanager.h"
2018-01-30 16:45:38 +08:00
#pragma comment(lib, "dinput8.lib")
2018-07-04 15:33:09 +08:00
e2d::Input * e2d::Input::_instance = nullptr;
2018-01-30 16:45:38 +08:00
2018-07-04 15:33:09 +08:00
e2d::Input::Input()
: _directInput(false)
, _keyboardDevice(false)
, _mouseDevice(false)
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
CoInitialize(nullptr);
2018-07-17 22:16:56 +08:00
ZeroMemory(_keyBuffer, sizeof(_keyBuffer));
ZeroMemory(_keyRecordBuffer, sizeof(_keyRecordBuffer));
2018-07-04 15:33:09 +08:00
ZeroMemory(&_mouseState, sizeof(_mouseState));
2018-07-17 22:16:56 +08:00
ZeroMemory(&_mouseRecordState, sizeof(_mouseRecordState));
2018-01-30 16:45:38 +08:00
// <20><>ʼ<EFBFBD><CABC><EFBFBD>ӿڶ<D3BF><DAB6><EFBFBD>
HRESULT hr = DirectInput8Create(
HINST_THISCOMPONENT,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
2018-07-04 15:33:09 +08:00
(void**)&_directInput,
2018-02-01 22:07:44 +08:00
nullptr
2018-01-30 16:45:38 +08:00
);
auto window = Window::getInstance();
2018-01-30 16:45:38 +08:00
if (SUCCEEDED(hr))
{
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-07-04 15:33:09 +08:00
hr = _directInput->CreateDevice(
2018-01-30 16:45:38 +08:00
GUID_SysKeyboard,
2018-07-04 15:33:09 +08:00
&_keyboardDevice,
2018-02-01 22:07:44 +08:00
nullptr
2018-01-30 16:45:38 +08:00
);
if (SUCCEEDED(hr))
{
2018-07-04 15:33:09 +08:00
_keyboardDevice->SetCooperativeLevel(
window->getHWnd(),
2018-01-30 16:45:38 +08:00
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE
);
2018-07-04 15:33:09 +08:00
_keyboardDevice->SetDataFormat(
2018-01-30 16:45:38 +08:00
&c_dfDIKeyboard);
2018-07-04 15:33:09 +08:00
_keyboardDevice->Acquire();
_keyboardDevice->Poll();
2018-01-30 16:45:38 +08:00
}
else
{
2018-07-04 15:33:09 +08:00
throw SystemException(L"Keyboard not found!");
2018-01-30 16:45:38 +08:00
}
}
if (SUCCEEDED(hr))
{
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-07-04 15:33:09 +08:00
hr = _directInput->CreateDevice(GUID_SysMouse, &_mouseDevice, nullptr);
2018-01-30 16:45:38 +08:00
if (SUCCEEDED(hr))
{
2018-07-04 15:33:09 +08:00
_mouseDevice->SetCooperativeLevel(window->getHWnd(), DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
_mouseDevice->SetDataFormat(&c_dfDIMouse);
_mouseDevice->Acquire();
_mouseDevice->Poll();
2018-01-30 16:45:38 +08:00
}
else
{
2018-07-04 15:33:09 +08:00
throw SystemException(L"Mouse not found!");
2018-01-30 16:45:38 +08:00
}
}
2018-07-04 15:33:09 +08:00
}
2018-01-30 16:45:38 +08:00
2018-07-04 15:33:09 +08:00
e2d::Input::~Input()
{
if (_keyboardDevice)
_keyboardDevice->Unacquire();
if (_mouseDevice)
_mouseDevice->Unacquire();
SafeRelease(_mouseDevice);
SafeRelease(_keyboardDevice);
SafeRelease(_directInput);
CoUninitialize();
2018-01-30 16:45:38 +08:00
}
2018-07-04 15:33:09 +08:00
e2d::Input * e2d::Input::getInstance()
{
2018-07-04 15:33:09 +08:00
if (!_instance)
_instance = new (std::nothrow) Input;
return _instance;
}
2018-07-04 15:33:09 +08:00
void e2d::Input::destroyInstance()
{
if (_instance)
{
delete _instance;
_instance = nullptr;
}
}
void e2d::Input::update()
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
if (_keyboardDevice)
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
HRESULT hr = _keyboardDevice->Poll();
2018-01-30 16:45:38 +08:00
if (FAILED(hr))
{
2018-07-04 15:33:09 +08:00
hr = _keyboardDevice->Acquire();
2018-01-30 16:45:38 +08:00
while (hr == DIERR_INPUTLOST)
2018-07-04 15:33:09 +08:00
hr = _keyboardDevice->Acquire();
2018-01-30 16:45:38 +08:00
}
else
{
2018-07-17 22:16:56 +08:00
strcpy_s(_keyRecordBuffer, 256, _keyBuffer);
_keyboardDevice->GetDeviceState(sizeof(_keyBuffer), (void**)&_keyBuffer);
2018-01-30 16:45:38 +08:00
}
}
2018-07-04 15:33:09 +08:00
if (_mouseDevice)
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
HRESULT hr = _mouseDevice->Poll();
2018-01-30 16:45:38 +08:00
if (FAILED(hr))
{
2018-07-04 15:33:09 +08:00
hr = _mouseDevice->Acquire();
2018-01-30 16:45:38 +08:00
while (hr == DIERR_INPUTLOST)
2018-07-04 15:33:09 +08:00
hr = _mouseDevice->Acquire();
2018-01-30 16:45:38 +08:00
}
else
{
2018-07-17 22:16:56 +08:00
_mouseRecordState = _mouseState;
2018-07-04 15:33:09 +08:00
_mouseDevice->GetDeviceState(sizeof(_mouseState), (void**)&_mouseState);
2018-01-30 16:45:38 +08:00
}
}
}
bool e2d::Input::isDown(KeyCode key)
2018-01-30 16:45:38 +08:00
{
2018-07-17 22:16:56 +08:00
if (_keyBuffer[static_cast<int>(key)] & 0x80)
2018-01-30 16:45:38 +08:00
return true;
return false;
}
bool e2d::Input::isPress(KeyCode key)
2018-01-30 16:45:38 +08:00
{
2018-07-17 22:16:56 +08:00
if ((_keyBuffer[static_cast<int>(key)] & 0x80) &&
!(_keyRecordBuffer[static_cast<int>(key)] & 0x80))
2018-01-30 16:45:38 +08:00
return true;
return false;
}
bool e2d::Input::isRelease(KeyCode key)
2018-01-30 16:45:38 +08:00
{
2018-07-17 22:16:56 +08:00
if (!(_keyBuffer[static_cast<int>(key)] & 0x80) &&
(_keyRecordBuffer[static_cast<int>(key)] & 0x80))
2018-01-30 16:45:38 +08:00
return true;
return false;
}
bool e2d::Input::isDown(MouseCode code)
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
if (_mouseState.rgbButtons[static_cast<int>(code)] & 0x80)
2018-01-30 16:45:38 +08:00
return true;
return false;
}
bool e2d::Input::isPress(MouseCode code)
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
if ((_mouseState.rgbButtons[static_cast<int>(code)] & 0x80) &&
2018-07-17 22:16:56 +08:00
!(_mouseRecordState.rgbButtons[static_cast<int>(code)] & 0x80))
2018-01-30 16:45:38 +08:00
return true;
return false;
}
bool e2d::Input::isRelease(MouseCode code)
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
if (!(_mouseState.rgbButtons[static_cast<int>(code)] & 0x80) &&
2018-07-17 22:16:56 +08:00
(_mouseRecordState.rgbButtons[static_cast<int>(code)] & 0x80))
2018-01-30 16:45:38 +08:00
return true;
return false;
}
2018-07-04 15:33:09 +08:00
double e2d::Input::getMouseX()
2018-01-30 16:45:38 +08:00
{
2018-07-17 22:16:56 +08:00
return getMousePos().x;
2018-01-30 16:45:38 +08:00
}
2018-07-04 15:33:09 +08:00
double e2d::Input::getMouseY()
2018-01-30 16:45:38 +08:00
{
2018-07-17 22:16:56 +08:00
return getMousePos().y;
2018-01-30 16:45:38 +08:00
}
2018-07-04 15:33:09 +08:00
e2d::Point e2d::Input::getMousePos()
2018-01-30 16:45:38 +08:00
{
HWND hWnd = Window::getInstance()->getHWnd();
2018-07-17 22:16:56 +08:00
POINT mousePos;
GetCursorPos(&mousePos);
ScreenToClient(hWnd, &mousePos);
2018-07-17 23:59:21 +08:00
UINT ret = ::GetDpiForWindow(hWnd);
return Point(mousePos.x * 96.0 / ret, mousePos.y * 96.0 / ret);
2018-01-30 16:45:38 +08:00
}
2018-07-04 15:33:09 +08:00
double e2d::Input::getMouseDeltaX()
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
return (double)_mouseState.lX;
2018-01-30 16:45:38 +08:00
}
2018-07-04 15:33:09 +08:00
double e2d::Input::getMouseDeltaY()
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
return (double)_mouseState.lY;
2018-01-30 16:45:38 +08:00
}
2018-07-04 15:33:09 +08:00
double e2d::Input::getMouseDeltaZ()
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
return (double)_mouseState.lZ;
}