fix bug: incorrect window size in fullscreen mode

This commit is contained in:
Nomango 2020-05-08 00:57:28 +08:00
parent ae247dd676
commit f8cdbe4335
6 changed files with 39 additions and 14 deletions

View File

@ -133,7 +133,7 @@ public:
* @brief * @brief
* @param fullscreen * @param fullscreen
*/ */
virtual void SetFullscreen(bool fullscreen) = 0; virtual void SetFullscreenState(bool fullscreen) = 0;
/** /**
* \~chinese * \~chinese

View File

@ -55,7 +55,7 @@ public:
void Resize(uint32_t width, uint32_t height) override; void Resize(uint32_t width, uint32_t height) override;
void SetFullscreen(bool fullscreen) override; void SetFullscreenState(bool fullscreen) override;
void SetCursor(CursorType cursor) 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_) if (is_fullscreen_)
{ {
SetFullscreen(true); SetFullscreenState(true);
} }
} }
@ -316,17 +316,15 @@ void WindowWin32Impl::Resize(uint32_t width, uint32_t height)
KGE_ASSERT(handle_); KGE_ASSERT(handle_);
if (!is_fullscreen_) 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) }; RECT rc = { 0, 0, LONG(width), LONG(height) };
::AdjustWindowRect(&rc, GetStyle(), false); ::AdjustWindowRect(&rc, GetStyle(), false);
width = rc.right - rc.left; width = rc.right - rc.left;
height = rc.bottom - rc.top; height = rc.bottom - rc.top;
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 left = screenw > width ? ((screenw - width) / 2) : 0;
int top = screenh > height ? ((screenh - height) / 2) : 0; int top = screenh > height ? ((screenh - height) / 2) : 0;
@ -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) if (is_fullscreen_ != fullscreen)
{ {
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_) if (is_fullscreen_)
{ {
// Reset window style // Reset window style
@ -352,17 +357,22 @@ void WindowWin32Impl::SetFullscreen(bool fullscreen)
// Top the window // Top the window
MONITORINFOEXA info = GetMoniterInfoEx(handle_); 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); SWP_NOACTIVATE);
} }
else 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 // Reset window style
::SetWindowLongPtrA(handle_, GWL_STYLE, GetStyle()); ::SetWindowLongPtrA(handle_, GWL_STYLE, GetStyle());
// Unpin the window // Unpin the window
::SetWindowPos(handle_, HWND_NOTOPMOST, 0, 0, 0, 0, ::SetWindowPos(handle_, HWND_NOTOPMOST, left, top, width, height, SWP_DRAWFRAME | SWP_FRAMECHANGED);
SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
} }
::ShowWindow(handle_, SW_SHOWNORMAL); ::ShowWindow(handle_, SW_SHOWNORMAL);

View File

@ -120,6 +120,12 @@ void RendererImpl::MakeContextForWindow(WindowPtr window)
KGE_THROW_IF_FAILED(hr, "Create render resources failed"); 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() void RendererImpl::Destroy()
{ {
KGE_SYS_LOG("Destroying device resources"); KGE_SYS_LOG("Destroying device resources");

View File

@ -104,6 +104,8 @@ protected:
void MakeContextForWindow(WindowPtr window) override; void MakeContextForWindow(WindowPtr window) override;
void SetFullscreenState(bool fullscreen) override;
void Destroy() override; void Destroy() override;
private: private:

View File

@ -49,6 +49,11 @@ void Renderer::HandleEvent(Event* evt)
auto window_evt = dynamic_cast<WindowResizedEvent*>(evt); auto window_evt = dynamic_cast<WindowResizedEvent*>(evt);
Resize(window_evt->width, window_evt->height); Resize(window_evt->width, window_evt->height);
} }
else if (evt->IsType<WindowFullscreenEvent>())
{
auto window_evt = dynamic_cast<WindowFullscreenEvent*>(evt);
SetFullscreenState(window_evt->fullscreen);
}
} }
void Renderer::BeginDraw() void Renderer::BeginDraw()

View File

@ -246,6 +246,8 @@ protected:
virtual void MakeContextForWindow(WindowPtr window) = 0; virtual void MakeContextForWindow(WindowPtr window) = 0;
virtual void SetFullscreenState(bool fullscreen) = 0;
virtual void Destroy() = 0; virtual void Destroy() = 0;
protected: protected: