From 121fba5930cc9bba750f095032382ec8ea703941 Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 31 Mar 2025 13:49:00 -0500 Subject: [PATCH] make include headers only --- flake.nix | 20 +-- include/utils/App/App.cpp | 124 ---------------- include/utils/App/App.h | 137 +++++++++++++++--- include/utils/GuiModule/GuiModule.h | 12 +- include/utils/Renderer/Renderer.cpp | 10 -- include/utils/Renderer/Renderer.h | 6 +- include/utils/Window/Window.cpp | 18 --- include/utils/Window/Window.h | 17 ++- modules/WindowModule/src/WindowModule.cpp | 8 + modules/WindowModule/src/WindowModule.h | 12 ++ .../GuiModules/TestImgui/src/TestImgui.cpp | 18 +-- 11 files changed, 167 insertions(+), 215 deletions(-) delete mode 100644 include/utils/App/App.cpp delete mode 100644 include/utils/Renderer/Renderer.cpp delete mode 100644 include/utils/Window/Window.cpp diff --git a/flake.nix b/flake.nix index 8ab2507..521b584 100755 --- a/flake.nix +++ b/flake.nix @@ -36,7 +36,6 @@ buildPhase = '' clang++ \ src/example_apps/MinimalApp/MinimalApp.cpp \ - include/utils/App/App.cpp \ -I src -I include \ -Wall \ -o $name @@ -73,9 +72,6 @@ $imgui/backends/imgui_impl_glfw.cpp \ $imgui/backends/imgui_impl_opengl3.cpp \ $imgui/*.cpp \ - include/utils/App/App.cpp \ - include/utils/Renderer/*.cpp \ - include/utils/Window/*.cpp \ -DRENDERER_OPENGL \ -DWINDOW_GLFW \ -DGUIMODULE \ @@ -107,7 +103,7 @@ buildPhase = '' clang++ \ - modules/TestMenu/src/*.cpp src/App.cpp \ + modules/TestMenu/src/*.cpp \ -fpic -shared \ -I src \ -Wall \ @@ -134,7 +130,6 @@ buildPhase = '' clang++ \ modules/Print/src/*.cpp \ - include/utils/App/App.cpp \ -fpic -shared \ -I src -I include \ -Wall \ @@ -161,7 +156,6 @@ buildPhase = '' clang++ \ modules/DependsOnPrint/src/*.cpp \ - include/utils/App/App.cpp \ -fpic -shared \ -I src -I include \ -Wall \ @@ -189,7 +183,6 @@ clang++ \ modules/DependsOnPrintStatic/src/*.cpp \ modules/Print/src/*.cpp \ - include/utils/App/App.cpp \ -fpic -shared \ -I src -I include -I . \ -DPRINT_STATIC \ @@ -221,9 +214,7 @@ buildPhase = '' clang++ \ - modules/Window/src/*.cpp src/App.cpp \ - include/utils/Window/*.cpp \ - include/utils/Renderer/*.cpp \ + modules/Window/src/*.cpp \ -fpic -shared \ -I src -I include \ -DRENDERER_OPENGL \ @@ -263,9 +254,6 @@ $imgui/backends/imgui_impl_glfw.cpp \ $imgui/backends/imgui_impl_opengl3.cpp \ $imgui/*.cpp \ - include/utils/App/App.cpp \ - include/utils/Renderer/*.cpp \ - include/utils/Window/*.cpp \ -DRENDERER_OPENGL \ -DWINDOW_GLFW \ -fpic -shared \ @@ -296,7 +284,7 @@ buildPhase = '' clang++ \ - modules/TestClay/src/*.cpp src/App.cpp \ + modules/TestClay/src/*.cpp \ -fpic -shared \ -I src -I include \ -DRENDERER_OPENGL \ @@ -326,7 +314,7 @@ buildPhase = '' clang++ \ - modules/MainGUI/src/*.cpp src/App.cpp \ + modules/MainGUI/src/*.cpp \ -fpic -shared \ -I src -I include \ -Wall \ diff --git a/include/utils/App/App.cpp b/include/utils/App/App.cpp deleted file mode 100644 index def6ec9..0000000 --- a/include/utils/App/App.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "App.h" - -Archimedes::App* Archimedes::App::instance = nullptr; - -namespace Archimedes { - - - App::App() { - - if(instance != nullptr) { - std::cout << "App already exists\nThere can only be one!\n"; - std::abort(); - } - - instance = this; - - } - - App::~App() { - - for(auto it = modules.begin(); it != modules.end(); it++) { - void* handle = (*it)->getHandle(); - delete *it; - if(handle) - dlclose(handle); - it = modules.erase(it); - } - } - - Module* App::dynamicLoad(std::string lib) { - - void* h = dlopen(lib.c_str(), RTLD_NOW); - - if(!h) { - std::cout << "could not open lib at: \"" << lib.c_str() << "\"\nError: " << dlerror() << std::endl; - return nullptr; - } - - Module::create_t* create = (Module::create_t*) dlsym(h, "create"); - char* err = dlerror(); - if(err) { - std::cout << "error finding create function in file: " << lib << std::endl; - std::cout << "dlerror(): " << err << std::endl; - return nullptr; - } - - return create(h, Get()); - } - - std::list::iterator App::load(Module* m, std::list blacklist = {}) { - - if(!m) { - return modules.end(); - } - - void* h = m->getHandle(); - for(auto it = blacklist.begin(); it != blacklist.end(); it++) { - if(*it == m->getName()) { - std::cout << "Module \"" << *it << "\" is already loaded!\n"; - delete m; - if(h) - dlclose(h); - return modules.end(); - } - } - - blacklist.push_back(m->getName()); - - bool skip = false; - for(auto it = m->deps.begin(); it != m->deps.end(); it++) { - for(std::string s : blacklist) { - if(it->first == s) - skip = true; - } - if(skip) { - skip = false; - continue; - } else { - if(std::holds_alternative(it->second)) - m->depsInstances[it->first] = load(std::get(it->second), blacklist); - else - m->depsInstances[it->first] = load(std::get(it->second), blacklist); - } - } - - - modules.push_back(m); - m->setSelf(--modules.end()); - modules.end()++; -/* - m->setSelf(modules.end()); - modules.push_back(m); -*/ - return m->self; - } - - std::list::iterator App::load(std::string modulePath, std::list blacklist = {}) { - Module* m = dynamicLoad(modulePath); - return load(m, blacklist); - } - - - void App::unload(std::list::iterator it) { - - Module* m = *it; - void* h = m->getHandle(); - - modules.erase(m->self); - delete m; - - if(h) - dlclose(h); - - } - - void App::stopModule(std::list::iterator it) { - toClose.push_back(*it); - } - - void App::startModule(std::string lib) { - toOpen.push_back(lib); - } - -} diff --git a/include/utils/App/App.h b/include/utils/App/App.h index 3dbacc6..6056b92 100644 --- a/include/utils/App/App.h +++ b/include/utils/App/App.h @@ -8,20 +8,130 @@ namespace Archimedes { class App { - protected: - static App* instance; + public: + App() { + if(instance != nullptr) { + std::cout << "App already exists\nThere can only be one!\n"; + std::abort(); + } + instance = this; + } + virtual ~App() { + + for(auto it = modules.begin(); it != modules.end(); it++) { + void* handle = (*it)->getHandle(); + delete *it; + if(handle) + dlclose(handle); + it = modules.erase(it); + } + } + + + static App* Get() { return instance; } + + virtual void handleArgs(const int&, char*[]) = 0; + + virtual void run() = 0; + + virtual void stopModule(std::list::iterator it) { toClose.push_back(*it); } + + virtual void startModule(std::string lib) { toOpen.push_back(lib); } + + void end() { done = true; } + + private: + bool done = false; + + inline static App* instance = nullptr; + + protected: std::list modules; std::list toClose; std::list toOpen; - Module* dynamicLoad(std::string); - virtual std::list::iterator load(std::string, std::list); - virtual std::list::iterator load(Module*, std::list); + virtual Module* dynamicLoad(std::string lib) { - virtual void unload(std::list::iterator); + void* h = dlopen(lib.c_str(), RTLD_NOW); + + if(!h) { + std::cout << "could not open lib at: \"" << lib.c_str() << "\"\nError: " << dlerror() << std::endl; + return nullptr; + } + + Module::create_t* create = (Module::create_t*) dlsym(h, "create"); + char* err = dlerror(); + if(err) { + std::cout << "error finding create function in file: " << lib << std::endl; + std::cout << "dlerror(): " << err << std::endl; + return nullptr; + } + + return create(h, Get()); + } + + virtual std::list::iterator load(std::string modulePath, std::list blacklist = {}) { + Module* m = dynamicLoad(modulePath); + return load(m, blacklist); + } + + virtual std::list::iterator load(Module* m, std::list blacklist = {}) { + + if(!m) { + return modules.end(); + } + + void* h = m->getHandle(); + for(auto it = blacklist.begin(); it != blacklist.end(); it++) { + if(*it == m->getName()) { + std::cout << "Module \"" << *it << "\" is already loaded!\n"; + delete m; + if(h) + dlclose(h); + return modules.end(); + } + } + + blacklist.push_back(m->getName()); + + bool skip = false; + for(auto it = m->deps.begin(); it != m->deps.end(); it++) { + for(std::string s : blacklist) { + if(it->first == s) + skip = true; + } + if(skip) { + skip = false; + continue; + } else { + if(std::holds_alternative(it->second)) + m->depsInstances[it->first] = load(std::get(it->second), blacklist); + else + m->depsInstances[it->first] = load(std::get(it->second), blacklist); + } + } + + + modules.push_back(m); + m->setSelf(--modules.end()); + modules.end()++; + + return m->self; + } + + virtual void unload(std::list::iterator it) { + Module* m = *it; + void* h = m->getHandle(); + + modules.erase(m->self); + delete m; + + if(h) + dlclose(h); + } virtual void printHelp() = 0; @@ -32,21 +142,6 @@ namespace Archimedes { return l; } - public: - App(); - virtual ~App(); - - static App* Get() { return instance; } - - virtual void handleArgs(const int&, char*[]) = 0; - - virtual void run() = 0; - - virtual void stopModule(std::list::iterator); - - virtual void startModule(std::string); - - void end() { done = true; } }; } diff --git a/include/utils/GuiModule/GuiModule.h b/include/utils/GuiModule/GuiModule.h index 0fd1146..68dcb00 100644 --- a/include/utils/GuiModule/GuiModule.h +++ b/include/utils/GuiModule/GuiModule.h @@ -12,19 +12,17 @@ namespace Archimedes { typedef GuiModule* create_t(void*, App*); GuiModule(void* h, App* a) : Module(h, a) { - window = new WindowModule(nullptr, a); - deps["WindowModule"] = window; + windowModule = new WindowModule(nullptr, a); + deps["WindowModule"] = windowModule; } - virtual ~GuiModule() { if(window) delete window; } + virtual ~GuiModule() {} virtual void onLoad() = 0; virtual void run() = 0; - Window* getWindow() { return window; } - void createWindow() { window = new Window(); } + Window* getWindow() { return windowModule; } protected: - WindowModule* window; - //Renderer* renderer; + WindowModule* windowModule; }; } diff --git a/include/utils/Renderer/Renderer.cpp b/include/utils/Renderer/Renderer.cpp deleted file mode 100644 index c070f1d..0000000 --- a/include/utils/Renderer/Renderer.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "Renderer.h" - -#include "pch.hpp" - -namespace Archimedes { - - void Renderer::render() { - r.render(rc, w, h); - } -} diff --git a/include/utils/Renderer/Renderer.h b/include/utils/Renderer/Renderer.h index a9e991f..d8e3716 100644 --- a/include/utils/Renderer/Renderer.h +++ b/include/utils/Renderer/Renderer.h @@ -1,7 +1,7 @@ #ifndef RENDERER_H #define RENDERER_H - +#include "pch.hpp" #include "RendererOpenGL/RendererOpenGL.h" namespace Archimedes { @@ -15,7 +15,9 @@ namespace Archimedes { void init() { r.init(); } - void render(); + void render() { + r.render(rc, w, h); + } std::list::iterator addRenderCmd(renderCmd* cmd) { diff --git a/include/utils/Window/Window.cpp b/include/utils/Window/Window.cpp deleted file mode 100644 index 14cff74..0000000 --- a/include/utils/Window/Window.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "Window.h" - -#include "pch.hpp" - -namespace Archimedes { - - void Window::doFrame() { - - window.pollEvents(); - - window.getSize(renderer.w, renderer.h); - - renderer.render(); - - window.doFrame(); - } - -} diff --git a/include/utils/Window/Window.h b/include/utils/Window/Window.h index 3bedec1..91f9e50 100644 --- a/include/utils/Window/Window.h +++ b/include/utils/Window/Window.h @@ -15,13 +15,24 @@ namespace Archimedes { bool shouldClose() { return window.shouldClose(); } - void doFrame(); + void doFrame() { + + window.pollEvents(); + + window.getSize(renderer->w, renderer->h); + + renderer->render(); + + window.doFrame(); + } + + Renderer* getRenderer() { return renderer; } + void setRenderer(Renderer* r) { renderer = r; } - Renderer& getRenderer() { return renderer; } WindowImpl& getWindowImpl() { return window; } private: - Renderer renderer; + Renderer* renderer; WindowImpl window; }; diff --git a/modules/WindowModule/src/WindowModule.cpp b/modules/WindowModule/src/WindowModule.cpp index 586079a..23b6d52 100644 --- a/modules/WindowModule/src/WindowModule.cpp +++ b/modules/WindowModule/src/WindowModule.cpp @@ -7,11 +7,18 @@ WindowModule::WindowModule(void* h, Archimedes::App* a) : Module(h, a) { WindowModule::~WindowModule() { if(window) delete window; + if(renderer) + delete renderer; } void WindowModule::onLoad() { window = new Archimedes::Window(); + renderer = new Archimedes::Renderer(); + + window->setRenderer(renderer); + + renderer->init(); } void WindowModule::run() { @@ -22,3 +29,4 @@ void WindowModule::run() { window->doFrame(); } + diff --git a/modules/WindowModule/src/WindowModule.h b/modules/WindowModule/src/WindowModule.h index 43feb7b..5e0ef6d 100644 --- a/modules/WindowModule/src/WindowModule.h +++ b/modules/WindowModule/src/WindowModule.h @@ -13,9 +13,21 @@ class WindowModule : public Archimedes::Module { void onLoad(); + //interface for other modules + std::list::iterator addRenderCmd(Archimedes::Renderer::renderCmd* cmd) { + renderer->addRenderCmd(cmd); + } + + void removeRenderCmd(std::list::iterator cmd) { + renderer->removeRenderCmd(cmd); + } + + auto getWindowImpl() { return window->getWindowImpl(); } + private: Archimedes::Window* window; + Archimedes::Renderer* renderer; }; diff --git a/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp b/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp index cb09a0e..25eae56 100644 --- a/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp +++ b/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp @@ -12,7 +12,7 @@ TestImgui::TestImgui(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a) } TestImgui::~TestImgui() { - window->getRenderer().removeRenderCmd(rcmd); + windowModule->removeRenderCmd(rcmd); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); @@ -20,10 +20,6 @@ TestImgui::~TestImgui() { void TestImgui::onLoad() { - createWindow(); - - window->getRenderer().init(); - IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; @@ -38,10 +34,10 @@ void TestImgui::onLoad() { //ImGui::StyleColorsLight(); // Setup Platform/Renderer backends - ImGui_ImplGlfw_InitForOpenGL(window->getWindowImpl().getWindow(), true); - ImGui_ImplOpenGL3_Init("#version 130"); + ImGui_ImplGlfw_InitForOpenGL(windowModule->getWindowImpl().getWindow(), true); + ImGui_ImplOpenGL3_Init("#version 330"); - rcmd = window->getRenderer().addRenderCmd([](){ + rcmd = windowModule->addRenderCmd([](){ ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); /*if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) @@ -51,7 +47,6 @@ void TestImgui::onLoad() { }*/ }); - //cmdlist.push_back(it); } void TestImgui::run() { @@ -66,9 +61,4 @@ void TestImgui::run() { ImGui::Render(); - - if(window->shouldClose()) - app->end(); - window->doFrame(); - }