From 7459d687b04668e0283cd97f8d15bd3297dd29f4 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 3 Feb 2022 21:06:44 -0600 Subject: [PATCH] [nostalgia/core] Rename customData functions in Context and move them out of Context --- src/nostalgia/core/context.hpp | 28 +++++++++++++--------- src/nostalgia/core/glfw/core.cpp | 2 +- src/nostalgia/core/userland/gfx.hpp | 2 +- src/nostalgia/core/userland/gfx_opengl.cpp | 3 +-- src/nostalgia/studio/main.cpp | 8 +++---- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/nostalgia/core/context.hpp b/src/nostalgia/core/context.hpp index 527fe55e..ae8213e2 100644 --- a/src/nostalgia/core/context.hpp +++ b/src/nostalgia/core/context.hpp @@ -25,6 +25,10 @@ class Drawer { // User Input Output class Context { + friend constexpr void setApplicationData(Context *ctx, void *applicationData) noexcept; + template + [[nodiscard]] + friend constexpr T *applicationData(Context *ctx) noexcept; friend bool bgStatus(Context *ctx, unsigned bg) noexcept; friend common::Size getScreenSize(Context *ctx) noexcept; friend int getScreenHeight(Context *ctx) noexcept; @@ -60,10 +64,12 @@ class Context { AssetManager assetManager; #endif private: - void *m_customData = nullptr; void *m_windowerData = nullptr; void *m_rendererData = nullptr; + protected: + void *m_customData = nullptr; + public: Context() noexcept = default; @@ -71,16 +77,6 @@ class Context { Context(const Context &other) noexcept = delete; Context(const Context &&other) noexcept = delete; - constexpr void setCustomData(void *customData) noexcept { - m_customData = customData; - } - - template - [[nodiscard]] - constexpr T *customData() noexcept { - return static_cast(m_customData); - } - protected: template [[nodiscard]] @@ -104,5 +100,15 @@ class Context { }; +constexpr void setApplicationData(Context *ctx, void *applicationData) noexcept { + ctx->m_customData = applicationData; +} + +template +[[nodiscard]] +constexpr T *applicationData(Context *ctx) noexcept { + return static_cast(ctx->m_customData); +} + } diff --git a/src/nostalgia/core/glfw/core.cpp b/src/nostalgia/core/glfw/core.cpp index fb662f8a..8d8f0044 100644 --- a/src/nostalgia/core/glfw/core.cpp +++ b/src/nostalgia/core/glfw/core.cpp @@ -45,7 +45,7 @@ ox::Error run(Context *ctx) noexcept { glfwSwapBuffers(id->window); } // destroy GLFW window - oxReturnError(renderer::shutdown(ctx, ctx->rendererData())); + renderer::shutdown(ctx, ctx->rendererData()); glfwDestroyWindow(id->window); ctx->setWindowerData(nullptr); delete id; diff --git a/src/nostalgia/core/userland/gfx.hpp b/src/nostalgia/core/userland/gfx.hpp index e5ed8e24..e7e2fc5c 100644 --- a/src/nostalgia/core/userland/gfx.hpp +++ b/src/nostalgia/core/userland/gfx.hpp @@ -12,7 +12,7 @@ namespace nostalgia::core::renderer { ox::Error init(Context *ctx, void **rendererData) noexcept; -ox::Error shutdown(Context *ctx, void *rendererData) noexcept; +void shutdown(Context *ctx, void *rendererData) noexcept; ox::Error loadBgTexture(void *rendererData, int section, void *pixels, int w, int h) noexcept; diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index e35fc3cd..55921ef9 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -195,10 +195,9 @@ ox::Error init(Context *ctx, void **rendererData) noexcept { return OxError(0); } -ox::Error shutdown(Context*, void *rendererData) noexcept { +void shutdown(Context*, void *rendererData) noexcept { const auto id = reinterpret_cast(rendererData); delete id; - return OxError(0); } ox::Error loadBgTexture(void *rendererData, int section, void *pixels, int w, int h) noexcept { diff --git a/src/nostalgia/studio/main.cpp b/src/nostalgia/studio/main.cpp index 347b1981..f55e35b0 100644 --- a/src/nostalgia/studio/main.cpp +++ b/src/nostalgia/studio/main.cpp @@ -23,7 +23,7 @@ class StudioUIDrawer: public core::Drawer { }; static int eventHandler(core::Context *ctx) noexcept { - auto ui = ctx->customData(); + auto ui = core::applicationData(ctx); ui->update(); return 16; } @@ -32,9 +32,9 @@ static ox::Error run(ox::UniquePtr fs) noexcept { oxRequireM(ctx, core::init(std::move(fs), "NostalgiaStudio")); core::setWindowTitle(ctx.get(), "Nostalgia Studio"); core::setEventHandler(ctx.get(), eventHandler); - auto ui = ox::make_unique(ctx.get()); - StudioUIDrawer drawer(ui.get()); - ctx->setCustomData(ui.get()); + StudioUI ui(ctx.get()); + StudioUIDrawer drawer(&ui); + core::setApplicationData(ctx.get(), &ui); ctx->drawers.emplace_back(&drawer); return core::run(ctx.get()); }