252 lines
7.0 KiB
C++
252 lines
7.0 KiB
C++
|
|
#pragma once
|
|
|
|
#if !defined(RAID_APPLICATION_HPP_INCLUDED)
|
|
#define RAID_APPLICATION_HPP_INCLUDED 1
|
|
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <variant>
|
|
#include <fmt/format.h>
|
|
#include <imgui.h>
|
|
#include <mijin/async/coroutine.hpp>
|
|
#include <mijin/util/bitflags.hpp>
|
|
#include <mijin/virtual_filesystem/memory.hpp>
|
|
#include <mijin/virtual_filesystem/stacked.hpp>
|
|
#include <SDL3/SDL.h>
|
|
#include <SDL3/SDL_vulkan.h>
|
|
#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<FontFlags>
|
|
{
|
|
bool pixelSnapH : 1 = false;
|
|
};
|
|
|
|
struct FontConfig
|
|
{
|
|
fs::path path;
|
|
std::vector<std::pair<ImWchar, ImWchar>> glyphRanges;
|
|
float size = 20.f;
|
|
FontFlags flags;
|
|
};
|
|
|
|
struct ApplicationFlags : mijin::BitFlags<ApplicationFlags>
|
|
{
|
|
/**
|
|
* (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<fs::path, ImTextureID> mTextures;
|
|
|
|
bool mRunning = true;
|
|
ImGuiWindowFlags mMainWindowFlags = DEFAULT_MAIN_WINDOW_FLAGS;
|
|
std::unordered_map<ImGuiStyleVar, std::variant<float, ImVec2>> 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<float, ImVec2> 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<const FontConfig> 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<FontConfig> 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<typename TArg, typename... TArgs>
|
|
void msgInfo(fmt::format_string<TArg, TArgs...> format, TArg&& arg, TArgs&&... args)
|
|
{
|
|
std::string text = fmt::format(format, std::forward<TArg>(arg), std::forward<TArgs>(args)...);
|
|
msgInfo(text);
|
|
}
|
|
template<typename TArg, typename... TArgs>
|
|
void msgWarning(fmt::format_string<TArg, TArgs...> format, TArg&& arg, TArgs&&... args)
|
|
{
|
|
std::string text = fmt::format(format, std::forward<TArg>(arg), std::forward<TArgs>(args)...);
|
|
msgWarning(text);
|
|
}
|
|
template<typename TArg, typename... TArgs>
|
|
void msgError(fmt::format_string<TArg, TArgs...> format, TArg&& arg, TArgs&&... args)
|
|
{
|
|
std::string text = fmt::format(format, std::forward<TArg>(arg), std::forward<TArgs>(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<void()>;
|
|
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)
|