From 28203878512ce444066ef1c3a17193f838c98e3a Mon Sep 17 00:00:00 2001 From: Nathan Date: Sun, 4 May 2025 22:51:45 -0500 Subject: [PATCH] wrap imgui impl funcs --- flake.nix | 48 +++++++++++++- include/utils/Window/WindowSDL3/WindowSDL3.h | 40 ++++++------ modules/ImguiModule/src/ImguiModule.cpp | 69 ++++---------------- modules/ImguiModule/src/ImguiModule.h | 57 ++++++++++++++++ 4 files changed, 136 insertions(+), 78 deletions(-) diff --git a/flake.nix b/flake.nix index a00f240..9e6dace 100755 --- a/flake.nix +++ b/flake.nix @@ -114,7 +114,8 @@ -DRENDERER_OPENGL \ -DWINDOW_SDL3 \ -DWINDOWMODULE_DYNAMIC \ - -lGL -lsdl -lGLEW \ + -lGL -lGLEW \ + `sdl3-config --cflags --libs` \ -Wall \ -o $name ''; @@ -161,6 +162,51 @@ }; + ImguiModuleSDL = pkgs.stdenvNoCC.mkDerivation { + + name = "ImguiModule"; + + src = ./.; + + imgui = inputs.imgui; + + nativeBuildInputs = with pkgs; [ + clang + ]; + + buildInputs = with pkgs; [ + sdl3 + glew + ]; + + buildPhase = '' + clang++ \ + modules/ImguiModule/src/*.cpp \ + modules/WindowModule/src/*.cpp \ + $imgui/backends/imgui_impl_sdl3.cpp \ + $imgui/backends/imgui_impl_opengl3.cpp \ + $imgui/misc/cpp/*.cpp \ + $imgui/*.cpp \ + -DRENDERER_OPENGL \ + -DWINDOW_SDL3 \ + -DIMGUIMODULE_DYNAMIC \ + -DCUSTOMFONT=${pkgs.fira-code}/share/fonts/truetype/FiraCode-VF.ttf \ + -fpic -shared \ + -I src -I include -I $imgui -I . \ + -lGL -lGLEW \ + `sdl3-config --cflags --libs` \ + -Wall \ + -o $name + ''; + + installPhase = '' + mkdir -p $out/bin + cp $name $out/bin + ''; + + }; + + ImguiModule = pkgs.stdenvNoCC.mkDerivation { name = "ImguiModule"; diff --git a/include/utils/Window/WindowSDL3/WindowSDL3.h b/include/utils/Window/WindowSDL3/WindowSDL3.h index 43abaae..bd553a9 100644 --- a/include/utils/Window/WindowSDL3/WindowSDL3.h +++ b/include/utils/Window/WindowSDL3/WindowSDL3.h @@ -42,16 +42,16 @@ namespace Archimedes { std::abort(); } - SDL_AddEventWatch([](void* ptr, SDL_Event* e) -> bool { + /*SDL_AddEventWatch([](void* ptr, SDL_Event* e) -> bool { - WindowData& d = (WindowData&) *ptr; + WindowData& data = (WindowData&) *ptr; switch(e->type) { } return true; - }, &data); + }, &data);*/ SDL_GL_MakeCurrent(w, gl_context); @@ -65,6 +65,8 @@ namespace Archimedes { SDL_Quit(); } + bool shouldClose() { return false; } + void doFrame() { restoreContext(); SDL_GL_SwapWindow(w); } void pollEvents() { @@ -74,53 +76,53 @@ namespace Archimedes { case SDL_EVENT_WINDOW_CLOSE_REQUESTED: case SDL_EVENT_QUIT: - d.sendEvent(new CloseWindowEvent(d.window)); + data.sendEvent(new CloseWindowEvent(data.window)); break; case SDL_EVENT_WINDOW_RESIZED: - w = e.window.data1; - h = e.window.data2; - d.sendEvent(new ResizeWindowEvent(e.window.data1, e.window.data2)); + width = e.window.data1; + height = e.window.data2; + data.sendEvent(new ResizeWindowEvent(e.window.data1, e.window.data2)); break; case SDL_EVENT_WINDOW_MOUSE_ENTER: case SDL_EVENT_WINDOW_FOCUS_GAINED: - d.sendEvent(new FocusedWindowEvent(d.window)); + data.sendEvent(new FocusedWindowEvent(data.window)); break; case SDL_EVENT_WINDOW_MOUSE_LEAVE: case SDL_EVENT_WINDOW_FOCUS_LOST: - d.sendEvent(new FocusLostWindowEvent(d.window)); + data.sendEvent(new FocusLostWindowEvent(data.window)); break; case SDL_EVENT_KEY_DOWN: - d.sendEvent(new KeyPressedWindowEvent(e.key.key, e.key.repeat ? 1 : 0)); + data.sendEvent(new KeyPressedWindowEvent(e.key.key, e.key.repeat ? 1 : 0)); break; case SDL_EVENT_KEY_UP: - d.sendEvent(new KeyReleasedWindowEvent(e.key.key)); + data.sendEvent(new KeyReleasedWindowEvent(e.key.key)); break; case SDL_EVENT_TEXT_EDITING: break; case SDL_EVENT_TEXT_INPUT: break; case SDL_EVENT_MOUSE_MOTION: - d.sendEvent(new MouseMovedWindowEvent(e.motion.x, e.motion.y)); + data.sendEvent(new MouseMovedWindowEvent(e.motion.x, e.motion.y)); break; case SDL_EVENT_MOUSE_BUTTON_DOWN: - d.sendEvent(new MouseButtonPressedWindowEvent(e.button.button)); + data.sendEvent(new MouseButtonPressedWindowEvent(e.button.button)); break; case SDL_EVENT_MOUSE_BUTTON_UP: - d.sendEvent(new MouseButtonReleasedWindowEvent(e.button.button)); + data.sendEvent(new MouseButtonReleasedWindowEvent(e.button.button)); break; case SDL_EVENT_MOUSE_WHEEL: - d.sendEvent(new ScrollWindowEvent(e.wheel.x, e.wheel.y)); + data.sendEvent(new ScrollWindowEvent(e.wheel.x, e.wheel.y)); break; default: break; } } - void restoreContext() { SDL_GLMakeCurrent(w, gl_context); } + void restoreContext() { SDL_GL_MakeCurrent(w, gl_context); } void getSize(int& w, int& h) { - w = this->w; - h = this->h; + w = this->width; + h = this->height; } SDL_Window* getWindow() { return w; } @@ -131,7 +133,7 @@ namespace Archimedes { private: SDL_Window* w; - int w = 0, h = 0; + int width = 0, height = 0; SDL_GLContext gl_context; SDL_Event e; diff --git a/modules/ImguiModule/src/ImguiModule.cpp b/modules/ImguiModule/src/ImguiModule.cpp index 5786fb7..b1f3af7 100644 --- a/modules/ImguiModule/src/ImguiModule.cpp +++ b/modules/ImguiModule/src/ImguiModule.cpp @@ -1,52 +1,5 @@ #include "ImguiModule.h" -#define RENDERER_OPENGL -#define WINDOW_GLFW - -#ifdef RENDERER_OPENGL - -#include "backends/imgui_impl_opengl3.h" - -#define RENDERER_INIT() ImGui_ImplOpenGL3_Init("#version 330") -#define RENDERER_SHUTDOWN() ImGui_ImplOpenGL3_Shutdown() - -#define RENDERER_NEWFRAME() ImGui_ImplOpenGL3_NewFrame() -#define RENDERER_RENDERDRAWDATA(x) ImGui_ImplOpenGL3_RenderDrawData(x) - -#endif - -#ifdef WINDOW_GLFW - -#include "backends/imgui_impl_glfw.h" -#include - -#ifdef RENDERER_OPENGL -#define WINDOW_INIT() ImGui_ImplGlfw_InitForOpenGL(window->getWindowImpl().getWindow(), true) -#endif - -#define WINDOW_SHUTDOWN() ImGui_ImplGlfw_Shutdown() - -#define WINDOW_NEWFRAME() ImGui_ImplGlfw_NewFrame() - -#endif - -#ifdef WINDOW_SDL3 - -#include "backends/imgui_impl_sdl3.h" -#include - -#ifdef RENDERER_OPENGL -#define WINDOW_INIT() ImGui_ImplSDL3_InitForOpenGL(window->getWindowImpl().getWindow(), window->getWindowImpl().getContext()) -#endif - -#define WINDOW_SHUTDOWN() ImGui_ImplSDL3_Shutdown() - -#define WINDOW_NEWFRAME() ImGui_ImplSDL3_NewFrame() - -#define WINDOW_PROCESSEVENT() ImGui_ImplSDL3_ProcessEvent(&e) - -#endif - #include "pch.hpp" ImguiModule::ImguiModule(Archimedes::App* a, void* h = nullptr) : Archimedes::Module(a, h) { @@ -64,8 +17,8 @@ ImguiModule::~ImguiModule() { wm->getRenderer()->getCmdList().erase(rcmd_it); - RENDERER_SHUTDOWN(); - WINDOW_SHUTDOWN(); + rendererShutdown(); + windowShutdown(); ImGui::DestroyContext(); wm->releaseWindow(window); @@ -100,28 +53,28 @@ void ImguiModule::onLoad() { //ImGui::StyleColorsLight(); // Setup Platform/Renderer backends - if(!WINDOW_INIT()) - std::cout << "WINDOW_INIT failed\n"; + if(!windowInit()) + std::cout << "windowInit failed\n"; - if(!RENDERER_INIT()) { - std::cout << "RENDERER_INIT failed!\n" << std::endl; + if(!rendererInit()) { + std::cout << "rendererInit failed!\n" << std::endl; } wm->getRenderer()->getCmdList().push_back([](){ ImGui::Render(); - RENDERER_RENDERDRAWDATA(ImGui::GetDrawData()); + rendererRenderDrawData(); - RENDERER_NEWFRAME(); - WINDOW_NEWFRAME(); + rendererNewFrame(); + windowNewFrame(); ImGui::NewFrame(); }); rcmd_it = --wm->getRenderer()->getCmdList().end()++; //Compute first frame ahead of first WindowModule->run() - RENDERER_NEWFRAME(); - WINDOW_NEWFRAME(); + rendererNewFrame(); + windowNewFrame(); ImGui::NewFrame(); } diff --git a/modules/ImguiModule/src/ImguiModule.h b/modules/ImguiModule/src/ImguiModule.h index 8d42c8d..46a0ca1 100644 --- a/modules/ImguiModule/src/ImguiModule.h +++ b/modules/ImguiModule/src/ImguiModule.h @@ -9,6 +9,26 @@ #include "modules/WindowModule/src/WindowModule.h" +#ifdef RENDERER_OPENGL + +#include "backends/imgui_impl_opengl3.h" + +#endif + +#ifdef WINDOW_GLFW + +#include "backends/imgui_impl_glfw.h" +#include + +#endif + +#ifdef WINDOW_SDL3 + +#include "backends/imgui_impl_sdl3.h" +#include + +#endif + class ImguiModule : public Archimedes::Module { public: @@ -44,9 +64,46 @@ class ImguiModule : public Archimedes::Module { Archimedes::Window* window; std::list>::iterator rcmd_it; + +#ifdef RENDERER_OPENGL + auto rendererInit() { return ImGui_ImplOpenGL3_Init("#version 330"); } + void rendererShutdown() { ImGui_ImplOpenGL3_Shutdown(); } + + void rendererNewFrame() { ImGui_ImplOpenGL3_NewFrame(); } + void rendererRenderDrawData() { ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } + +#endif + +#ifdef WINDOW_GLFW +#ifdef RENDERER_OPENGL + auto windowInit(Archimedes::Window* window) { return ImGui_ImplGlfw_InitForOpenGL(window->getWindowImpl().getWindow(), true); } +#endif + + void windowShutdown() { ImGui_ImplGlfw_Shutdown(); } + + void windowNewFrame() { ImGui_ImplGlfw_NewFrame(); } + +#endif + +#ifdef WINDOW_SDL3 +#ifdef RENDERER_OPENGL + auto windowInit(Archimedes::Window* window) { return ImGui_ImplSDL3_InitForOpenGL(window->getWindowImpl().getWindow(), window->getWindowImpl().getContext()); } +#endif + +#define windowShutdown() ImGui_ImplSDL3_Shutdown() + void windowShutdown() { ImGui_ImplSDL3_Shutdown(); } + +#define windowNewFrame() ImGui_ImplSDL3_NewFrame() + void windowNewFrame() { ImGui_ImplSDL3_NewFrame(); } + + void windowProcessEvent(SDL_Event e) { ImGui_ImplSDL3_ProcessEvent(&e); } + +#endif }; #ifdef IMGUIMODULE_DYNAMIC #define MODULE_TYPE ImguiModule #include "endModule.h" #endif + +