the gui problem^TM

This commit is contained in:
2025-03-21 16:16:25 -05:00
parent 7900def444
commit 6b8861a7fb
15 changed files with 316 additions and 206 deletions

View File

@@ -1,5 +1,49 @@
#include "TestImgui.h" #include "TestImgui.h"
void TestImgui::load() {} #include "imgui/imgui.h"
#include "imgui/backends/imgui_impl_glfw.h"
#include "imgui/backends/imgui_impl_opengl3.h"
void TestImgui::run() {} #include <GLFW/glfw3.h>
TestImgui::TestImgui(void* h, Archimedes::App& a) : Archimedes::GuiModule(h, a) {
name = "TestImgui";
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(getWindow()->getWindowImpl().getWindow(), true);
ImGui_ImplOpenGL3_Init("#version 130");
}
TestImgui::~TestImgui() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void TestImgui::onLoad() {}
void TestImgui::run() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow(&this->demo);
ImGui::Render();
getWindow()->getRenderer().addRenderCmd([](){
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
});
}

View File

@@ -1,38 +1,24 @@
#include "Archimedes.h" #include "Archimedes.h"
#include <imgui.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h>
#include <GLFW/glfw3.h> class TestImgui : public Archimedes::GuiModule {
class TestImgui : public Module {
public: public:
TestImgui(void* h, App& a) : Module(h, a) { TestImgui(void*, Archimedes::App&);
std::cout << "Enter path to Window library: "; ~TestImgui();
std::string path;
std::cin.ignore();
std::getline(std::cin, path);
deps["Window"] = path;
name = "TestImgui";
}
~TestImgui() {} void onLoad();
void load();
void run(); void run();
private: private:
bool demo = true;
}; };
extern "C" { extern "C" {
Module* create(void* handle, App& app) { Archimedes::Module* create(void* handle, Archimedes::App& app) {
return new TestImgui(handle, app); return new TestImgui(handle, app);
} }
} }

View File

@@ -1,160 +1,164 @@
#include "App.h" #include "App.h"
App* App::instance = nullptr; Archimedes::App* Archimedes::App::instance = nullptr;
App::App(const int& argc, char* argv[]) { namespace Archimedes {
App::App(const int& argc, char* argv[]) {
if(instance != nullptr) {
std::cout << "App already exists\nThere can only be one!\n";
std::abort();
}
std::cout << "Initializing...\n";
instance = this;
handleArgs(argc, argv);
if(instance != nullptr) {
std::cout << "App already exists\nThere can only be one!\n";
std::abort();
} }
std::cout << "Initializing...\n"; App::~App() {
instance = this; std::cout << "\nExiting...\n";
for(auto it = modules.begin(); it != modules.end(); it++) {
handleArgs(argc, argv); void* handle = (*it)->getHandle();
delete *it;
} dlclose(handle);
it = modules.erase(it);
App::~App() { }
std::cout << "\nExiting...\n";
for(auto it = modules.begin(); it != modules.end(); it++) {
void* handle = (*it)->getHandle();
delete *it;
dlclose(handle);
it = modules.erase(it);
}
}
bool App::load(std::string lib, std::list<std::string> blacklist = {}) {
std::cout << "print twice!\n";
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;
} }
Module::create_t* create = (Module::create_t*) dlsym(h, "create");
if(dlerror()) {
std::cout << "error finding create function in file: " << lib << std::endl;
}
Module* m = create(h, App::Get()); bool App::load(std::string lib, std::list<std::string> blacklist = {}) {
for(auto it = blacklist.begin(); it != blacklist.end(); it++) {
if(*it == m->getName()) { std::cout << "print twice!\n";
std::cout << "Module \"" << *it << "\" is already loaded!\n"; void* h = dlopen(lib.c_str(), RTLD_NOW);
delete m;
dlclose(h); if(!h) {
std::cout << "could not open lib at: \"" << lib.c_str() << "\"\nError: " << dlerror() << std::endl;
return false; return false;
} }
}
blacklist.push_back(m->getName()); Module::create_t* create = (Module::create_t*) dlsym(h, "create");
bool skip = false; if(dlerror()) {
for(auto it = m->deps.begin(); it != m->deps.end(); it++) { std::cout << "error finding create function in file: " << lib << std::endl;
for(std::string s : blacklist) {
if(it->first == s)
skip = true;
} }
if(skip) {
skip = false; Module* m = create(h, App::Get());
continue;
} else { for(auto it = blacklist.begin(); it != blacklist.end(); it++) {
load(it->second, blacklist); if(*it == m->getName()) {
std::cout << "Module \"" << *it << "\" is already loaded!\n";
delete m;
dlclose(h);
return false;
}
} }
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 {
load(it->second, blacklist);
}
}
modules.push_back(m);
m->setSelf(--modules.end());
modules.end()++;
return true;
} }
void App::unload(std::list<Module*>::iterator it) {
modules.push_back(m); Module* m = *it;
m->setSelf(--modules.end()); void* h = m->getHandle();
modules.end()++;
return true; modules.erase(m->self);
} delete m;
void App::unload(std::list<Module*>::iterator it) {
Module* m = *it; dlclose(h);
void* h = m->getHandle();
modules.erase(m->self);
delete m;
dlclose(h);
}
void App::stopModule(std::list<Module*>::iterator it) {
toClose.push_back(*it);
}
void App::startModule(std::string lib) {
toOpen.push_back(lib);
}
void App::handleArgs(const int& argc, char* argv[]) {
int i = 1;
if(argc == 1) {
printHelp();
end();
} }
while(i < argc) { void App::stopModule(std::list<Module*>::iterator it) {
if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { toClose.push_back(*it);
}
void App::startModule(std::string lib) {
toOpen.push_back(lib);
}
void App::handleArgs(const int& argc, char* argv[]) {
int i = 1;
if(argc == 1) {
printHelp(); printHelp();
end(); end();
} else {
break;
} }
i++;
while(i < argc) {
if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
printHelp();
end();
} else {
break;
}
i++;
}
while(i < argc) {
std::cout << "Attempting to load: " << argv[i] << std::endl;
load(argv[i]);
i++;
}
} }
while(i < argc) { void App::printHelp() {
std::cout << "Attempting to load: " << argv[i] << std::endl; std::cout << "archimedes [ -h | --help ] /path/to/some/library.so\n";
load(argv[i]); }
void App::run() {
std::cout << "\nTesting...\n";
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(s, getBlacklist());
}
toOpen.clear();
}
i++;
} }
} }
void App::printHelp() {
std::cout << "archimedes [ -h | --help ] /path/to/some/library.so\n";
}
void App::run() {
std::cout << "\nTesting...\n";
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(s, getBlacklist());
}
toOpen.clear();
}
}

