Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cursor locking behaviour #192

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions src/d2dx/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ RenderContext::RenderContext(
GetClientRect(hWnd, &clientRect);

SetWindowSubclass((HWND)hWnd, d2dxSubclassWndProc, 1234, (DWORD_PTR)this);
_isActiveWindow = GetForegroundWindow() == hWnd;

const int32_t widthFromClientRect = clientRect.right - clientRect.left;
const int32_t heightFromClientRect = clientRect.bottom - clientRect.top;
Expand Down Expand Up @@ -400,7 +401,7 @@ void RenderContext::Present()
UpdateViewport(_renderRect);

RenderContextPixelShader pixelShader;

switch (_d2dxContext->GetOptions().GetFiltering())
{
default:
Expand Down Expand Up @@ -685,17 +686,20 @@ static LRESULT CALLBACK d2dxSubclassWndProc(
{
RenderContext* renderContext = (RenderContext*)dwRefData;

if (uMsg == WM_ACTIVATEAPP)
if (uMsg == WM_ACTIVATE)
{
if (wParam)
{
renderContext->ClipCursor();
renderContext->SetActiveWindow(true);
}
else
{
renderContext->UnclipCursor();
renderContext->SetActiveWindow(false);
}
}
else if (uMsg == WM_MOVING) {
renderContext->UnclipCursor();
}
else if (uMsg == WM_SYSKEYDOWN || uMsg == WM_KEYDOWN)
{
if (wParam == VK_RETURN && (HIWORD(lParam) & KF_ALTDOWN))
Expand All @@ -719,6 +723,11 @@ static LRESULT CALLBACK d2dxSubclassWndProc(
#ifdef NDEBUG
ShowCursor_Real(FALSE);
#endif
if (uMsg != WM_MOUSEMOVE)
{
renderContext->ClipCursor(false);
}

Offset mousePos = { LOWORD(lParam), HIWORD(lParam) };

Size gameSize;
Expand Down Expand Up @@ -921,7 +930,7 @@ void RenderContext::SetSizes(
SetWindowPos_Real(_hWnd, HWND_TOP, 0, 0, _desktopSize.width, _desktopSize.height, SWP_SHOWWINDOW | SWP_NOSENDCHANGING | SWP_FRAMECHANGED);
}

ClipCursor();
ClipCursor(true);

if (!_d2dxContext->GetOptions().GetFlag(OptionsFlag::NoTitleChange))
{
Expand Down Expand Up @@ -1019,23 +1028,29 @@ void RenderContext::GetCurrentMetrics(
}
}

void RenderContext::ClipCursor()
void RenderContext::ClipCursor(bool resizing)
{
if (_d2dxContext->GetOptions().GetFlag(OptionsFlag::NoClipCursor))
if (!_isActiveWindow || (resizing != _isCursorClipped) || _d2dxContext->GetOptions().GetFlag(OptionsFlag::NoClipCursor))
{
return;
}

RECT clipRect;
::GetClientRect(_hWnd, &clipRect);
RECT clipRect = {
_renderRect.offset.x,
_renderRect.offset.y,
_renderRect.offset.x + _renderRect.size.width,
_renderRect.offset.y + _renderRect.size.height
};
::ClientToScreen(_hWnd, (LPPOINT)&clipRect.left);
::ClientToScreen(_hWnd, (LPPOINT)&clipRect.right);
::ClipCursor(&clipRect);
::ClientToScreen(_hWnd, (LPPOINT)&clipRect.right);
::ClipCursor(&clipRect);
_isCursorClipped = true;
}

void RenderContext::UnclipCursor()
{
::ClipCursor(NULL);
_isCursorClipped = false;
}

float RenderContext::GetFrameTime() const
Expand Down
12 changes: 11 additions & 1 deletion src/d2dx/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ namespace d2dx

virtual ScreenMode GetScreenMode() const override;

void ClipCursor();
void SetActiveWindow(bool active) {
if (!active)
{
UnclipCursor();
}
_isActiveWindow = active;
}

void ClipCursor(bool resizing);
void UnclipCursor();

private:
Expand Down Expand Up @@ -210,6 +218,8 @@ namespace d2dx
EventHandle _frameLatencyWaitableObject;
int64_t _timeStart;
bool _hasAdjustedWindowPlacement = false;
bool _isActiveWindow = false;
bool _isCursorClipped = false;

double _prevTime;
double _frameTimeMs;
Expand Down