heavy refactoring

This commit is contained in:
2025-04-02 11:50:32 -05:00
parent c3685d2b30
commit 44ef73395c
18 changed files with 132 additions and 93 deletions

View File

@@ -57,6 +57,7 @@
-DWINDOW_GLFW \
-DGUIMODULE \
-DTESTIMGUI_STATIC \
-DWINDOWMODULE_STATIC \
-I src -I include -I $imgui -I . \
-lGL -lglfw -lGLEW \
-Wall \

View File

@@ -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

View File

@@ -42,7 +42,7 @@
buildPhase = ''
clang++ \
modules/Window/src/*.cpp \
modules/WindowModule/src/*.cpp \
-fpic -shared \
-I src -I include \
-DRENDERER_OPENGL \

View File

@@ -7,8 +7,9 @@
#include <array>
#include <vector>
#include <variant>
#include <any>
#include <unordered_map>
#include <functional>
#include <optional>
//#include <chrono>
//#include <thread>

View File

@@ -49,7 +49,9 @@ namespace Archimedes {
bool done = false;
std::list<Module*> modules;
std::unordered_map<std::string, Module*> modules;
std::list<std::string> runOrder;
std::list<Module*> toClose;
std::list<std::string> toOpen;
@@ -73,53 +75,47 @@ namespace Archimedes {
return create(h, Get());
}
virtual std::list<Module*>::iterator load(std::string modulePath, std::list<std::string> blacklist = {}) {
virtual Module* load(std::string modulePath, std::list<std::string>::iterator ins) {
Module* m = dynamicLoad(modulePath);
return load(m, blacklist);
return load(m, ins);
}
virtual std::list<Module*>::iterator load(Module* m, std::list<std::string> blacklist = {}) {
virtual Module* load(Module* m, std::list<std::string>::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<std::string>(it->second))
m->depsInstances[it->first] = load(std::get<std::string>(it->second), blacklist);
if(std::holds_alternative<std::string>(m->deps[*it]))
m->depsInstances[*it] = load(std::get<std::string>(m->deps[*it]), ins);
else
m->depsInstances[it->first] = load(std::get<Module*>(it->second), blacklist);
m->depsInstances[*it] = load(std::get<Module*>(m->deps[*it]), ins);
}
}
modules.push_back(m);
m->setSelf(--modules.end());
modules.end()++;
return m->self;
return m;
}
virtual void unload(std::list<Module*>::iterator it) {
@@ -135,13 +131,6 @@ namespace Archimedes {
virtual void printHelp() = 0;
std::list<std::string> getBlacklist() {
std::list<std::string> l;
for(Module* m : modules)
l.push_back(m->getName());
return l;
}
};
}

View File

@@ -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;
};
}

View File

@@ -25,17 +25,18 @@ namespace Archimedes {
std::string getName() const { return name; }
void* getHandle() { return handle; }
void setSelf(std::list<Module*>::iterator s) { self = s; }
std::any getData(std::string s) { return data[s.c_str()]; };
protected:
std::string name;
void* handle;
std::list<Module*>::iterator self;
App* app;
std::unordered_map<std::string, std::variant<std::string, Module*>> deps;
std::unordered_map<std::string, std::list<Module*>::iterator> depsInstances;
std::unordered_map<std::string, Module*> depsInstances;
std::unordered_map<std::string, std::any> data;
};
}

View File

@@ -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<renderCmd*>::iterator addRenderCmd(renderCmd* cmd) {
auto it = rc.end();
rc.push_back(cmd);
return it;
}
void removeRenderCmd(std::list<renderCmd*>::iterator cmd) {
rc.erase(cmd);
std::list<renderCmd*>& getCmdList() {
return rc;
}
private:

View File

@@ -17,7 +17,9 @@ namespace Archimedes {
RendererOpenGL() {};
~RendererOpenGL() {};
void init() { glewInit(); };
bool init() {
return glewInit() == GLEW_OK;
};
void render(std::list<renderCmd*> cmdList, int& w, int& h) {

View File

@@ -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; }

View File

@@ -31,6 +31,7 @@ namespace Archimedes {
std::cout << "Window Created!\n";
glfwMakeContextCurrent(w);
glfwSwapInterval(1);
}
~WindowGLFW() {

View File

@@ -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->init();
renderer = window->getRenderer();
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_cast<decltype(window->getWindowImpl().getWindow())>(data["window"]) << std::endl;
}
void WindowModule::run() {

View File

@@ -1,14 +1,24 @@
#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();
@@ -17,20 +27,11 @@ class WindowModule : public Archimedes::Module {
void onLoad();
//interface for other modules
std::list<Archimedes::Renderer::renderCmd*>::iterator addRenderCmd(Archimedes::Renderer::renderCmd* cmd) {
return renderer->addRenderCmd(cmd);
}
void removeRenderCmd(std::list<Archimedes::Renderer::renderCmd*>::iterator cmd) {
renderer->removeRenderCmd(cmd);
}
auto getWindowImpl() { return window->getWindowImpl(); }
private:
Archimedes::Window* window;
Archimedes::Renderer* renderer;
private:
};

View File

@@ -12,7 +12,10 @@ TestImgui::TestImgui(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a)
}
TestImgui::~TestImgui() {
windowModule->removeRenderCmd(rcmd);
std::list<Archimedes::Renderer::renderCmd*>* cmdList =
std::any_cast<std::list<Archimedes::Renderer::renderCmd*>*>(depsInstances["WindowModule"]->getData("renderCmdList"));
cmdList->erase(rcmd);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
@@ -20,6 +23,13 @@ TestImgui::~TestImgui() {
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");
std::cout << wm->getName() << std::endl;
GLFWwindow* w = std::any_cast<GLFWwindow*>(wm->getData("window"));
rcmd = windowModule->addRenderCmd([](){
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
std::cout << "TestImgui GLFWwindow*: " << w << std::endl;
/*if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}*/
});
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<Archimedes::Renderer::renderCmd*>* cmdList =
std::any_cast<std::list<Archimedes::Renderer::renderCmd*>*>(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();
}

View File

@@ -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<Archimedes::Renderer::renderCmd*>::iterator rcmd;
Archimedes::Window* window;
};
#ifndef TESTIMGUI_STATIC

View File

@@ -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();
}

View File

@@ -18,7 +18,7 @@ void MinimalApp::run() {
toClose.clear();
for(std::string s : toOpen) {
load(s, getBlacklist());
load(s, modules.begin());
}
toOpen.clear();
}

View File

@@ -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();