From 908b00db62ceb9274ca3873f4d5eb8cffd2fdf3a Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 9 Apr 2025 14:16:38 -0500 Subject: [PATCH] layers are redundant --- include/endModule.h | 4 +- include/utils/App/App.h | 16 ++-- include/utils/GuiModule/GuiModule.h | 9 +-- include/utils/Layers/Layer.h | 25 ------ include/utils/Layers/Layerstack.h | 51 ------------ include/utils/Module/Module.h | 19 +++-- modules/ImguiModule/src/ImguiModule.cpp | 90 ++++++++-------------- modules/ImguiModule/src/ImguiModule.h | 25 +----- modules/WindowModule/src/WindowModule.cpp | 7 -- modules/WindowModule/src/WindowModule.h | 5 +- src/example_apps/MinimalApp/MinimalApp.cpp | 8 +- 11 files changed, 68 insertions(+), 191 deletions(-) delete mode 100644 include/utils/Layers/Layer.h delete mode 100644 include/utils/Layers/Layerstack.h diff --git a/include/endModule.h b/include/endModule.h index 5ca5705..59dbe5c 100644 --- a/include/endModule.h +++ b/include/endModule.h @@ -3,8 +3,8 @@ #ifdef MODULE_TYPE extern "C" { - Archimedes::Module* create(void* handle, Archimedes::App* app) { - return new MODULE_TYPE(handle, app); + Archimedes::Module* create(Archimedes::App* app, void* handle) { + return new MODULE_TYPE(app, handle); } } diff --git a/include/utils/App/App.h b/include/utils/App/App.h index 2bddc7b..b1451c8 100644 --- a/include/utils/App/App.h +++ b/include/utils/App/App.h @@ -39,7 +39,7 @@ namespace Archimedes { virtual void stopModule(std::string lib) { toClose.push_back(lib); } - virtual void startModule(std::string lib) { toOpen.push_back(lib); } + virtual void startModule(std::string lib, std::variant m) { toOpen[lib] = m; } void end() { done = true; } @@ -57,8 +57,8 @@ namespace Archimedes { std::list runOrder; std::list toClose; - std::list toOpen; - + std::unordered_map> toOpen; + virtual Module* dynamicLoad(std::string lib) { void* h = dlopen(lib.c_str(), RTLD_NOW); @@ -76,7 +76,7 @@ namespace Archimedes { return nullptr; } - return create(h, Get()); + return create(Get(), h); } virtual Module* load(std::string modulePath) { @@ -144,6 +144,9 @@ namespace Archimedes { virtual void unload(std::string name) { + if(modules.find(name) == modules.end()) + return; + //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()) { @@ -155,10 +158,11 @@ namespace Archimedes { void* h = m->getHandle(); modules[name] = nullptr; - delete m; - if(h) + if(h) { + delete m; dlclose(h); + } runOrder.remove(name); } diff --git a/include/utils/GuiModule/GuiModule.h b/include/utils/GuiModule/GuiModule.h index 9cd2995..a6de3c0 100644 --- a/include/utils/GuiModule/GuiModule.h +++ b/include/utils/GuiModule/GuiModule.h @@ -12,14 +12,11 @@ namespace Archimedes { class GuiModule : public Module { public: - typedef GuiModule* create_t(void*, App*); - - GuiModule(void* h, App* a) : Module(h, a) { - deps["WindowModule"] = new WindowModule(nullptr, a); + GuiModule(App* a, void* h) : Module(a, h) { + WindowModule* wm = new WindowModule(a); + deps[wm->getName()] = wm; } virtual ~GuiModule() {} - virtual void onLoad() = 0; - virtual void run() = 0; }; } diff --git a/include/utils/Layers/Layer.h b/include/utils/Layers/Layer.h deleted file mode 100644 index 7ff5e1b..0000000 --- a/include/utils/Layers/Layer.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef LAYER_H -#define LAYER_H - -#include "pch.hpp" -#include "utils/Events/Event.h" - -namespace Archimedes { - - class Layer { - - public: - - virtual ~Layer() {} - - virtual void onRender() = 0; - - virtual void onAttach() = 0; - - virtual void onDetach() = 0; - - virtual bool onEvent(const Event&) = 0; - }; -} - -#endif diff --git a/include/utils/Layers/Layerstack.h b/include/utils/Layers/Layerstack.h deleted file mode 100644 index e95504a..0000000 --- a/include/utils/Layers/Layerstack.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef LAYERSTACK_H -#define LAYERSTACK_H - -#include "pch.hpp" -#include "Layer.h" - -namespace Archimedes { - class Layerstack { - - public: - Layerstack() {} - - ~Layerstack() { - while(!lstack.empty()) { - pop(); - } - } - - void push(Layer* l) { - if(l) { - lstack.push_front(l); - l->onAttach(); - } - } - - void pop() { - Layer* l = lstack.front(); - lstack.pop_front(); - l->onDetach(); - } - - void renderAll() { - for(Layer* l : lstack) - l->onRender(); - } - - void sendEvent(const Event& e) { - for(Layer* l : lstack) { - if(l->onEvent(e)) { - break; - } - } - } - - private: - - std::list lstack; - }; -} - -#endif diff --git a/include/utils/Module/Module.h b/include/utils/Module/Module.h index a41f130..9ee9919 100644 --- a/include/utils/Module/Module.h +++ b/include/utils/Module/Module.h @@ -7,36 +7,39 @@ namespace Archimedes { class App; + class Event; + class Module { friend class App; public: - typedef Module* create_t(void*, App*); + typedef Module* create_t(App*, void*); - Module(void* h, App* a) : handle(h), app(a) {} + Module(App* a, void* h) : app(a), handle(h) {} virtual ~Module() {} - virtual void run() = 0; - virtual void onLoad() = 0; + virtual void run() {} + virtual bool onEvent(const Event& e) { return false; } + virtual void onLoad() {}; std::string getName() const { return name; } void* getHandle() { return handle; } - std::any getData(std::string s) { return data[s.c_str()]; }; + //std::any getData(std::string s) { return data[s.c_str()]; }; protected: std::string name; - void* handle; - + App* app; + void* handle; std::unordered_map> deps; std::unordered_map depsInstances; - std::unordered_map data; + //std::unordered_map data; }; } diff --git a/modules/ImguiModule/src/ImguiModule.cpp b/modules/ImguiModule/src/ImguiModule.cpp index 7b185dd..50e123a 100644 --- a/modules/ImguiModule/src/ImguiModule.cpp +++ b/modules/ImguiModule/src/ImguiModule.cpp @@ -6,13 +6,16 @@ #include -ImguiModule::ImguiModule(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a) { +ImguiModule::ImguiModule(Archimedes::App* a, void* h = nullptr) : Archimedes::GuiModule(a, h) { name = "ImguiModule"; } ImguiModule::~ImguiModule() { - + + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); } void ImguiModule::onLoad() { @@ -22,40 +25,7 @@ void ImguiModule::onLoad() { 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"; - std::abort(); - } - + IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; @@ -77,34 +47,38 @@ void ImguiModule::IGLayer::onAttach() { } + wm->getRenderer()->getCmdList().push_back([](){ + ImGui::Render(); + + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + }); + //Compute first frame ahead of first WindowModule->run() ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - - ImGui::Render(); - - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); - } -void ImguiModule::IGLayer::onRender() { - ImGui::Render(); +void ImguiModule::run() { + ImGuiIO& io = ImGui::GetIO(); + static float f = 0.0f; + static int counter = 0; - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. - 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(); + 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(); } diff --git a/modules/ImguiModule/src/ImguiModule.h b/modules/ImguiModule/src/ImguiModule.h index 13aaf0f..7619985 100644 --- a/modules/ImguiModule/src/ImguiModule.h +++ b/modules/ImguiModule/src/ImguiModule.h @@ -9,39 +9,20 @@ class ImguiModule : public Archimedes::GuiModule { public: - ImguiModule(void*, Archimedes::App*); + ImguiModule(Archimedes::App*, void*); ~ImguiModule(); void onLoad(); + bool onEvent(const Archimedes::Event& e) { return false; } + void run(); private: 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 a8bf18f..830e765 100644 --- a/modules/WindowModule/src/WindowModule.cpp +++ b/modules/WindowModule/src/WindowModule.cpp @@ -1,9 +1,6 @@ #include "WindowModule.h" WindowModule::~WindowModule() { - if(layers) { - delete layers; - } if(renderer) { renderer->getCmdList().clear(); delete renderer; @@ -23,14 +20,10 @@ void WindowModule::onLoad() { //renderer = window->getRenderer(); - layers = new Archimedes::Layerstack(); - if(!renderer->init()) { std::cout << "Renderer init failed!\n"; std::abort(); } - - renderer->getCmdList().push_back([this](){ layers->renderAll(); }); } void WindowModule::run() { diff --git a/modules/WindowModule/src/WindowModule.h b/modules/WindowModule/src/WindowModule.h index 0f0c10e..6c83ca8 100644 --- a/modules/WindowModule/src/WindowModule.h +++ b/modules/WindowModule/src/WindowModule.h @@ -11,12 +11,11 @@ #include "utils/Window/Window.h" #include "utils/Renderer/Renderer.h" -#include "utils/Layers/Layerstack.h" class WindowModule : public Archimedes::Module { public: - WindowModule(void* h, Archimedes::App* a) : Archimedes::Module(h, a) { + WindowModule(Archimedes::App* a, void* h = nullptr) : Archimedes::Module(a, h) { name = "WindowModule"; } @@ -31,13 +30,11 @@ class WindowModule : public Archimedes::Module { Archimedes::Window* getWindow() { return window; } Archimedes::Renderer* getRenderer() { return renderer; } - Archimedes::Layerstack* getLayerstack() { return layers; } private: Archimedes::Window* window; Archimedes::Renderer* renderer; - Archimedes::Layerstack* layers; }; diff --git a/src/example_apps/MinimalApp/MinimalApp.cpp b/src/example_apps/MinimalApp/MinimalApp.cpp index f061630..10d49ff 100644 --- a/src/example_apps/MinimalApp/MinimalApp.cpp +++ b/src/example_apps/MinimalApp/MinimalApp.cpp @@ -18,8 +18,12 @@ void MinimalApp::run() { } toClose.clear(); - for(std::string m : toOpen) { - load(m); + for(auto m : toOpen) { + if(std::holds_alternative(m.second)) { + load(std::get(m.second)); + } else { + load(std::get(m.second)); + } } toOpen.clear(); }