From f8cdbe43358237e81ae15d83b8a4bdf5ec7f539c Mon Sep 17 00:00:00 2001 From: Nomango Date: Fri, 8 May 2020 00:57:28 +0800 Subject: [PATCH] fix bug: incorrect window size in fullscreen mode --- src/kiwano/platform/Window.h | 2 +- src/kiwano/platform/win32/WindowImpl.cpp | 36 ++++++++++++++-------- src/kiwano/render/DirectX/RendererImpl.cpp | 6 ++++ src/kiwano/render/DirectX/RendererImpl.h | 2 ++ src/kiwano/render/Renderer.cpp | 5 +++ src/kiwano/render/Renderer.h | 2 ++ 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/kiwano/platform/Window.h b/src/kiwano/platform/Window.h index ac1aa963..bd8a973f 100644 --- a/src/kiwano/platform/Window.h +++ b/src/kiwano/platform/Window.h @@ -133,7 +133,7 @@ public: * @brief 设置全屏模式 * @param fullscreen 是否全屏 */ - virtual void SetFullscreen(bool fullscreen) = 0; + virtual void SetFullscreenState(bool fullscreen) = 0; /** * \~chinese diff --git a/src/kiwano/platform/win32/WindowImpl.cpp b/src/kiwano/platform/win32/WindowImpl.cpp index 6a238585..818b6698 100644 --- a/src/kiwano/platform/win32/WindowImpl.cpp +++ b/src/kiwano/platform/win32/WindowImpl.cpp @@ -55,7 +55,7 @@ public: void Resize(uint32_t width, uint32_t height) override; - void SetFullscreen(bool fullscreen) override; + void SetFullscreenState(bool fullscreen) override; void SetCursor(CursorType cursor) override; @@ -279,7 +279,7 @@ void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, if (is_fullscreen_) { - SetFullscreen(true); + SetFullscreenState(true); } } @@ -316,19 +316,17 @@ void WindowWin32Impl::Resize(uint32_t width, uint32_t height) KGE_ASSERT(handle_); if (!is_fullscreen_) { - MONITORINFOEXA info = GetMoniterInfoEx(handle_); - - uint32_t screenw = uint32_t(info.rcWork.right - info.rcWork.left); - uint32_t screenh = uint32_t(info.rcWork.bottom - info.rcWork.top); - RECT rc = { 0, 0, LONG(width), LONG(height) }; ::AdjustWindowRect(&rc, GetStyle(), false); width = rc.right - rc.left; height = rc.bottom - rc.top; - int left = screenw > width ? ((screenw - width) / 2) : 0; - int top = screenh > height ? ((screenh - height) / 2) : 0; + MONITORINFOEXA info = GetMoniterInfoEx(handle_); + uint32_t screenw = uint32_t(info.rcWork.right - info.rcWork.left); + uint32_t screenh = uint32_t(info.rcWork.bottom - info.rcWork.top); + int left = screenw > width ? ((screenw - width) / 2) : 0; + int top = screenh > height ? ((screenh - height) / 2) : 0; ::SetWindowPos(handle_, 0, left, top, width, height, SWP_NOZORDER | SWP_NOACTIVATE); } @@ -339,12 +337,19 @@ void WindowWin32Impl::Resize(uint32_t width, uint32_t height) } } -void WindowWin32Impl::SetFullscreen(bool fullscreen) +void WindowWin32Impl::SetFullscreenState(bool fullscreen) { if (is_fullscreen_ != fullscreen) { is_fullscreen_ = fullscreen; + // Adjust the rect of client area + RECT rc = { 0, 0, LONG(width_), LONG(height_) }; + ::AdjustWindowRect(&rc, GetStyle(), false); + + uint32_t width = uint32_t(rc.right - rc.left); + uint32_t height = uint32_t(rc.bottom - rc.top); + if (is_fullscreen_) { // Reset window style @@ -352,17 +357,22 @@ void WindowWin32Impl::SetFullscreen(bool fullscreen) // Top the window MONITORINFOEXA info = GetMoniterInfoEx(handle_); - ::SetWindowPos(handle_, HWND_TOPMOST, info.rcMonitor.top, info.rcMonitor.left, width_, height_, + ::SetWindowPos(handle_, HWND_TOPMOST, info.rcMonitor.top, info.rcMonitor.left, width, height, SWP_NOACTIVATE); } else { + MONITORINFOEXA info = GetMoniterInfoEx(handle_); + uint32_t screenw = uint32_t(info.rcWork.right - info.rcWork.left); + uint32_t screenh = uint32_t(info.rcWork.bottom - info.rcWork.top); + int left = screenw > width ? ((screenw - width) / 2) : 0; + int top = screenh > height ? ((screenh - height) / 2) : 0; + // Reset window style ::SetWindowLongPtrA(handle_, GWL_STYLE, GetStyle()); // Unpin the window - ::SetWindowPos(handle_, HWND_NOTOPMOST, 0, 0, 0, 0, - SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE); + ::SetWindowPos(handle_, HWND_NOTOPMOST, left, top, width, height, SWP_DRAWFRAME | SWP_FRAMECHANGED); } ::ShowWindow(handle_, SW_SHOWNORMAL); diff --git a/src/kiwano/render/DirectX/RendererImpl.cpp b/src/kiwano/render/DirectX/RendererImpl.cpp index fe48773c..f0bced1d 100644 --- a/src/kiwano/render/DirectX/RendererImpl.cpp +++ b/src/kiwano/render/DirectX/RendererImpl.cpp @@ -120,6 +120,12 @@ void RendererImpl::MakeContextForWindow(WindowPtr window) KGE_THROW_IF_FAILED(hr, "Create render resources failed"); } +void RendererImpl::SetFullscreenState(bool fullscreen) +{ + KGE_ASSERT(d3d_res_); + d3d_res_->SetFullscreenState(fullscreen); +} + void RendererImpl::Destroy() { KGE_SYS_LOG("Destroying device resources"); diff --git a/src/kiwano/render/DirectX/RendererImpl.h b/src/kiwano/render/DirectX/RendererImpl.h index f2a1e13a..e90d35ac 100644 --- a/src/kiwano/render/DirectX/RendererImpl.h +++ b/src/kiwano/render/DirectX/RendererImpl.h @@ -104,6 +104,8 @@ protected: void MakeContextForWindow(WindowPtr window) override; + void SetFullscreenState(bool fullscreen) override; + void Destroy() override; private: diff --git a/src/kiwano/render/Renderer.cpp b/src/kiwano/render/Renderer.cpp index f25086ef..4c56e029 100644 --- a/src/kiwano/render/Renderer.cpp +++ b/src/kiwano/render/Renderer.cpp @@ -49,6 +49,11 @@ void Renderer::HandleEvent(Event* evt) auto window_evt = dynamic_cast(evt); Resize(window_evt->width, window_evt->height); } + else if (evt->IsType()) + { + auto window_evt = dynamic_cast(evt); + SetFullscreenState(window_evt->fullscreen); + } } void Renderer::BeginDraw() diff --git a/src/kiwano/render/Renderer.h b/src/kiwano/render/Renderer.h index ac03ca20..07bf9ff1 100644 --- a/src/kiwano/render/Renderer.h +++ b/src/kiwano/render/Renderer.h @@ -246,6 +246,8 @@ protected: virtual void MakeContextForWindow(WindowPtr window) = 0; + virtual void SetFullscreenState(bool fullscreen) = 0; + virtual void Destroy() = 0; protected: