diff --git a/modules/GUImodules/TestImgui/src/TestImgui.cpp b/modules/GUImodules/TestImgui/src/TestImgui.cpp index 99728ac..75bcdc9 100644 --- a/modules/GUImodules/TestImgui/src/TestImgui.cpp +++ b/modules/GUImodules/TestImgui/src/TestImgui.cpp @@ -1,5 +1,49 @@ #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 + +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()); + }); + +} diff --git a/modules/GUImodules/TestImgui/src/TestImgui.h b/modules/GUImodules/TestImgui/src/TestImgui.h index c21ac02..c191de1 100644 --- a/modules/GUImodules/TestImgui/src/TestImgui.h +++ b/modules/GUImodules/TestImgui/src/TestImgui.h @@ -1,38 +1,24 @@ #include "Archimedes.h" -#include -#include -#include -#include - -class TestImgui : public Module { +class TestImgui : public Archimedes::GuiModule { public: - TestImgui(void* h, App& a) : Module(h, a) { + TestImgui(void*, Archimedes::App&); - std::cout << "Enter path to Window library: "; - std::string path; - std::cin.ignore(); - std::getline(std::cin, path); - - deps["Window"] = path; - name = "TestImgui"; - } + ~TestImgui(); - ~TestImgui() {} - - void load(); + void onLoad(); void run(); private: - + bool demo = true; }; extern "C" { - Module* create(void* handle, App& app) { + Archimedes::Module* create(void* handle, Archimedes::App& app) { return new TestImgui(handle, app); } } diff --git a/src/App.cpp b/src/App.cpp index 16576ad..7cc6e5e 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -1,160 +1,164 @@ #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; - - handleArgs(argc, argv); - -} - -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 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; + 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); + } } - 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 blacklist = {}) { - 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); + + 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; } - } - 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; + Module::create_t* create = (Module::create_t*) dlsym(h, "create"); + + if(dlerror()) { + std::cout << "error finding create function in file: " << lib << std::endl; } - if(skip) { - skip = false; - continue; - } else { - load(it->second, blacklist); + + Module* m = create(h, App::Get()); + + 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); + 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::iterator it) { - modules.push_back(m); - m->setSelf(--modules.end()); - modules.end()++; + Module* m = *it; + void* h = m->getHandle(); - return true; -} - -void App::unload(std::list::iterator it) { + modules.erase(m->self); + delete m; - Module* m = *it; - void* h = m->getHandle(); + dlclose(h); - modules.erase(m->self); - delete m; - - dlclose(h); - -} - -void App::stopModule(std::list::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) { - if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + + void App::stopModule(std::list::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(); - } 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) { - std::cout << "Attempting to load: " << argv[i] << std::endl; - load(argv[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(); + } - 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(); - } - -} diff --git a/src/App.h b/src/App.h index 53d6fcf..577495a 100644 --- a/src/App.h +++ b/src/App.h @@ -5,44 +5,48 @@ #include "Module.h" #include "GuiModule.h" -class App { +namespace Archimedes { - private: - static App* instance; + class App { - bool done = false; + private: + static App* instance; - std::list modules; - std::list toClose; - std::list toOpen; - - bool load(std::string, std::list); - void unload(std::list::iterator); - - void handleArgs(const int&, char*[]); + bool done = false; - void printHelp(); + std::list modules; + std::list toClose; + std::list toOpen; - std::list getBlacklist() { - std::list l; - for(Module* m : modules) - l.push_back(m->getName()); - return l; - } + bool load(std::string, std::list); + void unload(std::list::iterator); - public: - App(const int&, char*[]); - ~App(); + void handleArgs(const int&, char*[]); - static App& Get() { return *instance; } + void printHelp(); - void run(); + std::list getBlacklist() { + std::list l; + for(Module* m : modules) + l.push_back(m->getName()); + return l; + } - void stopModule(std::list::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::iterator); + + void startModule(std::string); + + void end() { done = true; } + }; + +} #endif diff --git a/src/GuiModule.h b/src/GuiModule.h index b799f01..e2b2c5d 100644 --- a/src/GuiModule.h +++ b/src/GuiModule.h @@ -4,21 +4,24 @@ #include "Module.h" #include "Window/Window.h" -class GuiModule : Module { +namespace Archimedes { - public: - typedef GuiModule* create_t(); + class GuiModule : public Module { - GuiModule(void* h, App& a) : Module(h, a) { - deps["Window"] = ""; - } - virtual ~GuiModule() {} - virtual void load() = 0; - virtual void run() = 0; + public: + typedef GuiModule* create_t(void*, App&); - private: - Window window; -}; + GuiModule(void* h, App& a) : Module(h, a) {} + virtual ~GuiModule() {} + virtual void onLoad() = 0; + virtual void run() = 0; + + Window* getWindow() { return window; } + + private: + Window* window; + }; +} #endif diff --git a/src/Module.h b/src/Module.h index 4de9f16..dd69e93 100644 --- a/src/Module.h +++ b/src/Module.h @@ -3,13 +3,15 @@ #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&); Module(void* h, App& a) : handle(h), app(a) {}; @@ -21,8 +23,8 @@ class Module { void* getHandle() { return handle; } void setSelf(std::list::iterator s) { self = s; } - - protected: + + protected: std::string name; void* handle; std::list::iterator self; @@ -30,6 +32,7 @@ class Module { App& app; std::unordered_map deps; -}; + }; +} #endif diff --git a/src/main.cpp b/src/main.cpp index 307cef6..3e65ab5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,6 @@ #include "App.h" int main(int argc, char* argv[]) { - App app(argc, argv); + Archimedes::App app(argc, argv); app.run(); } diff --git a/utils/Renderer/Renderer.cpp b/utils/Renderer/Renderer.cpp index e69de29..1ac91e1 100644 --- a/utils/Renderer/Renderer.cpp +++ b/utils/Renderer/Renderer.cpp @@ -0,0 +1,8 @@ +#include "Renderer.h" + +namespace Archimedes { + + void Renderer::render() { + r.render(rc, w, h); + } +} diff --git a/utils/Renderer/Renderer.h b/utils/Renderer/Renderer.h index a8b1242..c6dcba8 100644 --- a/utils/Renderer/Renderer.h +++ b/utils/Renderer/Renderer.h @@ -1,9 +1,16 @@ +#ifndef RENDERER +#define RENDERER + #include "Archimedes.h" +#define RENDERER_OPENGL +#include "RendererOpenGL/RendererOpenGL.h" + namespace Archimedes { class Renderer { public: + int w, h; typedef void renderCmd(); ~Renderer() {} @@ -14,6 +21,8 @@ namespace Archimedes { private: std::list rc; + RendererImpl r; }; } +#endif diff --git a/utils/Renderer/RendererOpenGL/RendererOpenGL.cpp b/utils/Renderer/RendererOpenGL/RendererOpenGL.cpp index e69de29..5eee66f 100644 --- a/utils/Renderer/RendererOpenGL/RendererOpenGL.cpp +++ b/utils/Renderer/RendererOpenGL/RendererOpenGL.cpp @@ -0,0 +1,23 @@ +#define RENDERER_OPENGL +#include "RendererOpenGL.h" + +#define GLEW_STATIC +#include + +namespace Archimedes { + + RendererOpenGL::RendererOpenGL() {} + + RendererOpenGL::~RendererOpenGL() {} + + void RendererOpenGL::render(std::list cmdList, int& w, int& h) { + + glViewport(0, 0, &w, &h); + + glClear(GL_COLOR_BUFFER_BIT); + + for(auto* f : cmdList) + f(); + cmdList.clear(); + } +} diff --git a/utils/Renderer/RendererOpenGL/RendererOpenGL.h b/utils/Renderer/RendererOpenGL/RendererOpenGL.h index c9303d7..e298a3c 100644 --- a/utils/Renderer/RendererOpenGL/RendererOpenGL.h +++ b/utils/Renderer/RendererOpenGL/RendererOpenGL.h @@ -1,15 +1,22 @@ -#include "Renderer/Renderer.h" +#ifdef RENDERER_OPENGL +#undef RENDERER_OPENGL + +#include "Archimedes.h" namespace Archimedes { class RendererOpenGL { public: + typedef void renderCmd(); + RendererOpenGL(); ~RendererOpenGL(); - void render(); + void render(std::list, int&, int&); }; typedef RendererOpenGL RendererImpl; } + +#endif diff --git a/utils/Window/Window.cpp b/utils/Window/Window.cpp index ab55ba0..435511b 100644 --- a/utils/Window/Window.cpp +++ b/utils/Window/Window.cpp @@ -1 +1,13 @@ #include "Window.h" + +namespace Archimedes { + + void Window::doFrame() { + window.getSize(renderer.w, renderer.h); + + renderer.render(); + + window.doFrame(); + } + +} diff --git a/utils/Window/Window.h b/utils/Window/Window.h index 03733ad..6922045 100644 --- a/utils/Window/Window.h +++ b/utils/Window/Window.h @@ -1,6 +1,6 @@ #include "Archimedes.h" #include "Renderer/Renderer.h" -#include "GLFW/WindowGLFW.h" +#include "WindowGLFW/WindowGLFW.h" namespace Archimedes { @@ -10,13 +10,16 @@ namespace Archimedes { ~Window() {}; - bool shouldClose(); + bool shouldClose() { return window.shouldClose(); } void doFrame(); + Renderer& getRenderer() { return renderer; } + WindowImpl& getWindowImpl() { return window; } + private: Renderer renderer; - WindowImpl w; + WindowImpl window; }; } diff --git a/utils/Window/GLFW/WindowGLFW.cpp b/utils/Window/WindowGLFW/WindowGLFW.cpp similarity index 95% rename from utils/Window/GLFW/WindowGLFW.cpp rename to utils/Window/WindowGLFW/WindowGLFW.cpp index cff6f6d..d15b4eb 100644 --- a/utils/Window/GLFW/WindowGLFW.cpp +++ b/utils/Window/WindowGLFW/WindowGLFW.cpp @@ -19,6 +19,7 @@ namespace Archimedes { } glfwMakeContextCurrent(w); + glfwSwapInterval(1); } bool WindowGLFW::shouldClose() { @@ -26,9 +27,8 @@ namespace Archimedes { } void WindowGLFW::doFrame() { - glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(w); glfwPollEvents(); + glfwSwapBuffers(w); } WindowGLFW::~WindowGLFW() { diff --git a/utils/Window/GLFW/WindowGLFW.h b/utils/Window/WindowGLFW/WindowGLFW.h similarity index 79% rename from utils/Window/GLFW/WindowGLFW.h rename to utils/Window/WindowGLFW/WindowGLFW.h index 587f665..047f4d6 100644 --- a/utils/Window/GLFW/WindowGLFW.h +++ b/utils/Window/WindowGLFW/WindowGLFW.h @@ -15,6 +15,10 @@ namespace Archimedes { void doFrame(); + void getSize(int&, int&); + + auto getWindow() { return w; } + private: GLFWwindow* w; };