From 9fabb7d672b95c75997fbb77d727c6dc39bf869a Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Sun, 2 Oct 2022 01:13:04 +0800 Subject: [PATCH] Window can close properly (with the exception of scene graph dtor crash) --- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 13 +++++++++---- .../Graphics/MiddleEnd/PerFrame/SHRenderContext.cpp | 10 ++++++++++ .../Graphics/MiddleEnd/PerFrame/SHRenderContext.h | 7 +++++-- SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp | 3 ++- SHADE_Engine/src/Graphics/Windowing/SHWindow.h | 2 +- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index fc02b994..8aec5a83 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -73,6 +73,11 @@ namespace SHADE renderContext.SetIsResized(true); }); + window->RegisterWindowCloseCallback([&](void) + { + renderContext.SetWindowIsDead(true); + } + ); // Create graphics queue graphicsQueue = device->GetQueue(SH_Q_FAM::GRAPHICS, 0); transferQueue = device->GetQueue(SH_Q_FAM::TRANSFER, 0); @@ -213,7 +218,7 @@ namespace SHADE /***************************************************************************/ void SHGraphicsSystem::Run(double) noexcept { - if (window->IsMinimized()) + if (window->IsMinimized() || renderContext.GetWindowIsDead()) return; if (renderContext.GetResized()) @@ -344,7 +349,7 @@ namespace SHADE /***************************************************************************/ void SHGraphicsSystem::BeginRender() { - if (window->IsMinimized()) + if (window->IsMinimized() || renderContext.GetWindowIsDead()) return; // Finalise all batches @@ -393,7 +398,7 @@ namespace SHADE /***************************************************************************/ void SHGraphicsSystem::EndRender() { - if (window->IsMinimized()) + if (window->IsMinimized() || renderContext.GetWindowIsDead()) return; if (renderContext.GetResized()) @@ -535,7 +540,7 @@ namespace SHADE void SHGraphicsSystem::HandleResize(void) noexcept { - if (window->IsMinimized()) + if (window->IsMinimized() || renderContext.GetWindowIsDead()) return; auto windowDims = window->GetWindowSize(); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.cpp index 4953447b..0ac7013a 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.cpp @@ -168,6 +168,11 @@ namespace SHADE isResized = resized; } + void SHRenderContext::SetWindowIsDead(bool dead) noexcept + { + windowIsDead = dead; + } + /***************************************************************************/ /*! @@ -209,4 +214,9 @@ namespace SHADE return b; } + bool SHRenderContext::GetWindowIsDead(void) const noexcept + { + return windowIsDead; + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h b/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h index afdbef2d..bf186922 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/PerFrame/SHRenderContext.h @@ -36,6 +36,7 @@ namespace SHADE uint32_t currentFrame; bool isResized{ false }; + bool windowIsDead {false}; public: SHRenderContext(void) noexcept; @@ -51,13 +52,15 @@ namespace SHADE bool WaitForFence (void) noexcept; void ResetFence (void) noexcept; - void SetIsResized (bool resized) noexcept; + void SetIsResized (bool resized) noexcept; + void SetWindowIsDead (bool dead) noexcept; SHPerFrameData& GetCurrentFrameData(void) noexcept; SHPerFrameData& GetFrameData (uint32_t index) noexcept; uint32_t GetCurrentFrame (void) const noexcept; - bool GetResized(void) noexcept; + bool GetResized (void) noexcept; bool GetResizeAndReset (void) noexcept; + bool GetWindowIsDead (void) const noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp b/SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp index 1688d348..f4ae3d7e 100644 --- a/SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp +++ b/SHADE_Engine/src/Graphics/Windowing/SHWindow.cpp @@ -272,7 +272,7 @@ namespace SHADE windowResizeCallbacks.erase(callbackid); } - SHWindow::CALLBACKID SHWindow::RegisterWindowCloseCallback(WindowResizeCallbackFn windowCloseCallback) + SHWindow::CALLBACKID SHWindow::RegisterWindowCloseCallback(WindowCloseCallbackFn windowCloseCallback) { windowCloseCallbacks.try_emplace(windowResizeCallbackCount, windowCloseCallback); return windowCloseCallbackCount++; @@ -388,6 +388,7 @@ namespace SHADE void SHWindow::OnDestroy() { + OnClose(); this->Destroy(); } diff --git a/SHADE_Engine/src/Graphics/Windowing/SHWindow.h b/SHADE_Engine/src/Graphics/Windowing/SHWindow.h index b1c1067d..0a180285 100644 --- a/SHADE_Engine/src/Graphics/Windowing/SHWindow.h +++ b/SHADE_Engine/src/Graphics/Windowing/SHWindow.h @@ -126,7 +126,7 @@ namespace SHADE CALLBACKID RegisterWindowSizeCallback(WindowResizeCallbackFn); void UnregisterWindowSizeCallback(CALLBACKID const& callbackid); - CALLBACKID RegisterWindowCloseCallback(WindowResizeCallbackFn); + CALLBACKID RegisterWindowCloseCallback(WindowCloseCallbackFn); void UnregisterWindowCloseCallback(CALLBACKID const& callbackid); bool IsMinimized() const { return wndData.isMinimised; }