Skip to content

Commit

Permalink
Fix window position being outside of the desktop when limiting window…
Browse files Browse the repository at this point in the history
… size.
  • Loading branch information
bolrog committed May 18, 2021
1 parent aee0454 commit b67babf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/d2dx/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ RenderContext::RenderContext(

_vbCapacity = 4 * 1024 * 1024;

AdjustWindowPlacement(hWnd, false);
SetSizes(_gameSize, _windowSize);

_resources = std::make_unique<RenderContextResources>(
_vbCapacity * sizeof(Vertex),
Expand Down Expand Up @@ -806,9 +806,11 @@ ITextureCache* RenderContext::GetTextureCache(

_Use_decl_annotations_
void RenderContext::AdjustWindowPlacement(
HWND hWnd,
bool centerOnCurrentPosition)
HWND hWnd)
{
bool centerOnCurrentPosition = _hasAdjustedWindowPlacement;
_hasAdjustedWindowPlacement = true;

const int32_t desktopCenterX = _desktopSize.width / 2;
const int32_t desktopCenterY = _screenMode == ScreenMode::FullscreenDefault ? _desktopSize.height / 2 : _desktopClientMaxHeight / 2;
const Offset preferredPosition = _d2dxContext->GetOptions().GetWindowPosition();
Expand Down Expand Up @@ -837,7 +839,7 @@ void RenderContext::AdjustWindowPlacement(
const int32_t newWindowCenterX = centerOnCurrentPosition ? oldWindowCenterX : desktopCenterX;
const int32_t newWindowCenterY = centerOnCurrentPosition ? oldWindowCenterY : desktopCenterY;
const int32_t newWindowX = usePreferredPosition ? preferredPosition.x : (newWindowCenterX - newWindowWidth / 2);
const int32_t newWindowY = usePreferredPosition ? preferredPosition.y : (newWindowCenterY - newWindowHeight / 2);
const int32_t newWindowY = max(0, usePreferredPosition ? preferredPosition.y : (newWindowCenterY - newWindowHeight / 2));

SetWindowLongPtr(hWnd, GWL_STYLE, windowStyle);
SetWindowPos_Real(hWnd, HWND_TOP, newWindowX, newWindowY, newWindowWidth, newWindowHeight, SWP_SHOWWINDOW | SWP_NOSENDCHANGING | SWP_FRAMECHANGED);
Expand Down Expand Up @@ -917,7 +919,7 @@ void RenderContext::SetSizes(
{
const float aspectRatio = (float)windowSize.width / windowSize.height;
windowSize.height = maxWindowSize.height;
windowSize.width = maxWindowSize.height * aspectRatio;
windowSize.width = (int32_t)(maxWindowSize.height * aspectRatio);
}

_gameSize = gameSize;
Expand All @@ -930,7 +932,7 @@ void RenderContext::SetSizes(
displaySize,
!_d2dxContext->GetOptions().GetFlag(OptionsFlag::NoWide));

AdjustWindowPlacement(_hWnd, true);
AdjustWindowPlacement(_hWnd);
}

bool RenderContext::IsFrameLatencyWaitableObjectSupported() const
Expand Down
4 changes: 2 additions & 2 deletions src/d2dx/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ namespace d2dx
_In_ AlphaBlend alphaBlend);

void AdjustWindowPlacement(
_In_ HWND hWnd,
_In_ bool resetPosition);
_In_ HWND hWnd);

uint32_t UpdateVerticesWithFullScreenTriangle(
_In_ Size srcSize,
Expand Down Expand Up @@ -210,6 +209,7 @@ namespace d2dx
DeviceContextState _shadowState;
EventHandle _frameLatencyWaitableObject;
int64_t _timeStart;
bool _hasAdjustedWindowPlacement = false;

double _prevTime;
double _frameTimeMs;
Expand Down

0 comments on commit b67babf

Please sign in to comment.