diff --git a/src/nostalgia/core/context.hpp b/src/nostalgia/core/context.hpp index 38321790..527fe55e 100644 --- a/src/nostalgia/core/context.hpp +++ b/src/nostalgia/core/context.hpp @@ -23,12 +23,6 @@ class Drawer { virtual void draw(Context*) noexcept = 0; }; -namespace renderer { -ox::Error init(Context *ctx) noexcept; -ox::Error shutdown(Context *ctx) noexcept; -ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) noexcept; -} - // User Input Output class Context { friend bool bgStatus(Context *ctx, unsigned bg) noexcept; @@ -36,9 +30,10 @@ class Context { friend int getScreenHeight(Context *ctx) noexcept; friend int getScreenWidth(Context *ctx) noexcept; friend ox::Error initGfx(Context *ctx) noexcept; - friend ox::Error renderer::init(Context *ctx) noexcept; - friend ox::Error renderer::loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) noexcept; - friend ox::Error renderer::shutdown(Context *ctx) noexcept; + friend ox::Error loadBgTileSheet(Context *ctx, + int section, + const ox::FileAddress &tilesheetPath, + const ox::FileAddress &palettePath) noexcept; friend ox::Error run(Context *ctx) noexcept; friend ox::Error shutdown(Context *ctx) noexcept; friend ox::Result> init(ox::UniquePtr fs, const char *appName) noexcept; diff --git a/src/nostalgia/core/glfw/core.cpp b/src/nostalgia/core/glfw/core.cpp index d754233e..fb662f8a 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)); + oxReturnError(renderer::shutdown(ctx, ctx->rendererData())); glfwDestroyWindow(id->window); ctx->setWindowerData(nullptr); delete id; diff --git a/src/nostalgia/core/glfw/gfx.cpp b/src/nostalgia/core/glfw/gfx.cpp index 61885e35..ef6a1714 100644 --- a/src/nostalgia/core/glfw/gfx.cpp +++ b/src/nostalgia/core/glfw/gfx.cpp @@ -66,7 +66,10 @@ ox::Error initGfx(Context *ctx) noexcept { //io.MouseDrawCursor = true; ImGui_ImplGlfw_InitForOpenGL(id->window, true); } - return renderer::init(ctx); + void *rendererData = nullptr; + oxReturnError(renderer::init(ctx, &rendererData)); + ctx->setRendererData(rendererData); + return OxError(0); } void setWindowTitle(Context *ctx, const char *title) noexcept { diff --git a/src/nostalgia/core/userland/gfx.cpp b/src/nostalgia/core/userland/gfx.cpp index 3b0cd14c..7abc6e24 100644 --- a/src/nostalgia/core/userland/gfx.cpp +++ b/src/nostalgia/core/userland/gfx.cpp @@ -46,7 +46,7 @@ ox::Error loadBgTileSheet(Context *ctx, pixels[i * 2 + 1] = toColor32(palette->colors[tilesheet->pixels[i] >> 4]); } } - return renderer::loadBgTexture(ctx, section, pixels.data(), width, height); + return renderer::loadBgTexture(ctx->rendererData(), section, pixels.data(), width, height); } void puts(Context *ctx, int column, int row, const char *str) noexcept { diff --git a/src/nostalgia/core/userland/gfx.hpp b/src/nostalgia/core/userland/gfx.hpp index dfc83ae1..e5ed8e24 100644 --- a/src/nostalgia/core/userland/gfx.hpp +++ b/src/nostalgia/core/userland/gfx.hpp @@ -10,10 +10,10 @@ namespace nostalgia::core::renderer { -ox::Error init(Context *ctx) noexcept; +ox::Error init(Context *ctx, void **rendererData) noexcept; -ox::Error shutdown(Context *ctx) noexcept; +ox::Error shutdown(Context *ctx, void *rendererData) noexcept; -ox::Error loadBgTexture(Context *ctx, int section, void *bytes, int w, int h) 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 75dacb7f..e35fc3cd 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -181,12 +181,13 @@ static void drawBackgrounds(GlImplData *id) noexcept { } } -ox::Error init(Context *ctx) noexcept { - const auto id = new GlImplData; - ctx->setRendererData(id); +ox::Error init(Context *ctx, void **rendererData) noexcept { const auto vshad = ox::sfmt(bgvshad, glutils::GlslVersion); const auto fshad = ox::sfmt(bgfshad, glutils::GlslVersion); - oxReturnError(glutils::buildShaderProgram(vshad.c_str(), fshad.c_str()).moveTo(&id->bgShader)); + oxRequireM(bgShader, glutils::buildShaderProgram(vshad.c_str(), fshad.c_str())); + const auto id = new GlImplData; + *rendererData = id; + id->bgShader = std::move(bgShader); for (auto &bg : id->backgrounds) { initBackgroundBufferset(ctx, id->bgShader, &bg); } @@ -194,16 +195,15 @@ ox::Error init(Context *ctx) noexcept { return OxError(0); } -ox::Error shutdown(Context *ctx) noexcept { - const auto id = ctx->rendererData(); - ctx->setRendererData(nullptr); +ox::Error shutdown(Context*, void *rendererData) noexcept { + const auto id = reinterpret_cast(rendererData); delete id; return OxError(0); } -ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) noexcept { +ox::Error loadBgTexture(void *rendererData, int section, void *pixels, int w, int h) noexcept { oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { section: {}, w: {}, h: {} }", section, w, h); - const auto id = ctx->rendererData(); + const auto id = static_cast(rendererData); auto &tex = id->backgrounds[static_cast(section)].tex; tex = loadTexture(w, h, pixels); return OxError(0);