diff --git a/lib/win/ApplicationWin32.cpp b/lib/win/ApplicationWin32.cpp index 3e44e11..5aa5e90 100644 --- a/lib/win/ApplicationWin32.cpp +++ b/lib/win/ApplicationWin32.cpp @@ -69,8 +69,8 @@ namespace boo static logvisor::Module Log("boo::ApplicationWin32"); Win32Cursors WIN32_CURSORS; -IWindow* _WindowWin32New(const SystemString& title, Boo3DAppContext& d3dCtx, - void* vulkanHandle, uint32_t sampleCount); +std::shared_ptr _WindowWin32New(const SystemString& title, Boo3DAppContext& d3dCtx, + void* vulkanHandle, uint32_t sampleCount); class ApplicationWin32 final : public IApplication { @@ -79,7 +79,7 @@ class ApplicationWin32 final : public IApplication const SystemString m_friendlyName; const SystemString m_pname; const std::vector m_args; - std::unordered_map m_allWindows; + std::unordered_map> m_allWindows; bool m_singleInstance; Boo3DAppContext m_3dCtx; @@ -314,7 +314,14 @@ public: LRESULT winHwndHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { /* Lookup boo window instance */ - IWindow* window = m_allWindows[hwnd]; + auto search = m_allWindows.find(hwnd); + if (search == m_allWindows.end()) + return 0; + + std::shared_ptr window = search->second.lock(); + if (!window) + return 0; + switch (uMsg) { case WM_CREATE: @@ -347,7 +354,10 @@ public: case WM_MOUSEHWHEEL: case WM_CHAR: case WM_UNICHAR: - window->_incomingEvent(&HWNDEvent(uMsg, wParam, lParam)); + { + HWNDEvent eventData(uMsg, wParam, lParam); + window->_incomingEvent(&eventData); + } default: return DefWindowProc(hwnd, uMsg, wParam, lParam); @@ -437,8 +447,8 @@ public: std::mutex m_nwmt; std::condition_variable m_nwcv; - IWindow* m_mwret = nullptr; - IWindow* newWindow(const SystemString& title, uint32_t sampleCount) + std::shared_ptr m_mwret; + std::shared_ptr newWindow(const SystemString& title, uint32_t sampleCount) { if (GetCurrentThreadId() != g_mainThreadId) { @@ -446,13 +456,15 @@ public: if (!PostThreadMessage(g_mainThreadId, WM_USER, WPARAM(&title), 0)) Log.report(logvisor::Fatal, "PostThreadMessage error"); m_nwcv.wait(lk); - return m_mwret; + std::shared_ptr ret = std::move(m_mwret); + m_mwret.reset(); + return ret; } #if BOO_HAS_VULKAN - IWindow* window = _WindowWin32New(title, m_3dCtx, m_getVkProc, sampleCount); + std::shared_ptr window = _WindowWin32New(title, m_3dCtx, m_getVkProc, sampleCount); #else - IWindow* window = _WindowWin32New(title, m_3dCtx, nullptr, sampleCount); + std::shared_ptr window = _WindowWin32New(title, m_3dCtx, nullptr, sampleCount); #endif HWND hwnd = HWND(window->getPlatformHandle()); diff --git a/lib/win/WindowWin32.cpp b/lib/win/WindowWin32.cpp index c74b06b..50c5178 100644 --- a/lib/win/WindowWin32.cpp +++ b/lib/win/WindowWin32.cpp @@ -1014,6 +1014,12 @@ public: m_callback = cb; } + void closeWindow() + { + // TODO: Perform thread-coalesced deallocation + ShowWindow(m_hwnd, SW_HIDE); + } + void showWindow() { ShowWindow(m_hwnd, SW_SHOW); @@ -1579,10 +1585,10 @@ public: }; -IWindow* _WindowWin32New(const SystemString& title, Boo3DAppContext& d3dCtx, - void* vulkanHandle, uint32_t sampleCount) +std::shared_ptr _WindowWin32New(const SystemString& title, Boo3DAppContext& d3dCtx, + void* vulkanHandle, uint32_t sampleCount) { - return new WindowWin32(title, d3dCtx, vulkanHandle, sampleCount); + return std::make_shared(title, d3dCtx, vulkanHandle, sampleCount); } }