From f35c58d3d6a911cba586251df7aeed2e344b5e58 Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 5 May 2025 13:17:38 -0500 Subject: [PATCH] use std::any --- flake.nix | 124 +++++++------------ include/pch.hpp | 3 +- include/utils/App/App.h | 4 + include/utils/Events/BasicEvents.h | 30 +++-- include/utils/Events/Event.h | 5 +- include/utils/Window/WindowEvents.h | 50 +++++--- include/utils/Window/WindowSDL3/WindowSDL3.h | 88 +++++++++---- modules/ImguiModule/src/ImguiModule.cpp | 18 +-- src/example_apps/MinimalApp/MinimalApp.cpp | 2 + 9 files changed, 171 insertions(+), 153 deletions(-) diff --git a/flake.nix b/flake.nix index 11452ee..91beb1a 100755 --- a/flake.nix +++ b/flake.nix @@ -91,42 +91,6 @@ }; - WindowModuleSDL = pkgs.stdenvNoCC.mkDerivation { - - name = "WindowModule"; - - src = ./.; - - nativeBuildInputs = with pkgs; [ - clang - ]; - - buildInputs = with pkgs; [ - sdl3 - glew - ]; - - buildPhase = '' - clang++ \ - modules/WindowModule/src/*.cpp \ - -fpic -shared \ - -I src -I include \ - -DRENDERER=1 \ - -DWINDOW=2 \ - -DWINDOWMODULE_DYNAMIC \ - -lGL -lGLEW \ - -lSDL3 \ - -Wall \ - -o $name - ''; - - installPhase = '' - mkdir -p $out/bin - cp $name $out/bin - ''; - - }; - WindowModule = pkgs.stdenvNoCC.mkDerivation { name = "WindowModule"; @@ -162,51 +126,6 @@ }; - 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=1 \ - -DWINDOW=2 \ - -DIMGUIMODULE_DYNAMIC \ - -DCUSTOMFONT=${pkgs.fira-code}/share/fonts/truetype/FiraCode-VF.ttf \ - -fpic -shared \ - -I src -I include -I ${imgui} -I . \ - -lGL -lGLEW \ - -lSDL3 \ - -Wall \ - -o $name - ''; - - installPhase = '' - mkdir -p $out/bin - cp $name $out/bin - ''; - - }; - - ImguiModule = pkgs.stdenvNoCC.mkDerivation { name = "ImguiModule"; @@ -249,6 +168,49 @@ ''; }; + MainGUIsdl = pkgs.stdenvNoCC.mkDerivation { + + name = "MainGUI"; + + src = ./.; + + inherit imgui; + + nativeBuildInputs = with pkgs; [ + clang + ]; + + buildInputs = with pkgs; [ + sdl3 + glew + ]; + + buildPhase = '' + clang++ \ + modules/MainGUI/src/*.cpp \ + 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=1 \ + -DWINDOW=2 \ + -DMAINGUI_DYNAMIC \ + -fpic -shared \ + -I src -I include -I $imgui -I . \ + -lGL -lSDL3 -lGLEW \ + -Wall \ + -o $name + ''; + + installPhase = '' + mkdir -p $out/bin + cp $name $out/bin + ''; + + }; + MainGUI = pkgs.stdenvNoCC.mkDerivation { diff --git a/include/pch.hpp b/include/pch.hpp index d89cc5c..d20cb7d 100644 --- a/include/pch.hpp +++ b/include/pch.hpp @@ -8,7 +8,8 @@ #include #include #include - +#include +#include #include #include #include diff --git a/include/utils/App/App.h b/include/utils/App/App.h index 5bcc6d6..79eb210 100644 --- a/include/utils/App/App.h +++ b/include/utils/App/App.h @@ -17,6 +17,8 @@ namespace Archimedes { } instance = this; roInsert = runOrder.begin(); + + registerEvent(AnonymousEvent()); registerEvent(DoLoadModuleEvent()); registerEvent(DoUnloadModuleEvent()); @@ -27,6 +29,8 @@ namespace Archimedes { virtual ~App() { + unregisterEvent(AnonymousEvent()); + unregisterEvent(DoLoadModuleEvent()); unregisterEvent(DoUnloadModuleEvent()); diff --git a/include/utils/Events/BasicEvents.h b/include/utils/Events/BasicEvents.h index 61a86da..516313a 100644 --- a/include/utils/Events/BasicEvents.h +++ b/include/utils/Events/BasicEvents.h @@ -7,13 +7,27 @@ namespace Archimedes { class Module; + class AnonymousEvent : public Event { + + public: + + AnonymousEvent() {} + + AnonymousEvent(std::any data) : Event(data) {} + + ~AnonymousEvent() {} + + operator std::string() const override { return "AnonymousEvent"; } + + }; + class LoadModuleEvent : public Event { public: - LoadModuleEvent() : Event(nullptr) {} + LoadModuleEvent() {} - LoadModuleEvent(std::string n) : Event(nullptr), module(n) {} + LoadModuleEvent(std::string n) : module(n) {} ~LoadModuleEvent() {} @@ -27,9 +41,9 @@ namespace Archimedes { public: - DoLoadModuleEvent() : Event(nullptr) {} + DoLoadModuleEvent() {} - DoLoadModuleEvent(std::variant n) : Event(nullptr), module(n) {} + DoLoadModuleEvent(std::variant n) : module(n) {} ~DoLoadModuleEvent() {} @@ -43,9 +57,9 @@ namespace Archimedes { public: - UnloadModuleEvent() : Event(nullptr) {} + UnloadModuleEvent() {} - UnloadModuleEvent(std::string n) : Event(nullptr), module(n) {} + UnloadModuleEvent(std::string n) : module(n) {} ~UnloadModuleEvent() {} @@ -58,9 +72,9 @@ namespace Archimedes { public: - DoUnloadModuleEvent() : Event(nullptr) {} + DoUnloadModuleEvent() {} - DoUnloadModuleEvent(std::string n) : Event(nullptr), module(n) {} + DoUnloadModuleEvent(std::string n) : module(n) {} ~DoUnloadModuleEvent() {} diff --git a/include/utils/Events/Event.h b/include/utils/Events/Event.h index 3910693..28d4747 100644 --- a/include/utils/Events/Event.h +++ b/include/utils/Events/Event.h @@ -9,13 +9,14 @@ namespace Archimedes { public: - Event(void* ptr) : userData(ptr) {} + Event() {} + Event(std::any data) : userData(data) {} virtual ~Event() {} virtual operator std::string() const = 0; - void* userData; + std::any userData; }; } diff --git a/include/utils/Window/WindowEvents.h b/include/utils/Window/WindowEvents.h index 71f7579..0c523ba 100644 --- a/include/utils/Window/WindowEvents.h +++ b/include/utils/Window/WindowEvents.h @@ -19,9 +19,10 @@ namespace Archimedes { public: - ResizeWindowEvent() : Event(nullptr), width(0), height(0) {} + ResizeWindowEvent() : width(0), height(0) {} - ResizeWindowEvent(int w, int h, void* userData = nullptr) : Event(userData), width(w), height(h) {} + ResizeWindowEvent(int w, int h) : width(w), height(h) {} + ResizeWindowEvent(int w, int h, std::any userData) : Event(userData), width(w), height(h) {} ~ResizeWindowEvent() { @@ -39,7 +40,8 @@ namespace Archimedes { CloseWindowEvent() : Event(nullptr), window(nullptr) {} - CloseWindowEvent(const Window* w, void* userData = nullptr) : Event(userData), window(w) {} + CloseWindowEvent(const Window* w) : window(w) {} + CloseWindowEvent(const Window* w, std::any userData) : Event(userData), window(w) {} ~CloseWindowEvent() { @@ -54,9 +56,10 @@ namespace Archimedes { public: - KeyPressedWindowEvent() : Event(nullptr), key(0), repeat(0) {} + KeyPressedWindowEvent() : key(0), repeat(0) {} - KeyPressedWindowEvent(unsigned int k, unsigned int r, void* userData = nullptr) : Event(userData), key(k), repeat(r) {} + KeyPressedWindowEvent(unsigned int k, unsigned int r) : key(k), repeat(r) {} + KeyPressedWindowEvent(unsigned int k, unsigned int r, std::any userData) : Event(userData), key(k), repeat(r) {} ~KeyPressedWindowEvent() { @@ -74,7 +77,8 @@ namespace Archimedes { KeyReleasedWindowEvent() : Event(nullptr), key(0) {} - KeyReleasedWindowEvent(unsigned int k, void* userData = nullptr) : Event(userData), key(k) {} + KeyReleasedWindowEvent(unsigned int k) : key(k) {} + KeyReleasedWindowEvent(unsigned int k, std::any userData) : Event(userData), key(k) {} ~KeyReleasedWindowEvent() { @@ -89,9 +93,10 @@ namespace Archimedes { public: - MouseButtonPressedWindowEvent() : Event(nullptr), button(0) {} + MouseButtonPressedWindowEvent() : button(0) {} - MouseButtonPressedWindowEvent(unsigned int b, void* userData = nullptr) : Event(userData), button(b) {} + MouseButtonPressedWindowEvent(unsigned int b) : button(b) {} + MouseButtonPressedWindowEvent(unsigned int b, std::any userData) : Event(userData), button(b) {} ~MouseButtonPressedWindowEvent() { @@ -106,9 +111,9 @@ namespace Archimedes { public: - MouseButtonReleasedWindowEvent() : Event(nullptr), button(0) {} + MouseButtonReleasedWindowEvent() : button(0) {} - MouseButtonReleasedWindowEvent(unsigned int b, void* userData = nullptr) : Event(userData), button(b) {} + MouseButtonReleasedWindowEvent(unsigned int b) : button(b) {} ~MouseButtonReleasedWindowEvent() { @@ -123,9 +128,10 @@ namespace Archimedes { public: - ScrollWindowEvent() : Event(nullptr), dx(0), dy(0) {} + ScrollWindowEvent() : dx(0), dy(0) {} - ScrollWindowEvent(double x, double y, void* userData = nullptr) : Event(userData), dx(x), dy(y) {} + ScrollWindowEvent(double x, double y) : dx(x), dy(y) {} + ScrollWindowEvent(double x, double y, std::any userData) : Event(userData), dx(x), dy(y) {} ~ScrollWindowEvent() { @@ -140,9 +146,10 @@ namespace Archimedes { public: - MouseMovedWindowEvent() : Event(nullptr), x(0), y(0) {} + MouseMovedWindowEvent() : x(0), y(0) {} - MouseMovedWindowEvent(double x, double y, void* userData = nullptr) : Event(userData), x(x), y(y) {} + MouseMovedWindowEvent(double x, double y) : x(x), y(y) {} + MouseMovedWindowEvent(double x, double y, std::any userData) : Event(userData), x(x), y(y) {} ~MouseMovedWindowEvent() { @@ -157,9 +164,10 @@ namespace Archimedes { public: - FocusedWindowEvent() : Event(nullptr), window(nullptr) {} + FocusedWindowEvent() : window(nullptr) {} - FocusedWindowEvent(const Window* w, void* userData = nullptr) : Event(userData), window(w) {} + FocusedWindowEvent(const Window* w) : window(w) {} + FocusedWindowEvent(const Window* w, std::any userData) : Event(userData), window(w) {} ~FocusedWindowEvent() { @@ -174,9 +182,10 @@ namespace Archimedes { public: - FocusLostWindowEvent() : Event(nullptr), window(nullptr) {} + FocusLostWindowEvent() : window(nullptr) {} - FocusLostWindowEvent(const Window* w, void* userData = nullptr) : Event(userData), window(w) {} + FocusLostWindowEvent(const Window* w) : window(w) {} + FocusLostWindowEvent(const Window* w, std::any userData) : Event(userData), window(w) {} ~FocusLostWindowEvent() { @@ -191,9 +200,10 @@ namespace Archimedes { public: - MovedWindowEvent() : Event(nullptr), x(0), y(0) {} + MovedWindowEvent() : x(0), y(0) {} - MovedWindowEvent(int x, int y, void* userData = nullptr) : Event(userData), x(x), y(y) {} + MovedWindowEvent(int x, int y) : x(x), y(y) {} + MovedWindowEvent(int x, int y, std::any userData) : Event(userData), x(x), y(y) {} ~MovedWindowEvent() { diff --git a/include/utils/Window/WindowSDL3/WindowSDL3.h b/include/utils/Window/WindowSDL3/WindowSDL3.h index 77ebdde..13723a8 100644 --- a/include/utils/Window/WindowSDL3/WindowSDL3.h +++ b/include/utils/Window/WindowSDL3/WindowSDL3.h @@ -48,31 +48,6 @@ namespace Archimedes { WindowData& data = (WindowData&) *ptr; - switch(e->type) { - - } - - return true; - }, &data);*/ - - - SDL_GL_MakeCurrent(w, gl_context); - SDL_GL_SetSwapInterval(1); // Enable vsync - SDL_ShowWindow(w); - } - - ~WindowSDL3() { - SDL_GL_DestroyContext(gl_context); - SDL_DestroyWindow(w); - SDL_Quit(); - } - - bool shouldClose() { return false; } - - void doFrame() { restoreContext(); SDL_GL_SwapWindow(w); } - - void pollEvents() { - while(SDL_PollEvent(&e)) { switch(e.type) { case SDL_EVENT_WINDOW_CLOSE_REQUESTED: @@ -117,6 +92,69 @@ namespace Archimedes { default: break; } + + return true; + }, &data);*/ + + + SDL_GL_MakeCurrent(w, gl_context); + SDL_GL_SetSwapInterval(1); // Enable vsync + SDL_ShowWindow(w); + } + + ~WindowSDL3() { + SDL_GL_DestroyContext(gl_context); + SDL_DestroyWindow(w); + SDL_Quit(); + } + + bool shouldClose() { return false; } + + void doFrame() { restoreContext(); SDL_GL_SwapWindow(w); } + + void pollEvents() { + while(SDL_PollEvent(&e)) { + switch(e.type) { + + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: + case SDL_EVENT_QUIT: + data.sendEvent(new CloseWindowEvent(data.window, &e)); + break; + case SDL_EVENT_WINDOW_RESIZED: + width = e.window.data1; + height = e.window.data2; + data.sendEvent(new ResizeWindowEvent(e.window.data1, e.window.data2, &e)); + break; + case SDL_EVENT_WINDOW_MOUSE_ENTER: + case SDL_EVENT_WINDOW_FOCUS_GAINED: + data.sendEvent(new FocusedWindowEvent(data.window, &e)); + break; + case SDL_EVENT_WINDOW_MOUSE_LEAVE: + case SDL_EVENT_WINDOW_FOCUS_LOST: + data.sendEvent(new FocusLostWindowEvent(data.window, &e)); + break; + case SDL_EVENT_KEY_DOWN: + data.sendEvent(new KeyPressedWindowEvent(e.key.key, e.key.repeat ? 1 : 0, &e)); + break; + case SDL_EVENT_KEY_UP: + data.sendEvent(new KeyReleasedWindowEvent(e.key.key, &e)); + break; + case SDL_EVENT_MOUSE_MOTION: + data.sendEvent(new MouseMovedWindowEvent(e.motion.x, e.motion.y, &e)); + break; + case SDL_EVENT_MOUSE_BUTTON_DOWN: + data.sendEvent(new MouseButtonPressedWindowEvent(e.button.button, &e)); + break; + case SDL_EVENT_MOUSE_BUTTON_UP: + data.sendEvent(new MouseButtonReleasedWindowEvent(e.button.button, &e)); + break; + case SDL_EVENT_MOUSE_WHEEL: + data.sendEvent(new ScrollWindowEvent(e.wheel.x, e.wheel.y, &e)); + break; + default: + data.sendEvent(new AnonymousEvent(&e)); + break; + } } } diff --git a/modules/ImguiModule/src/ImguiModule.cpp b/modules/ImguiModule/src/ImguiModule.cpp index 8273c4b..e7676bb 100644 --- a/modules/ImguiModule/src/ImguiModule.cpp +++ b/modules/ImguiModule/src/ImguiModule.cpp @@ -78,22 +78,8 @@ void ImguiModule::onLoad() { #if WINDOW == 2 ecmd_it = wm->addEventFn([this](Archimedes::Event* e){ - if(e->userData != nullptr) { - unsigned int type = app->getEventType(*e); - - if(type == app->getEventType(Archimedes::ResizeWindowEvent()) - || type == app->getEventType(Archimedes::CloseWindowEvent()) - || type == app->getEventType(Archimedes::KeyPressedWindowEvent()) - || type == app->getEventType(Archimedes::KeyReleasedWindowEvent()) - || type == app->getEventType(Archimedes::MouseButtonPressedWindowEvent()) - || type == app->getEventType(Archimedes::MouseButtonReleasedWindowEvent()) - || type == app->getEventType(Archimedes::ScrollWindowEvent()) - || type == app->getEventType(Archimedes::MouseMovedWindowEvent()) - || type == app->getEventType(Archimedes::FocusedWindowEvent()) - || type == app->getEventType(Archimedes::FocusLostWindowEvent()) - || type == app->getEventType(Archimedes::MovedWindowEvent())) { - ImGui_ImplSDL3_ProcessEvent((SDL_Event*) e->userData); - } + if(e->userData.type() == std::typeid(SDL_Event*)) { + ImGui_ImplSDL3_ProcessEvent(std::any_cast(e.userData)); } }); #endif diff --git a/src/example_apps/MinimalApp/MinimalApp.cpp b/src/example_apps/MinimalApp/MinimalApp.cpp index 36dde6f..d997830 100644 --- a/src/example_apps/MinimalApp/MinimalApp.cpp +++ b/src/example_apps/MinimalApp/MinimalApp.cpp @@ -63,6 +63,8 @@ bool MinimalApp::onEvent(const Archimedes::Event& event) { } else if(type == getEventType(Archimedes::UnloadModuleEvent())) { return true; + } else if(type == getEventType(Archimedes::AnonymousEvent())) { + return true; } return false;