View File

@@ -5,44 +5,48 @@
#include "Module.h" #include "Module.h"
#include "GuiModule.h" #include "GuiModule.h"
class App { namespace Archimedes {
private: class App {
static App* instance;
bool done = false; private:
static App* instance;
std::list<Module*> modules; bool done = false;
std::list<Module*> toClose;
std::list<std::string> toOpen;
bool load(std::string, std::list<std::string>);
void unload(std::list<Module*>::iterator);
void handleArgs(const int&, char*[]);
void printHelp(); std::list<Module*> modules;
std::list<Module*> toClose;
std::list<std::string> toOpen;
std::list<std::string> getBlacklist() { bool load(std::string, std::list<std::string>);
std::list<std::string> l; void unload(std::list<Module*>::iterator);
for(Module* m : modules)
l.push_back(m->getName());
return l;
}
public: void handleArgs(const int&, char*[]);
App(const int&, char*[]);
~App();
static App& Get() { return *instance; } void printHelp();
void run(); std::list<std::string> getBlacklist() {
std::list<std::string> l;
for(Module* m : modules)
l.push_back(m->getName());
return l;
}
void stopModule(std::list<Module*>::iterator); public:
App(const int&, char*[]);
~App();
void startModule(std::string); static App& Get() { return *instance; }
void end() { done = true; } void run();
};
void stopModule(std::list<Module*>::iterator);
void startModule(std::string);
void end() { done = true; }
};
}
#endif #endif

View File

@@ -4,21 +4,24 @@
#include "Module.h" #include "Module.h"
#include "Window/Window.h" #include "Window/Window.h"
class GuiModule : Module { namespace Archimedes {
public: class GuiModule : public Module {
typedef GuiModule* create_t();
GuiModule(void* h, App& a) : Module(h, a) { public:
deps["Window"] = ""; typedef GuiModule* create_t(void*, App&);
}
virtual ~GuiModule() {}
virtual void load() = 0;
virtual void run() = 0;
private: GuiModule(void* h, App& a) : Module(h, a) {}
Window window; virtual ~GuiModule() {}
}; virtual void onLoad() = 0;
virtual void run() = 0;
Window* getWindow() { return window; }
private:
Window* window;
};
}
#endif #endif

View File

