diff --git a/ExampleApps.nix b/ExampleApps.nix index ec22ca0..e34b634 100644 --- a/ExampleApps.nix +++ b/ExampleApps.nix @@ -57,6 +57,7 @@ -DWINDOW_GLFW \ -DGUIMODULE \ -DTESTIMGUI_STATIC \ + -DWINDOWMODULE_STATIC \ -I src -I include -I $imgui -I . \ -lGL -lglfw -lGLEW \ -Wall \ diff --git a/ExampleModules.nix b/ExampleModules.nix index 244d86a..347aa39 100644 --- a/ExampleModules.nix +++ b/ExampleModules.nix @@ -126,13 +126,16 @@ buildPhase = '' clang++ \ modules/examples/GuiModules/TestImgui/src/*.cpp \ + modules/WindowModule/src/*.cpp \ $imgui/backends/imgui_impl_glfw.cpp \ $imgui/backends/imgui_impl_opengl3.cpp \ $imgui/*.cpp \ -DRENDERER_OPENGL \ -DWINDOW_GLFW \ + -DGUIMODULE \ + -DWINDOWMODULE_STATIC \ -fpic -shared \ - -I src -I include -I $imgui \ + -I src -I include -I $imgui -I . \ -lGL -lglfw -lGLEW \ -Wall \ -o $name diff --git a/flake.nix b/flake.nix index 492a9ef..c93639f 100755 --- a/flake.nix +++ b/flake.nix @@ -42,7 +42,7 @@ buildPhase = '' clang++ \ - modules/Window/src/*.cpp \ + modules/WindowModule/src/*.cpp \ -fpic -shared \ -I src -I include \ -DRENDERER_OPENGL \ diff --git a/include/pch.hpp b/include/pch.hpp index 9894e41..97d7ef4 100644 --- a/include/pch.hpp +++ b/include/pch.hpp @@ -7,8 +7,9 @@ #include #include #include +#include #include - +#include #include //#include //#include diff --git a/include/utils/App/App.h b/include/utils/App/App.h index 811c0b4..8039f06 100644 --- a/include/utils/App/App.h +++ b/include/utils/App/App.h @@ -49,7 +49,9 @@ namespace Archimedes { bool done = false; - std::list modules; + std::unordered_map modules; + std::list runOrder; + std::list toClose; std::list toOpen; @@ -73,53 +75,47 @@ namespace Archimedes { return create(h, Get()); } - virtual std::list::iterator load(std::string modulePath, std::list blacklist = {}) { + virtual Module* load(std::string modulePath, std::list::iterator ins) { Module* m = dynamicLoad(modulePath); - return load(m, blacklist); + return load(m, ins); } - virtual std::list::iterator load(Module* m, std::list blacklist = {}) { + virtual Module* load(Module* m, std::list::iterator ins) { if(!m) { - return modules.end(); + return nullptr; } void* h = m->getHandle(); - for(auto it = blacklist.begin(); it != blacklist.end(); it++) { + for(auto it = runOrder.begin(); it != runOrder.end(); it++) { if(*it == m->getName()) { std::cout << "Module \"" << *it << "\" is already loaded!\n"; delete m; if(h) dlclose(h); - return modules.end(); + return nullptr; } } - 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; + for(auto it = runOrder.begin(); it != runOrder.end(); it++) { + + if(m->deps.find(*it) != m->deps.end()) { + skip = true; + m->depsInstances[*it] = modules[*it]; } if(skip) { skip = false; continue; } else { - if(std::holds_alternative(it->second)) - m->depsInstances[it->first] = load(std::get(it->second), blacklist); + if(std::holds_alternative(m->deps[*it])) + m->depsInstances[*it] = load(std::get(m->deps[*it]), ins); else - m->depsInstances[it->first] = load(std::get(it->second), blacklist); + m->depsInstances[*it] = load(std::get(m->deps[*it]), ins); } } - - modules.push_back(m); - m->setSelf(--modules.end()); - modules.end()++; - - return m->self; + return m; } virtual void unload(std::list::iterator it) { @@ -135,13 +131,6 @@ namespace Archimedes { virtual void printHelp() = 0; - std::list getBlacklist() { - std::list l; - for(Module* m : modules) - l.push_back(m->getName()); - return l; - } - }; } diff --git a/include/utils/GuiModule/GuiModule.h b/include/utils/GuiModule/GuiModule.h index 3b7efb8..71ba8a7 100644 --- a/include/utils/GuiModule/GuiModule.h +++ b/include/utils/GuiModule/GuiModule.h @@ -3,7 +3,9 @@ #include "utils/Module/Module.h" +#ifndef WINDOWMODULE_STATIC #define WINDOWMODULE_STATIC +#endif #include "modules/WindowModule/src/WindowModule.h" namespace Archimedes { @@ -14,15 +16,15 @@ namespace Archimedes { typedef GuiModule* create_t(void*, App*); GuiModule(void* h, App* a) : Module(h, a) { - windowModule = new WindowModule(nullptr, a); - deps["WindowModule"] = windowModule; + //wm = new WindowModule(nullptr, a); + deps["WindowModule"] = new WindowModule(nullptr, a); } virtual ~GuiModule() {} virtual void onLoad() = 0; virtual void run() = 0; protected: - WindowModule* windowModule; + //WindowModule* wm; }; } diff --git a/include/utils/Module/Module.h b/include/utils/Module/Module.h index 5360f50..a41f130 100644 --- a/include/utils/Module/Module.h +++ b/include/utils/Module/Module.h @@ -25,17 +25,18 @@ namespace Archimedes { std::string getName() const { return name; } void* getHandle() { return handle; } - void setSelf(std::list::iterator s) { self = s; } + std::any getData(std::string s) { return data[s.c_str()]; }; protected: std::string name; void* handle; - std::list::iterator self; App* app; std::unordered_map> deps; - std::unordered_map::iterator> depsInstances; + std::unordered_map depsInstances; + + std::unordered_map data; }; } diff --git a/include/utils/Renderer/Renderer.h b/include/utils/Renderer/Renderer.h index d8e3716..610915e 100644 --- a/include/utils/Renderer/Renderer.h +++ b/include/utils/Renderer/Renderer.h @@ -13,21 +13,14 @@ namespace Archimedes { ~Renderer() {} - void init() { r.init(); } + bool init() { return r.init(); } void render() { r.render(rc, w, h); } - std::list::iterator addRenderCmd(renderCmd* cmd) { - - auto it = rc.end(); - rc.push_back(cmd); - return it; - } - - void removeRenderCmd(std::list::iterator cmd) { - rc.erase(cmd); + std::list& getCmdList() { + return rc; } private: diff --git a/include/utils/Renderer/RendererOpenGL/RendererOpenGL.h b/include/utils/Renderer/RendererOpenGL/RendererOpenGL.h index 0812722..d8ac059 100644 --- a/include/utils/Renderer/RendererOpenGL/RendererOpenGL.h +++ b/include/utils/Renderer/RendererOpenGL/RendererOpenGL.h @@ -17,7 +17,9 @@ namespace Archimedes { RendererOpenGL() {}; ~RendererOpenGL() {}; - void init() { glewInit(); }; + bool init() { + return glewInit() == GLEW_OK; + }; void render(std::list cmdList, int& w, int& h) { diff --git a/include/utils/Window/Window.h b/include/utils/Window/Window.h index 91f9e50..1e51a18 100644 --- a/include/utils/Window/Window.h +++ b/include/utils/Window/Window.h @@ -11,7 +11,9 @@ namespace Archimedes { public: - ~Window() {}; + Window() { renderer = new Renderer(); } + + ~Window() { delete renderer; } bool shouldClose() { return window.shouldClose(); } @@ -27,7 +29,7 @@ namespace Archimedes { } Renderer* getRenderer() { return renderer; } - void setRenderer(Renderer* r) { renderer = r; } + //void setRenderer(Renderer* r) { renderer = r; } WindowImpl& getWindowImpl() { return window; } diff --git a/include/utils/Window/WindowGLFW/WindowGLFW.h b/include/utils/Window/WindowGLFW/WindowGLFW.h index e6c92a4..d54c75f 100644 --- a/include/utils/Window/WindowGLFW/WindowGLFW.h +++ b/include/utils/Window/WindowGLFW/WindowGLFW.h @@ -31,6 +31,7 @@ namespace Archimedes { std::cout << "Window Created!\n"; glfwMakeContextCurrent(w); glfwSwapInterval(1); + } ~WindowGLFW() { diff --git a/modules/WindowModule/src/WindowModule.cpp b/modules/WindowModule/src/WindowModule.cpp index 23b6d52..48e4b20 100644 --- a/modules/WindowModule/src/WindowModule.cpp +++ b/modules/WindowModule/src/WindowModule.cpp @@ -1,9 +1,5 @@ #include "WindowModule.h" -WindowModule::WindowModule(void* h, Archimedes::App* a) : Module(h, a) { - name = "Window"; -} - WindowModule::~WindowModule() { if(window) delete window; @@ -14,11 +10,21 @@ WindowModule::~WindowModule() { void WindowModule::onLoad() { window = new Archimedes::Window(); - renderer = new Archimedes::Renderer(); + //renderer = new Archimedes::Renderer(); - window->setRenderer(renderer); + //window->setRenderer(renderer); + + renderer = window->getRenderer(); - renderer->init(); + if(!renderer->init()) { + std::cout << "Renderer init failed!\n"; + std::abort(); + } + + data["window"] = window->getWindowImpl().getWindow(); + data["renderCmdList"] = &renderer->getCmdList(); + + std::cout << "WindowModule GLFWwindow*: " << std::any_castgetWindowImpl().getWindow())>(data["window"]) << std::endl; } void WindowModule::run() { diff --git a/modules/WindowModule/src/WindowModule.h b/modules/WindowModule/src/WindowModule.h index 0659c31..fd2568f 100644 --- a/modules/WindowModule/src/WindowModule.h +++ b/modules/WindowModule/src/WindowModule.h @@ -1,36 +1,37 @@ #ifndef WINDOWMODULE_H #define WINDOWMODULE_H +#ifdef GUIMODULE +#undef GUIMODULE #include "Archimedes.h" +#define GUIMODULE +#else +#include "Archimedes.h" +#endif + #include "utils/Window/Window.h" #include "utils/Renderer/Renderer.h" class WindowModule : public Archimedes::Module { public: - WindowModule(void*, Archimedes::App*); + WindowModule(void* h, Archimedes::App* a) : Archimedes::Module(h, a) { + name = "WindowModule"; + } + ~WindowModule(); void run(); void onLoad(); - + //interface for other modules - std::list::iterator addRenderCmd(Archimedes::Renderer::renderCmd* cmd) { - return renderer->addRenderCmd(cmd); - } - - void removeRenderCmd(std::list::iterator cmd) { - renderer->removeRenderCmd(cmd); - } - - auto getWindowImpl() { return window->getWindowImpl(); } - - private: Archimedes::Window* window; Archimedes::Renderer* renderer; + private: + }; diff --git a/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp b/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp index 25eae56..c4b89b7 100644 --- a/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp +++ b/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp @@ -12,14 +12,24 @@ TestImgui::TestImgui(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a) } TestImgui::~TestImgui() { - windowModule->removeRenderCmd(rcmd); + + std::list* cmdList = + std::any_cast*>(depsInstances["WindowModule"]->getData("renderCmdList")); + cmdList->erase(rcmd); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); } void TestImgui::onLoad() { - + + Archimedes::Module* wm = depsInstances["WindowModule"]; + + if(!wm) { + std::cout << "No WindowModule for TestImgui!\n"; + std::abort(); + } + IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; @@ -27,26 +37,45 @@ void TestImgui::onLoad() { io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking - //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows + //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows - // Setup Dear ImGui style + // Setup Dear ImGui style ImGui::StyleColorsDark(); //ImGui::StyleColorsLight(); + std::cout << "init backends\n"; // Setup Platform/Renderer backends - ImGui_ImplGlfw_InitForOpenGL(windowModule->getWindowImpl().getWindow(), true); - ImGui_ImplOpenGL3_Init("#version 330"); - - rcmd = windowModule->addRenderCmd([](){ - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + std::cout << wm->getName() << std::endl; + GLFWwindow* w = std::any_cast(wm->getData("window")); - /*if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - ImGui::UpdatePlatformWindows(); - ImGui::RenderPlatformWindowsDefault(); - }*/ - }); + std::cout << "TestImgui GLFWwindow*: " << w << std::endl; + + if(!ImGui_ImplGlfw_InitForOpenGL(w, true)) + std::cout << "GLFWImpl failed\n"; + if(!ImGui_ImplOpenGL3_Init("#version 330")) { + std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl; + } + std::cout << "register renderCmd\n"; + + + + 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 TestImgui::run() { @@ -60,5 +89,4 @@ void TestImgui::run() { app->end(); ImGui::Render(); - } diff --git a/modules/examples/GuiModules/TestImgui/src/TestImgui.h b/modules/examples/GuiModules/TestImgui/src/TestImgui.h index 028f618..a40273a 100644 --- a/modules/examples/GuiModules/TestImgui/src/TestImgui.h +++ b/modules/examples/GuiModules/TestImgui/src/TestImgui.h @@ -4,6 +4,7 @@ #include "Archimedes.h" +#include "utils/Window/Window.h" class TestImgui : public Archimedes::GuiModule { @@ -16,9 +17,12 @@ class TestImgui : public Archimedes::GuiModule { void run(); + private: bool demo = true; std::list::iterator rcmd; + + Archimedes::Window* window; }; #ifndef TESTIMGUI_STATIC diff --git a/src/example_apps/ImguiEmbed/ImguiEmbed.cpp b/src/example_apps/ImguiEmbed/ImguiEmbed.cpp index 3a8baff..003b646 100644 --- a/src/example_apps/ImguiEmbed/ImguiEmbed.cpp +++ b/src/example_apps/ImguiEmbed/ImguiEmbed.cpp @@ -2,14 +2,19 @@ void ImguiEmbed::run() { - for(auto* m : modules) - m->onLoad(); + for(auto* m : modules) { + std::cout << "Loading Module: " << m->getName() << std::endl; + //if(m->getName() != "WindowModule") + m->onLoad(); + } // Main loop while (!done && !modules.empty()) { for(auto* m : modules) { - m->run(); + //std::cout << "Running Module: " << m->getName() << std::endl; + //if(m->getName() != "WindowModule") + m->run(); } for(auto it = toClose.begin(); it != toClose.end(); it++) { @@ -18,7 +23,7 @@ void ImguiEmbed::run() { toClose.clear(); for(std::string s : toOpen) { - load(s, getBlacklist()); + load(s, modules.begin()); } toOpen.clear(); } diff --git a/src/example_apps/MinimalApp/MinimalApp.cpp b/src/example_apps/MinimalApp/MinimalApp.cpp index ad3d314..092f94c 100644 --- a/src/example_apps/MinimalApp/MinimalApp.cpp +++ b/src/example_apps/MinimalApp/MinimalApp.cpp @@ -18,7 +18,7 @@ void MinimalApp::run() { toClose.clear(); for(std::string s : toOpen) { - load(s, getBlacklist()); + load(s, modules.begin()); } toOpen.clear(); } diff --git a/src/example_apps/MinimalApp/MinimalApp.h b/src/example_apps/MinimalApp/MinimalApp.h index 9e07aed..316e04d 100644 --- a/src/example_apps/MinimalApp/MinimalApp.h +++ b/src/example_apps/MinimalApp/MinimalApp.h @@ -14,7 +14,7 @@ class MinimalApp : public Archimedes::App { void handleArgs(const int& argc, char* argv[]) { if(argc > 1) { for(int i = 1; i < argc; i++) - load(dynamicLoad(argv[i]), getBlacklist()); + load(dynamicLoad(argv[i]), modules.begin()); } else { std::cout << "No modules to load\n"; end();