ControllerInterface/DInput: Optimize cursor position updating.
This commit is contained in:
parent
4b1adab785
commit
8c1310d1d1
|
@ -64,7 +64,7 @@ void PopulateDevices(HWND hwnd)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
InitKeyboardMouse(idi8);
|
InitKeyboardMouse(idi8, hwnd);
|
||||||
InitJoystick(idi8, hwnd);
|
InitJoystick(idi8, hwnd);
|
||||||
|
|
||||||
idi8->Release();
|
idi8->Release();
|
||||||
|
|
|
@ -32,7 +32,7 @@ static const struct
|
||||||
// Prevent duplicate keyboard/mouse devices.
|
// Prevent duplicate keyboard/mouse devices.
|
||||||
static bool s_keyboard_mouse_exists = false;
|
static bool s_keyboard_mouse_exists = false;
|
||||||
|
|
||||||
void InitKeyboardMouse(IDirectInput8* const idi8)
|
void InitKeyboardMouse(IDirectInput8* const idi8, HWND hwnd)
|
||||||
{
|
{
|
||||||
if (s_keyboard_mouse_exists)
|
if (s_keyboard_mouse_exists)
|
||||||
return;
|
return;
|
||||||
|
@ -52,7 +52,7 @@ void InitKeyboardMouse(IDirectInput8* const idi8)
|
||||||
SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2)) &&
|
SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2)) &&
|
||||||
SUCCEEDED(mo_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
|
SUCCEEDED(mo_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
|
||||||
{
|
{
|
||||||
g_controller_interface.AddDevice(std::make_shared<KeyboardMouse>(kb_device, mo_device));
|
g_controller_interface.AddDevice(std::make_shared<KeyboardMouse>(kb_device, mo_device, hwnd));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,27 +75,22 @@ KeyboardMouse::~KeyboardMouse()
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyboardMouse::KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device,
|
KeyboardMouse::KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device,
|
||||||
const LPDIRECTINPUTDEVICE8 mo_device)
|
const LPDIRECTINPUTDEVICE8 mo_device, HWND hwnd)
|
||||||
: m_kb_device(kb_device), m_mo_device(mo_device)
|
: m_kb_device(kb_device), m_mo_device(mo_device), m_hwnd(hwnd), m_last_update(GetTickCount()),
|
||||||
|
m_state_in()
|
||||||
{
|
{
|
||||||
s_keyboard_mouse_exists = true;
|
s_keyboard_mouse_exists = true;
|
||||||
|
|
||||||
m_kb_device->Acquire();
|
m_kb_device->Acquire();
|
||||||
m_mo_device->Acquire();
|
m_mo_device->Acquire();
|
||||||
|
|
||||||
m_last_update = GetTickCount();
|
|
||||||
|
|
||||||
ZeroMemory(&m_state_in, sizeof(m_state_in));
|
|
||||||
|
|
||||||
// KEYBOARD
|
// KEYBOARD
|
||||||
// add keys
|
// add keys
|
||||||
for (u8 i = 0; i < sizeof(named_keys) / sizeof(*named_keys); ++i)
|
for (u8 i = 0; i < sizeof(named_keys) / sizeof(*named_keys); ++i)
|
||||||
AddInput(new Key(i, m_state_in.keyboard[named_keys[i].code]));
|
AddInput(new Key(i, m_state_in.keyboard[named_keys[i].code]));
|
||||||
|
|
||||||
// MOUSE
|
// MOUSE
|
||||||
// get caps
|
DIDEVCAPS mouse_caps = {};
|
||||||
DIDEVCAPS mouse_caps;
|
|
||||||
ZeroMemory(&mouse_caps, sizeof(mouse_caps));
|
|
||||||
mouse_caps.dwSize = sizeof(mouse_caps);
|
mouse_caps.dwSize = sizeof(mouse_caps);
|
||||||
m_mo_device->GetCapabilities(&mouse_caps);
|
m_mo_device->GetCapabilities(&mouse_caps);
|
||||||
// mouse buttons
|
// mouse buttons
|
||||||
|
@ -115,35 +110,26 @@ KeyboardMouse::KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device,
|
||||||
AddInput(new Cursor(!!(i & 2), (&m_state_in.cursor.x)[i / 2], !!(i & 1)));
|
AddInput(new Cursor(!!(i & 2), (&m_state_in.cursor.x)[i / 2], !!(i & 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetMousePos(ControlState* const x, ControlState* const y)
|
void KeyboardMouse::UpdateCursorInput()
|
||||||
{
|
{
|
||||||
POINT point = {1, 1};
|
POINT point = {};
|
||||||
GetCursorPos(&point);
|
GetCursorPos(&point);
|
||||||
// Get the cursor position relative to the upper left corner of the current window (separate or
|
|
||||||
// render to main)
|
|
||||||
HWND hwnd = WindowFromPoint(point);
|
|
||||||
DWORD processId;
|
|
||||||
GetWindowThreadProcessId(hwnd, &processId);
|
|
||||||
if (processId == GetCurrentProcessId())
|
|
||||||
{
|
|
||||||
ScreenToClient(hwnd, &point);
|
|
||||||
|
|
||||||
// Get the size of the current window. (In my case Rect.top and Rect.left was zero.)
|
// Get the cursor position relative to the upper left corner of the current window
|
||||||
RECT rect;
|
// (separate or render to main)
|
||||||
GetClientRect(hwnd, &rect);
|
ScreenToClient(m_hwnd, &point);
|
||||||
// Width and height is the size of the rendering window
|
|
||||||
unsigned int win_width = rect.right - rect.left;
|
|
||||||
unsigned int win_height = rect.bottom - rect.top;
|
|
||||||
|
|
||||||
// Return the mouse position as a range from -1 to 1
|
// Get the size of the current window. (In my case Rect.top and Rect.left was zero.)
|
||||||
*x = (ControlState)point.x / (ControlState)win_width * 2 - 1;
|
RECT rect;
|
||||||
*y = (ControlState)point.y / (ControlState)win_height * 2 - 1;
|
GetClientRect(m_hwnd, &rect);
|
||||||
}
|
|
||||||
else
|
// Width and height are the size of the rendering window.
|
||||||
{
|
const auto win_width = rect.right - rect.left;
|
||||||
*x = (ControlState)1;
|
const auto win_height = rect.bottom - rect.top;
|
||||||
*y = (ControlState)1;
|
|
||||||
}
|
// Convert the cursor position to a range from -1 to 1.
|
||||||
|
m_state_in.cursor.x = ControlState(point.x) / win_width * 2 - 1;
|
||||||
|
m_state_in.cursor.y = ControlState(point.y) / win_height * 2 - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyboardMouse::UpdateInput()
|
void KeyboardMouse::UpdateInput()
|
||||||
|
@ -155,7 +141,7 @@ void KeyboardMouse::UpdateInput()
|
||||||
if (cur_time - m_last_update > DROP_INPUT_TIME)
|
if (cur_time - m_last_update > DROP_INPUT_TIME)
|
||||||
{
|
{
|
||||||
// set axes to zero
|
// set axes to zero
|
||||||
ZeroMemory(&m_state_in.mouse, sizeof(m_state_in.mouse));
|
m_state_in.mouse = {};
|
||||||
// skip this input state
|
// skip this input state
|
||||||
m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse);
|
m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse);
|
||||||
}
|
}
|
||||||
|
@ -180,8 +166,7 @@ void KeyboardMouse::UpdateInput()
|
||||||
// copy over the buttons
|
// copy over the buttons
|
||||||
memcpy(m_state_in.mouse.rgbButtons, tmp_mouse.rgbButtons, sizeof(m_state_in.mouse.rgbButtons));
|
memcpy(m_state_in.mouse.rgbButtons, tmp_mouse.rgbButtons, sizeof(m_state_in.mouse.rgbButtons));
|
||||||
|
|
||||||
// update mouse cursor
|
UpdateCursorInput();
|
||||||
GetMousePos(&m_state_in.cursor.x, &m_state_in.cursor.y);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,12 +220,12 @@ ControlState KeyboardMouse::Button::GetState() const
|
||||||
|
|
||||||
ControlState KeyboardMouse::Axis::GetState() const
|
ControlState KeyboardMouse::Axis::GetState() const
|
||||||
{
|
{
|
||||||
return std::max(0.0, ControlState(m_axis) / m_range);
|
return ControlState(m_axis) / m_range;
|
||||||
}
|
}
|
||||||
|
|
||||||
ControlState KeyboardMouse::Cursor::GetState() const
|
ControlState KeyboardMouse::Cursor::GetState() const
|
||||||
{
|
{
|
||||||
return std::max(0.0, ControlState(m_axis) / (m_positive ? 1.0 : -1.0));
|
return m_axis / (m_positive ? 1.0 : -1.0);
|
||||||
}
|
}
|
||||||
} // namespace DInput
|
} // namespace DInput
|
||||||
} // namespace ciface
|
} // namespace ciface
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace ciface
|
||||||
{
|
{
|
||||||
namespace DInput
|
namespace DInput
|
||||||
{
|
{
|
||||||
void InitKeyboardMouse(IDirectInput8* const idi8);
|
void InitKeyboardMouse(IDirectInput8* const idi8, HWND hwnd);
|
||||||
|
|
||||||
class KeyboardMouse : public Core::Device
|
class KeyboardMouse : public Core::Device
|
||||||
{
|
{
|
||||||
|
@ -85,16 +85,21 @@ private:
|
||||||
public:
|
public:
|
||||||
void UpdateInput() override;
|
void UpdateInput() override;
|
||||||
|
|
||||||
KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device);
|
KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device,
|
||||||
|
HWND hwnd);
|
||||||
~KeyboardMouse();
|
~KeyboardMouse();
|
||||||
|
|
||||||
std::string GetName() const override;
|
std::string GetName() const override;
|
||||||
std::string GetSource() const override;
|
std::string GetSource() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void UpdateCursorInput();
|
||||||
|
|
||||||
const LPDIRECTINPUTDEVICE8 m_kb_device;
|
const LPDIRECTINPUTDEVICE8 m_kb_device;
|
||||||
const LPDIRECTINPUTDEVICE8 m_mo_device;
|
const LPDIRECTINPUTDEVICE8 m_mo_device;
|
||||||
|
|
||||||
|
const HWND m_hwnd;
|
||||||
|
|
||||||
DWORD m_last_update;
|
DWORD m_last_update;
|
||||||
State m_state_in;
|
State m_state_in;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue