From 91a590ade629a54f1c721c987381c6a7149dc01f Mon Sep 17 00:00:00 2001
From: bartvbl <bartvblokl@gmail.com>
Date: Thu, 30 Jan 2025 10:19:07 +0100
Subject: [PATCH] Hopefully fix window closing issues on MacOS (and hopefully
 not break it on windows/linux)

---
 .../animationwindow/include/AnimationWindow.h |  2 ++
 .../animationwindow/src/AnimationWindow.cpp   | 26 ++++++++++++++-----
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/dependencies/subprojects/animationwindow/include/AnimationWindow.h b/dependencies/subprojects/animationwindow/include/AnimationWindow.h
index c9211b7..b63887f 100644
--- a/dependencies/subprojects/animationwindow/include/AnimationWindow.h
+++ b/dependencies/subprojects/animationwindow/include/AnimationWindow.h
@@ -41,10 +41,12 @@ class AnimationWindow {
    private:
     void show_frame();
     void update_gui();
+    void pump_events();
     TDT4102::Point getWindowDimensions() const;
     void startNuklearDraw(TDT4102::Point location, std::string uniqueWindowName, unsigned int width = 0, unsigned int height = 0);
     void endNuklearDraw();
     void destroy();
+    bool destroyed = false;
 
     // If set to true, new shapes will be drawn on top of the old ones. Can create some neat effects.
     // However, note that GUI elements such as buttons will not draw themselves correctly if you use this.
diff --git a/dependencies/subprojects/animationwindow/src/AnimationWindow.cpp b/dependencies/subprojects/animationwindow/src/AnimationWindow.cpp
index 15385aa..14c872c 100644
--- a/dependencies/subprojects/animationwindow/src/AnimationWindow.cpp
+++ b/dependencies/subprojects/animationwindow/src/AnimationWindow.cpp
@@ -60,6 +60,8 @@ TDT4102::AnimationWindow::~AnimationWindow() {
 }
 
 void TDT4102::AnimationWindow::destroy() {
+    this->destroyed = true;
+
     // Free SDL resources depending on how much ended up being initialised in the constructor
     if (rendererHandle != nullptr) {
         SDL_DestroyRenderer(rendererHandle);
@@ -73,14 +75,16 @@ void TDT4102::AnimationWindow::destroy() {
         nk_free(context);
         context = nullptr;
     }
+    // Needed for MacOS
+    // Window does not close unless the events are pumped
+    // after requesting that
+    pump_events();
 }
 
-void TDT4102::AnimationWindow::show_frame() {
-    SDL_RenderPresent(rendererHandle);
-    deltaMouseWheel = 0;
-
+void TDT4102::AnimationWindow::pump_events() {
     SDL_Event event;
-    nk_input_begin(context);
+    SDL_PumpEvents();
+ 
     while (SDL_PollEvent(&event)) {
         if (event.type == SDL_QUIT) {
             closeRequested = true;
@@ -107,8 +111,18 @@ void TDT4102::AnimationWindow::show_frame() {
             deltaMouseWheel = event.wheel.preciseY; // The amount scrolled vertically, positive away from the user and negative toward the user
         }
 
-        nk_sdl_handle_event(&event);
+        if(!destroyed) {
+            nk_sdl_handle_event(&event);
+        }
+        
     }
+}
+
+void TDT4102::AnimationWindow::show_frame() {
+    SDL_RenderPresent(rendererHandle);
+    deltaMouseWheel = 0;
+    nk_input_begin(context);
+    pump_events();
     nk_input_end(context);
 }
 
-- 
GitLab