From 771363bbfe505bba06649da7924517a94fa69a73 Mon Sep 17 00:00:00 2001 From: Jayden Grubb Date: Wed, 30 Apr 2025 02:52:29 +1000 Subject: [PATCH] Fix Window CLOSED event when using more than one window Fixed an issue where if there were more than one window, when they were closed, the CLOSED event would trigger but the window count did not decrease. This happened because after the window was destroyed the DestroyNotify event was triggered, which when looked up in the m_windows map, reinserted itself with default initialization. --- engine/src/drivers/x11/window_driver.cpp | 27 ++++++++++++++---------- engine/src/drivers/x11/window_driver.h | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/engine/src/drivers/x11/window_driver.cpp b/engine/src/drivers/x11/window_driver.cpp index 917e47a..e14b15d 100644 --- a/engine/src/drivers/x11/window_driver.cpp +++ b/engine/src/drivers/x11/window_driver.cpp @@ -59,15 +59,19 @@ void X11WindowDriver::poll_events() { XEvent event; XNextEvent(m_display, &event); - X11::Window handle = event.xany.window; - Nova::Window& window = m_windows[handle]; + auto iter = m_windows.find(event.xany.window); + if (iter == m_windows.end()) { + continue; + } + + WindowID window = iter->second; switch (event.type) { case ConfigureNotify: { XConfigureEvent xce = event.xconfigure; - if (xce.width != window.width || xce.height != window.height) { - window.width = xce.width; - window.height = xce.height; + if (xce.width != window->width || xce.height != window->height) { + window->width = xce.width; + window->height = xce.height; NOVA_DEBUG("Window event: RESIZED ({}x{})", xce.width, xce.height); } break; @@ -75,7 +79,7 @@ void X11WindowDriver::poll_events() { case ClientMessage: { if (event.xclient.data.l[0] == static_cast(m_window_close_atom)) { NOVA_DEBUG("Window event: CLOSED"); - destroy_window(&window); + destroy_window(window); } break; } @@ -105,10 +109,10 @@ WindowID X11WindowDriver::create_window(const std::string_view p_title, const u3 X11::Window handle = XCreateSimpleWindow(m_display, DefaultRootWindow(m_display), 0, 0, p_width, p_height, 0, 0, 0); - Nova::Window& window = m_windows[handle]; - window.width = p_width; - window.height = p_height; - window.handle = handle; + Window* window = new Window(); + window->width = p_width; + window->height = p_height; + window->handle = handle; XSetWMProtocols(m_display, handle, &m_window_close_atom, 1); XSelectInput(m_display, handle, StructureNotifyMask); @@ -116,7 +120,8 @@ WindowID X11WindowDriver::create_window(const std::string_view p_title, const u3 XMapWindow(m_display, handle); XFlush(m_display); - return &window; + m_windows[handle] = window; + return window; } void X11WindowDriver::destroy_window(WindowID p_window) { diff --git a/engine/src/drivers/x11/window_driver.h b/engine/src/drivers/x11/window_driver.h index df4c886..8e10fda 100644 --- a/engine/src/drivers/x11/window_driver.h +++ b/engine/src/drivers/x11/window_driver.h @@ -51,7 +51,7 @@ namespace Nova { private: X11::Display* m_display = nullptr; X11::Atom m_window_close_atom = 0; - std::unordered_map m_windows; + std::unordered_map m_windows; }; } // namespace Nova