diff --git a/flake.nix b/flake.nix index f26f3bf..acbdf0d 100755 --- a/flake.nix +++ b/flake.nix @@ -20,7 +20,7 @@ Archimedes = { examples = { - minimal = pkgs.stdenvNoCC.mkDerivation { + MinimalApp = pkgs.stdenvNoCC.mkDerivation { name = "Archimedes"; @@ -48,6 +48,48 @@ ''; }; + + ImguiEmbed = pkgs.stdenvNoCC.mkDerivation { + + name = "Archimedes"; + + src = ./.; + + inherit imgui; + + nativeBuildInputs = with pkgs; [ + clang + ]; + + buildInputs = with pkgs; [ + glfw + glew + ]; + + buildPhase = '' + clang++ \ + src/example_apps/ImguiEmbed/*.cpp \ + modules/GUImodules/TestImgui/src/*.cpp \ + $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 \ + -I src -I include -I $imgui -I . \ + -lGL -lglfw -lGLEW \ + -Wall \ + -o $name + ''; + + installPhase = '' + mkdir -p $out/bin + cp $name $out/bin + ''; + + }; }; }; diff --git a/include/utils/App/App.cpp b/include/utils/App/App.cpp index 471fa74..f799845 100644 --- a/include/utils/App/App.cpp +++ b/include/utils/App/App.cpp @@ -21,19 +21,19 @@ namespace Archimedes { for(auto it = modules.begin(); it != modules.end(); it++) { void* handle = (*it)->getHandle(); delete *it; - dlclose(handle); + if(handle) + dlclose(handle); it = modules.erase(it); } } - - bool App::load(std::string lib, std::list blacklist = {}) { + 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 false; + return nullptr; } Module::create_t* create = (Module::create_t*) dlsym(h, "create"); @@ -41,15 +41,25 @@ namespace Archimedes { if(err) { std::cout << "error finding create function in file: " << lib << std::endl; std::cout << "dlerror(): " << err << std::endl; + return nullptr; } - Module* m = create(h, App::Get()); + return create(h, Get()); + } + bool App::load(Module* m, std::list blacklist = {}) { + + if(!m) { + return false; + } + + 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; - dlclose(h); + if(h) + dlclose(h); return false; } } @@ -66,7 +76,7 @@ namespace Archimedes { skip = false; continue; } else { - load(it->second, blacklist); + load(dynamicLoad(it->second), blacklist); } } @@ -85,8 +95,9 @@ namespace Archimedes { modules.erase(m->self); delete m; - - dlclose(h); + + if(h) + dlclose(h); } diff --git a/include/utils/App/App.h b/include/utils/App/App.h index 8b9fcf1..25d4fa9 100644 --- a/include/utils/App/App.h +++ b/include/utils/App/App.h @@ -17,7 +17,9 @@ namespace Archimedes { std::list toClose; std::list toOpen; - virtual bool load(std::string, std::list); + Module* dynamicLoad(std::string); + virtual bool load(Module*, std::list); + virtual void unload(std::list::iterator); virtual void printHelp() = 0; diff --git a/src/example_apps/ImguiEmbed/ImguiEmbed.cpp b/src/example_apps/ImguiEmbed/ImguiEmbed.cpp new file mode 100644 index 0000000..9d4a562 --- /dev/null +++ b/src/example_apps/ImguiEmbed/ImguiEmbed.cpp @@ -0,0 +1,26 @@ +#include "ImguiEmbed.h" + +void ImguiEmbed::run() { + + for(auto* m : modules) + m->onLoad(); + + // Main loop + while (!done && !modules.empty()) { + + for(auto* m : modules) { + m->run(); + } + + for(auto it = toClose.begin(); it != toClose.end(); it++) { + unload(it); + } + toClose.clear(); + + for(std::string s : toOpen) { + load(dynamicLoad(s), getBlacklist()); + } + toOpen.clear(); + } + +} diff --git a/src/example_apps/ImguiEmbed/ImguiEmbed.h b/src/example_apps/ImguiEmbed/ImguiEmbed.h new file mode 100644 index 0000000..b08ea0e --- /dev/null +++ b/src/example_apps/ImguiEmbed/ImguiEmbed.h @@ -0,0 +1,30 @@ +#define ENTRYPOINT +#include "Archimedes.h" + +#include "modules/GUImodules/TestImgui/src/TestImgui.h" + +class ImguiEmbed : public Archimedes::App { + + private: + + void printHelp() {}; + + public: + ImguiEmbed() { + Archimedes::Module* m = (Archimedes::Module*) new TestImgui(nullptr, Get()); + + load(m, {}); + }; + ~ImguiEmbed() {}; + + void handleArgs(const int& argc, char* argv[]) {}; + + void run(); + + //void stopModule(std::list::iterator); + + //void startModule(std::string); + +}; + +Archimedes::App* MakeApp() { return new ImguiEmbed(); } diff --git a/src/example_apps/MinimalApp/MinimalApp.cpp b/src/example_apps/MinimalApp/MinimalApp.cpp index ad3d314..efe3f9d 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(dynamicLoad(s), getBlacklist()); } toOpen.clear(); }