Magic_Game/core/Base/Input.cpp

229 lines
4.4 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-05-14 22:51:40 +08:00
#define BUFFER_SIZE 256
2018-05-17 23:53:27 +08:00
static char s_KeyBuffer[BUFFER_SIZE] = { 0 }; // <20><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD><CCB0><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static char s_KeyRecordBuffer[BUFFER_SIZE] = { 0 }; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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)
, _mouseState()
, _mouseStateRecord()
, _mousePos()
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
CoInitialize(nullptr);
2018-01-30 16:45:38 +08:00
ZeroMemory(s_KeyBuffer, sizeof(s_KeyBuffer));
2018-02-02 10:06:29 +08:00
ZeroMemory(s_KeyRecordBuffer, sizeof(s_KeyRecordBuffer));
2018-07-04 15:33:09 +08:00
ZeroMemory(&_mouseState, sizeof(_mouseState));
ZeroMemory(&_mouseStateRecord, sizeof(_mouseStateRecord));
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-05-14 22:51:40 +08:00
for (int i = 0; i < BUFFER_SIZE; ++i)
2018-02-02 10:06:29 +08:00
s_KeyRecordBuffer[i] = s_KeyBuffer[i];
2018-01-30 16:45:38 +08:00
2018-07-04 15:33:09 +08:00
_keyboardDevice->GetDeviceState(sizeof(s_KeyBuffer), (void**)&s_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-04 15:33:09 +08:00
_mouseStateRecord = _mouseState;
_mouseDevice->GetDeviceState(sizeof(_mouseState), (void**)&_mouseState);
2018-01-30 16:45:38 +08:00
}
}
2018-07-04 15:33:09 +08:00
GetCursorPos(&_mousePos);
ScreenToClient(Window::getInstance()->getHWnd(), &_mousePos);
2018-01-30 16:45:38 +08:00
}
2018-07-04 15:33:09 +08:00
bool e2d::Input::isDown(Key key)
2018-01-30 16:45:38 +08:00
{
if (s_KeyBuffer[static_cast<int>(key)] & 0x80)
2018-01-30 16:45:38 +08:00
return true;
return false;
}
2018-07-04 15:33:09 +08:00
bool e2d::Input::isPress(Key key)
2018-01-30 16:45:38 +08:00
{
if ((s_KeyBuffer[static_cast<int>(key)] & 0x80) &&
!(s_KeyRecordBuffer[static_cast<int>(key)] & 0x80))
2018-01-30 16:45:38 +08:00
return true;
return false;
}
2018-07-04 15:33:09 +08:00
bool e2d::Input::isRelease(Key key)
2018-01-30 16:45:38 +08:00
{
if (!(s_KeyBuffer[static_cast<int>(key)] & 0x80) &&
(s_KeyRecordBuffer[static_cast<int>(key)] & 0x80))
2018-01-30 16:45:38 +08:00
return true;
return false;
}
2018-05-18 23:56:36 +08:00
bool e2d::Input::isDown(Mouse 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;
}
2018-05-18 23:56:36 +08:00
bool e2d::Input::isPress(Mouse 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) &&
!(_mouseStateRecord.rgbButtons[static_cast<int>(code)] & 0x80))
2018-01-30 16:45:38 +08:00
return true;
return false;
}
2018-05-18 23:56:36 +08:00
bool e2d::Input::isRelease(Mouse 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) &&
(_mouseStateRecord.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-04 15:33:09 +08:00
return (double)_mousePos.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-04 15:33:09 +08:00
return (double)_mousePos.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
{
2018-07-04 15:33:09 +08:00
return Point((double)_mousePos.x, (double)_mousePos.y);
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;
}