@@ -3,13 +3,15 @@
#include "pch.hpp" #include "pch.hpp"
class App; namespace Archimedes {
class Module { class App;
friend class App; class Module {
public: friend class App;
public:
typedef Module* create_t(void*, App&); typedef Module* create_t(void*, App&);
Module(void* h, App& a) : handle(h), app(a) {}; Module(void* h, App& a) : handle(h), app(a) {};
@@ -21,8 +23,8 @@ class Module {
void* getHandle() { return handle; } void* getHandle() { return handle; }
void setSelf(std::list<Module*>::iterator s) { self = s; } void setSelf(std::list<Module*>::iterator s) { self = s; }
protected: protected:
std::string name; std::string name;
void* handle; void* handle;
std::list<Module*>::iterator self; std::list<Module*>::iterator self;
@@ -30,6 +32,7 @@ class Module {
App& app; App& app;
std::unordered_map<std::string, std::string> deps; std::unordered_map<std::string, std::string> deps;
}; };
}
#endif #endif

View File

@@ -2,6 +2,6 @@
#include "App.h" #include "App.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
App app(argc, argv); Archimedes::App app(argc, argv);
app.run(); app.run();
} }

View File

@@ -0,0 +1,8 @@
#include "Renderer.h"
namespace Archimedes {
void Renderer::render() {
r.render(rc, w, h);
}
}

View File

@@ -1,9 +1,16 @@
#ifndef RENDERER
#define RENDERER
#include "Archimedes.h" #include "Archimedes.h"
#define RENDERER_OPENGL
#include "RendererOpenGL/RendererOpenGL.h"
namespace Archimedes { namespace Archimedes {
class Renderer { class Renderer {
public: public:
int w, h;
typedef void renderCmd(); typedef void renderCmd();
~Renderer() {} ~Renderer() {}
@@ -14,6 +21,8 @@ namespace Archimedes {
private: private:
std::list<renderCmd*> rc; std::list<renderCmd*> rc;
RendererImpl r;
}; };
} }
#endif

View File

@@ -0,0 +1,23 @@
#define RENDERER_OPENGL
#include "RendererOpenGL.h"
#define GLEW_STATIC
#include <GL/glew.h>
namespace Archimedes {
RendererOpenGL::RendererOpenGL() {}
RendererOpenGL::~RendererOpenGL() {}
void RendererOpenGL::render(std::list<renderCmd*> cmdList, int& w, int& h) {
glViewport(0, 0, &w, &h);
glClear(GL_COLOR_BUFFER_BIT);
for(auto* f : cmdList)
f();
cmdList.clear();
}
}

View File

@@ -1,15 +1,22 @@
#include "Renderer/Renderer.h" #ifdef RENDERER_OPENGL
#undef RENDERER_OPENGL
#include "Archimedes.h"
namespace Archimedes { namespace Archimedes {
class RendererOpenGL { class RendererOpenGL {
public: public:
typedef void renderCmd();
RendererOpenGL(); RendererOpenGL();
~RendererOpenGL(); ~RendererOpenGL();
void render(); void render(std::list<renderCmd*>, int&, int&);
}; };
typedef RendererOpenGL RendererImpl; typedef RendererOpenGL RendererImpl;
} }
#endif

View File

@@ -1 +1,13 @@
#include "Window.h" #include "Window.h"
namespace Archimedes {
void Window::doFrame() {
window.getSize(renderer.w, renderer.h);
renderer.render();
window.doFrame();
}
}

View File

@@ -1,6 +1,6 @@
#include "Archimedes.h" #include "Archimedes.h"
#include "Renderer/Renderer.h" #include "Renderer/Renderer.h"
#include "GLFW/WindowGLFW.h" #include "WindowGLFW/WindowGLFW.h"
namespace Archimedes { namespace Archimedes {
@@ -10,13 +10,16 @@ namespace Archimedes {
~Window() {}; ~Window() {};
bool shouldClose(); bool shouldClose() { return window.shouldClose(); }
void doFrame(); void doFrame();
Renderer& getRenderer() { return renderer; }
WindowImpl& getWindowImpl() { return window; }
private: private:
Renderer renderer; Renderer renderer;
WindowImpl w; WindowImpl window;
}; };
} }

View File

@@ -19,6 +19,7 @@ namespace Archimedes {
} }
glfwMakeContextCurrent(w); glfwMakeContextCurrent(w);
glfwSwapInterval(1);
} }
bool WindowGLFW::shouldClose() { bool WindowGLFW::shouldClose() {
@@ -26,9 +27,8 @@ namespace Archimedes {
} }
void WindowGLFW::doFrame() { void WindowGLFW::doFrame() {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(w);
glfwPollEvents(); glfwPollEvents();
glfwSwapBuffers(w);
} }
WindowGLFW::~WindowGLFW() { WindowGLFW::~WindowGLFW() {

View File

@@ -15,6 +15,10 @@ namespace Archimedes {
void doFrame(); void doFrame();
void getSize(int&, int&);
auto getWindow() { return w; }
private: private:
GLFWwindow* w; GLFWwindow* w;
}; };