[nostalgia/core] Rename customData functions in Context and move them out of Context

This commit is contained in:
Gary Talent 2022-02-03 21:06:44 -06:00
parent c7cd54ae52
commit 7459d687b0
5 changed files with 24 additions and 19 deletions

View File

@ -25,6 +25,10 @@ class Drawer {
// User Input Output
class Context {
friend constexpr void setApplicationData(Context *ctx, void *applicationData) noexcept;
template<typename T>
[[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<typename T>
[[nodiscard]]
constexpr T *customData() noexcept {
return static_cast<T*>(m_customData);
}
protected:
template<typename T>
[[nodiscard]]
@ -104,5 +100,15 @@ class Context {
};
constexpr void setApplicationData(Context *ctx, void *applicationData) noexcept {
ctx->m_customData = applicationData;
}
template<typename T>
[[nodiscard]]
constexpr T *applicationData(Context *ctx) noexcept {
return static_cast<T*>(ctx->m_customData);
}
}

View File

@ -45,7 +45,7 @@ ox::Error run(Context *ctx) noexcept {
glfwSwapBuffers(id->window);
}
// destroy GLFW window
oxReturnError(renderer::shutdown(ctx, ctx->rendererData<void>()));
renderer::shutdown(ctx, ctx->rendererData<void>());
glfwDestroyWindow(id->window);
ctx->setWindowerData(nullptr);
delete id;

View File

@ -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;

View File

@ -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<GlImplData*>(rendererData);
delete id;
return OxError(0);
}
ox::Error loadBgTexture(void *rendererData, int section, void *pixels, int w, int h) noexcept {

View File

@ -23,7 +23,7 @@ class StudioUIDrawer: public core::Drawer {
};
static int eventHandler(core::Context *ctx) noexcept {
auto ui = ctx->customData<StudioUI>();
auto ui = core::applicationData<StudioUI>(ctx);
ui->update();
return 16;
}
@ -32,9 +32,9 @@ static ox::Error run(ox::UniquePtr<ox::FileSystem> 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<StudioUI>(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());
}