From 42b5a53b894d7eb5acda3163b4272fe16526df29 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 9 Apr 2025 10:52:23 -0500 Subject: [PATCH] work on layers --- include/utils/App/App.h | 8 ++ include/utils/Layers/Layerstack.h | 8 +- .../Renderer/RendererOpenGL/RendererOpenGL.h | 4 +- modules/ImguiModule/src/ImguiModule.cpp | 96 ++++++++++--------- modules/ImguiModule/src/ImguiModule.h | 24 ++++- modules/WindowModule/src/WindowModule.cpp | 13 +-- .../GuiModules/TestImgui/src/TestImgui.cpp | 11 +-- .../GuiModules/TestImgui/src/TestImgui.h | 1 - 8 files changed, 102 insertions(+), 63 deletions(-) diff --git a/include/utils/App/App.h b/include/utils/App/App.h index 265d45b..2bddc7b 100644 --- a/include/utils/App/App.h +++ b/include/utils/App/App.h @@ -143,6 +143,14 @@ namespace Archimedes { } virtual void unload(std::string name) { + + //unload modules that depend on the one we are unloading + for(std::string s : runOrder) { + if(modules[s]->deps.find(name) != modules[s]->deps.end()) { + unload(s); + } + } + Module* m = modules[name]; void* h = m->getHandle(); diff --git a/include/utils/Layers/Layerstack.h b/include/utils/Layers/Layerstack.h index 365f350..e95504a 100644 --- a/include/utils/Layers/Layerstack.h +++ b/include/utils/Layers/Layerstack.h @@ -16,9 +16,11 @@ namespace Archimedes { } } - void push(Layer* l) { - lstack.push_front(l); - l->onAttach(); + void push(Layer* l) { + if(l) { + lstack.push_front(l); + l->onAttach(); + } } void pop() { diff --git a/include/utils/Renderer/RendererOpenGL/RendererOpenGL.h b/include/utils/Renderer/RendererOpenGL/RendererOpenGL.h index d8ac059..e506104 100644 --- a/include/utils/Renderer/RendererOpenGL/RendererOpenGL.h +++ b/include/utils/Renderer/RendererOpenGL/RendererOpenGL.h @@ -21,13 +21,13 @@ namespace Archimedes { return glewInit() == GLEW_OK; }; - void render(std::list cmdList, int& w, int& h) { + void render(std::list> cmdList, int& w, int& h) { glViewport(0, 0, w, h); glClear(GL_COLOR_BUFFER_BIT); - for(auto* f : cmdList) + for(auto f : cmdList) f(); } }; diff --git a/modules/ImguiModule/src/ImguiModule.cpp b/modules/ImguiModule/src/ImguiModule.cpp index 63846c0..7b185dd 100644 --- a/modules/ImguiModule/src/ImguiModule.cpp +++ b/modules/ImguiModule/src/ImguiModule.cpp @@ -13,17 +13,43 @@ ImguiModule::ImguiModule(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, ImguiModule::~ImguiModule() { - std::list* cmdList = - std::any_cast*>(depsInstances["WindowModule"]->getData("renderCmdList")); - cmdList->erase(rcmd); - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplGlfw_Shutdown(); - ImGui::DestroyContext(); } void ImguiModule::onLoad() { + WindowModule* wm = (WindowModule*) depsInstances["WindowModule"]; - Archimedes::Module* wm = depsInstances["WindowModule"]; + if(!wm) { + std::cout << "No WindowModule for ImguiModule!\n"; + std::abort(); + } + + wm->getLayerstack()->push(&layer); +} + +void ImguiModule::run() { + ImGuiIO& io = ImGui::GetIO(); + static float f = 0.0f; + static int counter = 0; + + ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. + + ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) + + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f + + if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) + counter++; + ImGui::SameLine(); + ImGui::Text("counter = %d", counter); + + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); + ImGui::End(); +} + +ImguiModule::IGLayer::~IGLayer() {} + +void ImguiModule::IGLayer::onAttach() { + WindowModule* wm = (WindowModule*) imgui->depsInstances["WindowModule"]; if(!wm) { std::cout << "No WindowModule for ImguiModule!\n"; @@ -44,57 +70,41 @@ void ImguiModule::onLoad() { //ImGui::StyleColorsLight(); // Setup Platform/Renderer backends - GLFWwindow* w = std::any_cast(wm->getData("window")); - - if(!ImGui_ImplGlfw_InitForOpenGL(w, true)) + if(!ImGui_ImplGlfw_InitForOpenGL(wm->getWindow()->getWindowImpl().getWindow(), true)) std::cout << "GLFWImpl failed\n"; if(!ImGui_ImplOpenGL3_Init("#version 330")) { std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl; } - std::list* cmdList = - std::any_cast*>(wm->getData("renderCmdList")); - - cmdList->push_back([](){ - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - }); - - rcmd = --cmdList->end(); - cmdList->end()++; - //Compute first frame ahead of first WindowModule->run() ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); ImGui::Render(); -} -void ImguiModule::run() { ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - - { - ImGuiIO& io = ImGui::GetIO(); - static float f = 0.0f; - static int counter = 0; - - ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. - - ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) - - ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f - - if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) - counter++; - ImGui::SameLine(); - ImGui::Text("counter = %d", counter); - - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); - ImGui::End(); - } - ImGui::Render(); +} + +void ImguiModule::IGLayer::onRender() { + ImGui::Render(); + + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); +} + +bool ImguiModule::IGLayer::onEvent(const Archimedes::Event& e) { return false; } + +void ImguiModule::IGLayer::onDetach() { + + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); } diff --git a/modules/ImguiModule/src/ImguiModule.h b/modules/ImguiModule/src/ImguiModule.h index d735dd5..13aaf0f 100644 --- a/modules/ImguiModule/src/ImguiModule.h +++ b/modules/ImguiModule/src/ImguiModule.h @@ -19,9 +19,29 @@ class ImguiModule : public Archimedes::GuiModule { private: - std::list::iterator rcmd; - Archimedes::Window* window; + + class IGLayer : public Archimedes::Layer { + public: + + IGLayer(ImguiModule* _imgui) : imgui(_imgui) {} + + ~IGLayer(); + + void onRender(); + + void onAttach(); + + void onDetach(); + + bool onEvent(const Archimedes::Event&); + + private: + + ImguiModule* imgui; + + } layer = IGLayer(this); + }; #ifdef TESTIMGUI_DYNAMIC diff --git a/modules/WindowModule/src/WindowModule.cpp b/modules/WindowModule/src/WindowModule.cpp index 6721883..a8bf18f 100644 --- a/modules/WindowModule/src/WindowModule.cpp +++ b/modules/WindowModule/src/WindowModule.cpp @@ -1,12 +1,16 @@ #include "WindowModule.h" WindowModule::~WindowModule() { - if(layers) + if(layers) { delete layers; - if(renderer) + } + if(renderer) { + renderer->getCmdList().clear(); delete renderer; - if(window) + } + if(window) { delete window; + } } void WindowModule::onLoad() { @@ -27,9 +31,6 @@ void WindowModule::onLoad() { } renderer->getCmdList().push_back([this](){ layers->renderAll(); }); - - data["window"] = window->getWindowImpl().getWindow(); - data["renderCmdList"] = &renderer->getCmdList(); } void WindowModule::run() { diff --git a/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp b/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp index 1a6a62f..ddfc007 100644 --- a/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp +++ b/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp @@ -12,9 +12,7 @@ TestImgui::TestImgui(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a) } TestImgui::~TestImgui() { - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplGlfw_Shutdown(); - ImGui::DestroyContext(); + } void TestImgui::onLoad() { @@ -86,9 +84,7 @@ void TestImgui::TILayer::onAttach() { //ImGui::StyleColorsLight(); // Setup Platform/Renderer backends - GLFWwindow* w = wm->getWindow()->getWindowImpl().getWindow(); - - if(!ImGui_ImplGlfw_InitForOpenGL(w, true)) + if(!ImGui_ImplGlfw_InitForOpenGL(wm->getWindow()->getWindowImpl().getWindow(), true)) std::cout << "GLFWImpl failed\n"; if(!ImGui_ImplOpenGL3_Init("#version 330")) { std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl; @@ -107,6 +103,9 @@ void TestImgui::TILayer::onRender() { } void TestImgui::TILayer::onDetach() { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); } bool TestImgui::TILayer::onEvent(const Archimedes::Event&) { return false; } diff --git a/modules/examples/GuiModules/TestImgui/src/TestImgui.h b/modules/examples/GuiModules/TestImgui/src/TestImgui.h index 9250b69..345b661 100644 --- a/modules/examples/GuiModules/TestImgui/src/TestImgui.h +++ b/modules/examples/GuiModules/TestImgui/src/TestImgui.h @@ -20,7 +20,6 @@ class TestImgui : public Archimedes::GuiModule { private: bool demo = true; - std::list::iterator rcmd; Archimedes::Window* window;