From 41258cda5b488fa864b1216b09d3e15c0c5d5e73 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Sun, 21 Sep 2025 17:44:43 +0200 Subject: [PATCH] Moved Application declarations to the appropriate header. --- private/raid/application.cpp | 2 +- public/raid/application.hpp | 251 +++++++++++++++++++++++++++++++++++ public/raid/raid.hpp | 244 +--------------------------------- 3 files changed, 253 insertions(+), 244 deletions(-) create mode 100644 public/raid/application.hpp diff --git a/private/raid/application.cpp b/private/raid/application.cpp index abef77f..5840764 100644 --- a/private/raid/application.cpp +++ b/private/raid/application.cpp @@ -1,5 +1,5 @@ -#include "raid/raid.hpp" +#include "raid/application.hpp" #include #include diff --git a/public/raid/application.hpp b/public/raid/application.hpp new file mode 100644 index 0000000..4dbd959 --- /dev/null +++ b/public/raid/application.hpp @@ -0,0 +1,251 @@ + +#pragma once + +#if !defined(RAID_APPLICATION_HPP_INCLUDED) +#define RAID_APPLICATION_HPP_INCLUDED 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "./internal/opengl.hpp" +#include "./internal/vulkan.hpp" + +namespace raid +{ +inline constexpr int ERR_INIT_FAILED = 100; +inline constexpr ImGuiWindowFlags DEFAULT_MAIN_WINDOW_FLAGS = 0 + | ImGuiWindowFlags_NoBackground + | ImGuiWindowFlags_NoDecoration + | ImGuiWindowFlags_NoBringToFrontOnFocus + | ImGuiWindowFlags_NoNav; +inline constexpr const char* DEFAULT_FONT_PATH = "/data/fonts/NotoSans-Regular.ttf"; + +enum class MessageSeverity : unsigned char +{ + INFO, + WARNING, + ERROR +}; + +struct Message +{ + MessageSeverity severity; + const char* text; +}; + +struct FontFlags : mijin::BitFlags +{ + bool pixelSnapH : 1 = false; +}; + +struct FontConfig +{ + fs::path path; + std::vector> glyphRanges; + float size = 20.f; + FontFlags flags; +}; + +struct ApplicationFlags : mijin::BitFlags +{ + /** + * (Linux only) prefer X11 even when on Wayland. Required for multi-viewport support, but currently really buggy. + * \see https://github.com/ocornut/imgui/issues/8609 + * \see https://github.com/ocornut/imgui/issues/8587 + */ + bool x11OnWayland : 1 = false; +}; + +enum class GraphicsAPI : std::uint8_t +{ + OPENGL, + VULKAN +}; + +struct ApplicationConfig +{ + ApplicationFlags flags = {}; + GraphicsAPI graphicsApi = GraphicsAPI::OPENGL; +}; + +class Application : private MixinOpenGLApplication, MixinVulkanApplication +{ +private: + SDL_Window* mWindow = nullptr; + + mijin::StackedFileSystemAdapter mFS; + mijin::MemoryFileSystemAdapter* mMemoryFS = nullptr; + mijin::SimpleTaskLoop mTaskLoop; + std::unordered_map mTextures; + + bool mRunning = true; + ImGuiWindowFlags mMainWindowFlags = DEFAULT_MAIN_WINDOW_FLAGS; + std::unordered_map> mMainWindowStyles; + const ApplicationConfig mConfig; + + union + { + OpenGLData gl; + VulkanData vk; + }; +public: + explicit Application(ApplicationConfig config = {}) noexcept : mConfig(config) {} + virtual ~Application() = default; + + [[nodiscard]] + const ApplicationConfig& getConfig() const noexcept { return mConfig; } + + [[nodiscard]] + mijin::StackedFileSystemAdapter& getFS() { return mFS; } + + [[nodiscard]] + mijin::MemoryFileSystemAdapter& getMemoryFS() + { + MIJIN_ASSERT_FATAL(mMemoryFS != nullptr, "Memory FS has not been initialized yet."); + return *mMemoryFS; + } + + [[nodiscard]] + mijin::SimpleTaskLoop& getLoop() { return mTaskLoop; } + + [[nodiscard]] + ImGuiWindowFlags getMainWindowFlags() const { return mMainWindowFlags; } + + void setMainWindowFlags(ImGuiWindowFlags flags) { mMainWindowFlags = flags; } + void setMainWindowStyle(ImGuiStyleVar variable, std::variant value) { mMainWindowStyles.emplace(variable, value); } + void unsetMainWindowStyle(ImGuiStyleVar variable) { mMainWindowStyles.erase(variable); } + void requestQuit() { mRunning = false; } + + [[nodiscard]] + int run(int argc, char* argv[]); + + [[nodiscard]] + bool loadFonts(std::span fonts); + + [[nodiscard]] + bool loadFont(const FontConfig& font) + { + return loadFonts({&font, 1}); + } + + [[nodiscard]] + ImTextureID getOrLoadTexture(fs::path path); + + void destroyTexture(ImTextureID texture); + +protected: + virtual void render() = 0; + virtual std::string getFolderName() = 0; + virtual std::string getWindowTitle() = 0; + virtual void configureImgui(); + virtual std::vector getDefaultFonts(); + virtual void initMemoryFS(); + virtual void handleMessage(const Message& message); + virtual void handleSDLEvent(const SDL_Event& event); + virtual void handleSDLError(const char* message); + + void msgInfo(const char* text) + { + handleMessage({ + .severity = MessageSeverity::INFO, + .text = text + }); + } + void msgWarning(const char* text) + { + handleMessage({ + .severity = MessageSeverity::WARNING, + .text = text + }); + } + void msgError(const char* text) + { + handleMessage({ + .severity = MessageSeverity::ERROR, + .text = text + }); + } + void msgInfo(const std::string& text) + { + msgInfo(text.c_str()); + } + void msgWarning(const std::string& text) + { + msgWarning(text.c_str()); + } + void msgError(const std::string& text) + { + msgError(text.c_str()); + } + template + void msgInfo(fmt::format_string format, TArg&& arg, TArgs&&... args) + { + std::string text = fmt::format(format, std::forward(arg), std::forward(args)...); + msgInfo(text); + } + template + void msgWarning(fmt::format_string format, TArg&& arg, TArgs&&... args) + { + std::string text = fmt::format(format, std::forward(arg), std::forward(args)...); + msgWarning(text); + } + template + void msgError(fmt::format_string format, TArg&& arg, TArgs&&... args) + { + std::string text = fmt::format(format, std::forward(arg), std::forward(args)...); + msgError(text); + } + virtual bool init(); + virtual void cleanup(); +private: + bool initSDL(); + bool initOpenGL(); + bool initVulkan(); + bool initImGui(); + void handleSDLEvents(); + void loadImGuiConfig(); + void saveImGuiConfig(); +}; + +using render_cb_t = std::function; +struct QuickAppOptions +{ + struct + { + render_cb_t render; + } callbacks; + std::string folderName = "raid"; + std::string windowTitle = "RAID"; + ImGuiWindowFlags mainWindowFlags = DEFAULT_MAIN_WINDOW_FLAGS; +}; + +class QuickApp : public Application +{ +private: + render_cb_t mRenderCallback; + std::string mFolderName; + std::string mWindowTitle; +public: + explicit QuickApp(ApplicationConfig config = {}) noexcept : Application(config) {} + + void preInit(QuickAppOptions options); + void render() override; + std::string getFolderName() override; + std::string getWindowTitle() override; + + static QuickApp& get(); +}; + +[[nodiscard]] +int runQuick(int argc, char* argv[], QuickAppOptions options); +} // namespace raid + +#endif // !defined(RAID_APPLICATION_HPP_INCLUDED) diff --git a/public/raid/raid.hpp b/public/raid/raid.hpp index 76c88e1..f75eb87 100644 --- a/public/raid/raid.hpp +++ b/public/raid/raid.hpp @@ -4,248 +4,6 @@ #if !defined(RAID_PUBLIC_RAID_RAID_HPP_INCLUDED) #define RAID_PUBLIC_RAID_RAID_HPP_INCLUDED 1 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "./internal/opengl.hpp" -#include "./internal/vulkan.hpp" - -namespace raid -{ -inline constexpr int ERR_INIT_FAILED = 100; -inline constexpr ImGuiWindowFlags DEFAULT_MAIN_WINDOW_FLAGS = 0 - | ImGuiWindowFlags_NoBackground - | ImGuiWindowFlags_NoDecoration - | ImGuiWindowFlags_NoBringToFrontOnFocus - | ImGuiWindowFlags_NoNav; -inline constexpr const char* DEFAULT_FONT_PATH = "/data/fonts/NotoSans-Regular.ttf"; - -enum class MessageSeverity : unsigned char -{ - INFO, - WARNING, - ERROR -}; - -struct Message -{ - MessageSeverity severity; - const char* text; -}; - -struct FontFlags : mijin::BitFlags -{ - bool pixelSnapH : 1 = false; -}; - -struct FontConfig -{ - fs::path path; - std::vector> glyphRanges; - float size = 20.f; - FontFlags flags; -}; - -struct ApplicationFlags : mijin::BitFlags -{ - /** - * (Linux only) prefer X11 even when on Wayland. Required for multi-viewport support, but currently really buggy. - * \see https://github.com/ocornut/imgui/issues/8609 - * \see https://github.com/ocornut/imgui/issues/8587 - */ - bool x11OnWayland : 1 = false; -}; - -enum class GraphicsAPI : std::uint8_t -{ - OPENGL, - VULKAN -}; - -struct ApplicationConfig -{ - ApplicationFlags flags = {}; - GraphicsAPI graphicsApi = GraphicsAPI::OPENGL; -}; - -class Application : private MixinOpenGLApplication, MixinVulkanApplication -{ -private: - SDL_Window* mWindow = nullptr; - - mijin::StackedFileSystemAdapter mFS; - mijin::MemoryFileSystemAdapter* mMemoryFS = nullptr; - mijin::SimpleTaskLoop mTaskLoop; - std::unordered_map mTextures; - - bool mRunning = true; - ImGuiWindowFlags mMainWindowFlags = DEFAULT_MAIN_WINDOW_FLAGS; - std::unordered_map> mMainWindowStyles; - const ApplicationConfig mConfig; - - union - { - OpenGLData gl; - VulkanData vk; - }; -public: - explicit Application(ApplicationConfig config = {}) noexcept : mConfig(config) {} - virtual ~Application() = default; - - [[nodiscard]] - const ApplicationConfig& getConfig() const noexcept { return mConfig; } - - [[nodiscard]] - mijin::StackedFileSystemAdapter& getFS() { return mFS; } - - [[nodiscard]] - mijin::MemoryFileSystemAdapter& getMemoryFS() - { - MIJIN_ASSERT_FATAL(mMemoryFS != nullptr, "Memory FS has not been initialized yet."); - return *mMemoryFS; - } - - [[nodiscard]] - mijin::SimpleTaskLoop& getLoop() { return mTaskLoop; } - - [[nodiscard]] - ImGuiWindowFlags getMainWindowFlags() const { return mMainWindowFlags; } - - void setMainWindowFlags(ImGuiWindowFlags flags) { mMainWindowFlags = flags; } - void setMainWindowStyle(ImGuiStyleVar variable, std::variant value) { mMainWindowStyles.emplace(variable, value); } - void unsetMainWindowStyle(ImGuiStyleVar variable) { mMainWindowStyles.erase(variable); } - void requestQuit() { mRunning = false; } - - [[nodiscard]] - int run(int argc, char* argv[]); - - [[nodiscard]] - bool loadFonts(std::span fonts); - - [[nodiscard]] - bool loadFont(const FontConfig& font) - { - return loadFonts({&font, 1}); - } - - [[nodiscard]] - ImTextureID getOrLoadTexture(fs::path path); - - void destroyTexture(ImTextureID texture); - -protected: - virtual void render() = 0; - virtual std::string getFolderName() = 0; - virtual std::string getWindowTitle() = 0; - virtual void configureImgui(); - virtual std::vector getDefaultFonts(); - virtual void initMemoryFS(); - virtual void handleMessage(const Message& message); - virtual void handleSDLEvent(const SDL_Event& event); - virtual void handleSDLError(const char* message); - - void msgInfo(const char* text) - { - handleMessage({ - .severity = MessageSeverity::INFO, - .text = text - }); - } - void msgWarning(const char* text) - { - handleMessage({ - .severity = MessageSeverity::WARNING, - .text = text - }); - } - void msgError(const char* text) - { - handleMessage({ - .severity = MessageSeverity::ERROR, - .text = text - }); - } - void msgInfo(const std::string& text) - { - msgInfo(text.c_str()); - } - void msgWarning(const std::string& text) - { - msgWarning(text.c_str()); - } - void msgError(const std::string& text) - { - msgError(text.c_str()); - } - template - void msgInfo(fmt::format_string format, TArg&& arg, TArgs&&... args) - { - std::string text = fmt::format(format, std::forward(arg), std::forward(args)...); - msgInfo(text); - } - template - void msgWarning(fmt::format_string format, TArg&& arg, TArgs&&... args) - { - std::string text = fmt::format(format, std::forward(arg), std::forward(args)...); - msgWarning(text); - } - template - void msgError(fmt::format_string format, TArg&& arg, TArgs&&... args) - { - std::string text = fmt::format(format, std::forward(arg), std::forward(args)...); - msgError(text); - } - virtual bool init(); - virtual void cleanup(); -private: - bool initSDL(); - bool initOpenGL(); - bool initVulkan(); - bool initImGui(); - void handleSDLEvents(); - void loadImGuiConfig(); - void saveImGuiConfig(); -}; - -using render_cb_t = std::function; -struct QuickAppOptions -{ - struct - { - render_cb_t render; - } callbacks; - std::string folderName = "raid"; - std::string windowTitle = "RAID"; - ImGuiWindowFlags mainWindowFlags = DEFAULT_MAIN_WINDOW_FLAGS; -}; - -class QuickApp : public Application -{ -private: - render_cb_t mRenderCallback; - std::string mFolderName; - std::string mWindowTitle; -public: - explicit QuickApp(ApplicationConfig config = {}) noexcept : Application(config) {} - - void preInit(QuickAppOptions options); - void render() override; - std::string getFolderName() override; - std::string getWindowTitle() override; - - static QuickApp& get(); -}; - -[[nodiscard]] -int runQuick(int argc, char* argv[], QuickAppOptions options); -} // namespace raid +#include "./application.hpp" #endif // !defined(RAID_PUBLIC_RAID_RAID_HPP_INCLUDED)