From d253ebb19746d7950596f289a83d5600fa2d70a3 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sun, 20 Apr 2025 14:58:39 -0500 Subject: [PATCH] aquire and release --- include/utils/App/App.h | 12 +++++--- include/utils/Window/Window.h | 6 ++-- modules/ImguiModule/src/ImguiModule.cpp | 8 +++-- modules/ImguiModule/src/ImguiModule.h | 19 +++++++++++- modules/MainGUI/src/MainGUI.cpp | 7 +++-- modules/WindowModule/src/WindowModule.cpp | 37 ++++------------------- modules/WindowModule/src/WindowModule.h | 34 +++++++++++++++++++-- 7 files changed, 78 insertions(+), 45 deletions(-) diff --git a/include/utils/App/App.h b/include/utils/App/App.h index f061c1a..0bc6e23 100644 --- a/include/utils/App/App.h +++ b/include/utils/App/App.h @@ -95,14 +95,19 @@ namespace Archimedes { std::list> toOpen; void handleEvents() { - bool handled = false; + static bool handled; while(!events.empty()) { + + handled = false; + for(auto it = runOrder.rbegin(); it != runOrder.rend(); it++) { - if(modules[*it]->onEvent(*events.front())) { + + handled = modules[*it]->onEvent(*events.front()); + + if(handled) { Event* e = events.front(); events.pop_front(); delete e; - handled = true; break; } } @@ -110,7 +115,6 @@ namespace Archimedes { if(!handled) { std::cout << "Error: Unhandled Event: " << (std::string) *events.front() << std::endl; } - handled = false; } } diff --git a/include/utils/Window/Window.h b/include/utils/Window/Window.h index 3a284ad..6f52ff8 100644 --- a/include/utils/Window/Window.h +++ b/include/utils/Window/Window.h @@ -18,12 +18,14 @@ namespace Archimedes { bool shouldClose() { return window.shouldClose(); } + void getSize(int& w, int& h) { + window.getSize(w, h); + } + void doFrame() { window.pollEvents(); - window.getSize(renderer->w, renderer->h); - renderer->render(); window.doFrame(); diff --git a/modules/ImguiModule/src/ImguiModule.cpp b/modules/ImguiModule/src/ImguiModule.cpp index 407c526..fa6fcb8 100644 --- a/modules/ImguiModule/src/ImguiModule.cpp +++ b/modules/ImguiModule/src/ImguiModule.cpp @@ -1,7 +1,5 @@ #include "ImguiModule.h" -#include "modules/WindowModule/src/WindowModule.h" - #include "backends/imgui_impl_glfw.h" #include "backends/imgui_impl_opengl3.h" @@ -27,6 +25,8 @@ ImguiModule::~ImguiModule() { ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); + + wm->releaseWindow(window); } } @@ -38,6 +38,8 @@ void ImguiModule::onLoad() { std::cout << "No WindowModule for ImguiModule!\n"; std::abort(); } + + window = wm->aquireWindow(); IMGUI_CHECKVERSION(); context = ImGui::CreateContext(); @@ -56,7 +58,7 @@ void ImguiModule::onLoad() { //ImGui::StyleColorsLight(); // Setup Platform/Renderer backends - if(!ImGui_ImplGlfw_InitForOpenGL(wm->getWindow()->getWindowImpl().getWindow(), true)) + if(!ImGui_ImplGlfw_InitForOpenGL(window->getWindowImpl().getWindow(), true)) std::cout << "GLFWImpl failed\n"; if(!ImGui_ImplOpenGL3_Init("#version 330")) { std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl; diff --git a/modules/ImguiModule/src/ImguiModule.h b/modules/ImguiModule/src/ImguiModule.h index ae262f8..5ff2882 100644 --- a/modules/ImguiModule/src/ImguiModule.h +++ b/modules/ImguiModule/src/ImguiModule.h @@ -7,6 +7,8 @@ #include "imgui.h" #include "misc/cpp/imgui_stdlib.h" +#include "modules/WindowModule/src/WindowModule.h" + class ImguiModule : public Archimedes::Module { public: @@ -20,12 +22,27 @@ class ImguiModule : public Archimedes::Module { bool onEvent(const Archimedes::Event&) override; - ImGuiContext* getContext() { return context; } + ImGuiContext* aquireContext() { + contextRefs++; + return context; + } + + void releaseContext(ImGuiContext* ctxt) { + if(ctxt == context && context != nullptr) { + if(--contextRefs == 0) { + app->stopModule(name); + } + } + } private: + + int contextRefs = 0; ImGuiContext* context; + Archimedes::Window* window; + std::list>::iterator rcmd_it; }; diff --git a/modules/MainGUI/src/MainGUI.cpp b/modules/MainGUI/src/MainGUI.cpp index df90d31..c15b535 100644 --- a/modules/MainGUI/src/MainGUI.cpp +++ b/modules/MainGUI/src/MainGUI.cpp @@ -12,7 +12,10 @@ MainGUI::MainGUI(Archimedes::App* a, void* h) : Archimedes::Module(a, h) { MainGUI::~MainGUI() { - if(app) {} + if(app) { + ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; } + im->releaseContext(ImGui::GetCurrentContext()); + } } void MainGUI::onLoad() { @@ -24,7 +27,7 @@ void MainGUI::onLoad() { std::abort(); } - ImGui::SetCurrentContext(im->getContext()); + ImGui::SetCurrentContext(im->aquireContext()); } diff --git a/modules/WindowModule/src/WindowModule.cpp b/modules/WindowModule/src/WindowModule.cpp index 04a9baa..d33d224 100644 --- a/modules/WindowModule/src/WindowModule.cpp +++ b/modules/WindowModule/src/WindowModule.cpp @@ -18,10 +18,9 @@ WindowModule::~WindowModule() { renderer->getCmdList().clear(); delete renderer; } - for(auto* w : windows) { - if(w) - delete w; - } + + if(window) + delete window; } } @@ -39,37 +38,13 @@ void WindowModule::onLoad() { app->registerEvent(Archimedes::WindowFocusLostEvent()); app->registerEvent(Archimedes::WindowMovedEvent()); - windows.push_back(new Archimedes::Window([this](Archimedes::Event* e){ - app->emitEvent(e); - })); - renderer = new Archimedes::Renderer(); - - for(auto* window : windows) - window->setRenderer(renderer); - - if(!renderer->init()) { - std::cout << "Renderer init failed!\n"; - std::abort(); - } } void WindowModule::run() { - for(auto* window : windows) { - - if(window->shouldClose()) { - app->end(); - } - - for(auto e : window->getWindowImpl().data.eventList) { - app->emitEvent(e); - } - - window->getWindowImpl().data.eventList.clear(); - + if(window) window->doFrame(); - } } bool WindowModule::onEvent(const Archimedes::Event& e) { @@ -77,10 +52,10 @@ bool WindowModule::onEvent(const Archimedes::Event& e) { unsigned int type = app->getEventType(e); if(type == app->getEventType(Archimedes::WindowResizeEvent())) { - + window->getSize(renderer->w, renderer->h); return true; } else if(type == app->getEventType(Archimedes::WindowCloseEvent())) { - + app->stopModule(name); return true; } else if(type == app->getEventType(Archimedes::WindowKeyPressedEvent())) { diff --git a/modules/WindowModule/src/WindowModule.h b/modules/WindowModule/src/WindowModule.h index 36b2c99..2efb6a3 100644 --- a/modules/WindowModule/src/WindowModule.h +++ b/modules/WindowModule/src/WindowModule.h @@ -25,12 +25,42 @@ class WindowModule : public Archimedes::Module { //interface for other modules - std::vector& getWindow() { return windows; } + Archimedes::Window* aquireWindow() { + if(!window) { + + window = new Archimedes::Window([this](Archimedes::Event* e){ + app->emitEvent(e); + }); + + window->setRenderer(renderer); + + + if(!renderer->init()) { + std::cout << "Renderer init failed!\n"; + std::abort(); + } + } + + windowRefs++; + return window; + } + + void releaseWindow(Archimedes::Window* w) { + if(w == window && window != nullptr) { + if(--windowRefs == 0) { + delete window; + window = nullptr; + } + } + } + Archimedes::Renderer* getRenderer() { return renderer; } private: - std::vector windows; + int windowRefs = 0; + + Archimedes::Window* window; Archimedes::Renderer* renderer; };