diff --git a/src/nostalgia/core/gba/panic.cpp b/src/nostalgia/core/gba/panic.cpp index fd2f0d23..c4e83a72 100644 --- a/src/nostalgia/core/gba/panic.cpp +++ b/src/nostalgia/core/gba/panic.cpp @@ -41,7 +41,7 @@ void panic(const char *file, int line, const char *panicMsg, const ox::Error &er } // disable all interrupt handling and IntrWait on no interrupts REG_IE = 0; - teagba_intrwait(0, 0); + teagba::intrwait(0, 0); } } diff --git a/src/nostalgia/core/opengl/context.cpp b/src/nostalgia/core/opengl/context.cpp index c746bd65..625cc332 100644 --- a/src/nostalgia/core/opengl/context.cpp +++ b/src/nostalgia/core/opengl/context.cpp @@ -7,13 +7,17 @@ namespace nostalgia::core { +GlContext::GlContext(turbine::Context &tctx) noexcept: + turbineCtx(tctx), + drawer(*this) { +} + GlContext::~GlContext() noexcept { - shutdownGfx(this); + shutdownGfx(*this); } ox::Result<ox::UniquePtr<Context>> init(turbine::Context *tctx, const InitParams ¶ms) noexcept { - auto ctx = ox::make_unique<GlContext>(); - ctx->turbineCtx = tctx; + auto ctx = ox::make_unique<GlContext>(*tctx); oxReturnError(initGfx(ctx.get(), params)); return ox::UPtr<Context>(ctx.release()); } diff --git a/src/nostalgia/core/opengl/context.hpp b/src/nostalgia/core/opengl/context.hpp index e23d8828..bfc9dc16 100644 --- a/src/nostalgia/core/opengl/context.hpp +++ b/src/nostalgia/core/opengl/context.hpp @@ -16,7 +16,7 @@ namespace nostalgia::core { struct GlContext: public core::Context { - turbine::Context *turbineCtx = nullptr; + turbine::Context &turbineCtx; glutils::GLProgram bgShader; glutils::GLProgram spriteShader; ox::Array<renderer::CBB, 4> cbbs; @@ -25,6 +25,7 @@ struct GlContext: public core::Context { ox::Array<renderer::Background, 4> backgrounds; ox::Optional<ox::Size> renderSize; renderer::Drawer drawer; + explicit GlContext(turbine::Context &tctx) noexcept; ~GlContext() noexcept; }; diff --git a/src/nostalgia/core/opengl/gfx.cpp b/src/nostalgia/core/opengl/gfx.cpp index 85d09518..f4991f8d 100644 --- a/src/nostalgia/core/opengl/gfx.cpp +++ b/src/nostalgia/core/opengl/gfx.cpp @@ -19,8 +19,10 @@ namespace nostalgia::core { namespace renderer { +Drawer::Drawer(Context &ctx) noexcept: m_ctx(ctx) {} + void Drawer::draw(turbine::Context &tctx) noexcept { - core::gl::drawMainView(m_ctx, turbine::getScreenSize(tctx)); + core::gl::drawMainView(&m_ctx, turbine::getScreenSize(tctx)); } constexpr ox::StringView bgvshadTmpl = R"( @@ -348,17 +350,16 @@ ox::Error initGfx( for (auto &bg : gctx.cbbs) { initBackgroundBufferset(ctx, gctx.bgShader, &bg); } - gctx.drawer.m_ctx = ctx; if (initParams.glInstallDrawer) { - turbine::gl::addDrawer(*gctx.turbineCtx, &gctx.drawer); + turbine::gl::addDrawer(gctx.turbineCtx, &gctx.drawer); initSpritesBufferset(ctx, gctx.spriteShader, &gctx.spriteBlocks); } return {}; } -void shutdownGfx(Context *ctx) noexcept { - auto &gctx = static_cast<GlContext&>(*ctx); - turbine::gl::removeDrawer(*gctx.turbineCtx, &gctx.drawer); +void shutdownGfx(Context &ctx) noexcept { + auto &gctx = static_cast<GlContext&>(ctx); + turbine::gl::removeDrawer(gctx.turbineCtx, &gctx.drawer); } ox::Error initConsole(Context *ctx) noexcept { @@ -405,7 +406,7 @@ ox::Error loadBgTileSheet( const ox::FileAddress &tilesheetAddr, const ox::FileAddress &paletteAddr) noexcept { auto &gctx = static_cast<GlContext&>(*ctx); - auto &kctx = *gctx.turbineCtx; + auto &kctx = gctx.turbineCtx; oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr)); oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette)); oxRequire(tsd, loadTileSheet(ctx, *tilesheet)); @@ -419,7 +420,7 @@ ox::Error loadSpriteTileSheet( const ox::FileAddress &tilesheetAddr, const ox::FileAddress &paletteAddr) noexcept { auto &gctx = static_cast<GlContext&>(*ctx); - auto &kctx = *gctx.turbineCtx; + auto &kctx = gctx.turbineCtx; oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr)); oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette)); oxRequire(tsd, loadTileSheet(ctx, *tilesheet)); @@ -467,7 +468,7 @@ void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept { } -void glearTileLayer(Context *ctx, unsigned bgIdx) noexcept { +void clearTileLayer(Context *ctx, unsigned bgIdx) noexcept { auto &gctx = static_cast<GlContext&>(*ctx); auto &bg = gctx.cbbs[static_cast<std::size_t>(bgIdx)]; initBackgroundBufferObjects(&gctx, &bg); @@ -483,14 +484,15 @@ void hideSprite(Context *ctx, unsigned idx) noexcept { gctx.spriteBlocks.updated = true; } -void setSprite(Context *ctx, - unsigned idx, - int x, - int y, - unsigned tileIdx, - unsigned spriteShape, - unsigned spriteSize, - unsigned flipX) noexcept { +void setSprite( + Context *ctx, + unsigned idx, + int x, + int y, + unsigned tileIdx, + unsigned spriteShape, + unsigned spriteSize, + unsigned flipX) noexcept { //oxTracef("nostalgia::core::gfx::gl", "setSprite(ctx, {}, {}, {}, {}, {}, {}, {})", // idx, x, y, tileIdx, spriteShape, spriteSize, flipX); // Tonc Table 8.4 diff --git a/src/nostalgia/core/opengl/gfx.hpp b/src/nostalgia/core/opengl/gfx.hpp index 43a7609e..b9bd05c1 100644 --- a/src/nostalgia/core/opengl/gfx.hpp +++ b/src/nostalgia/core/opengl/gfx.hpp @@ -53,8 +53,10 @@ struct Sprite { }; class Drawer: public turbine::gl::Drawer { + private: + Context &m_ctx; public: - Context *m_ctx = nullptr; + explicit Drawer(Context &ctx) noexcept; void draw(turbine::Context&) noexcept final; }; @@ -62,5 +64,5 @@ class Drawer: public turbine::gl::Drawer { namespace nostalgia::core { ox::Error initGfx(Context *ctx, const InitParams&) noexcept; -void shutdownGfx(Context *ctx) noexcept; +void shutdownGfx(Context &ctx) noexcept; } \ No newline at end of file diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.cpp b/src/nostalgia/core/studio/tilesheeteditormodel.cpp index 027b1590..500a4f39 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.cpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.cpp @@ -97,7 +97,7 @@ class DrawCommand: public TileSheetCommand { oldPalIdx = pOldPalIdx; } }; - TileSheet *m_img = nullptr; + TileSheet &m_img; TileSheet::SubSheetIdx m_subSheetIdx; ox::Vector<Change, 8> m_changes; int m_palIdx = 0; @@ -108,10 +108,10 @@ class DrawCommand: public TileSheetCommand { TileSheet::SubSheetIdx subSheetIdx, std::size_t idx, int palIdx) noexcept: - m_img(&img), + m_img(img), m_subSheetIdx(std::move(subSheetIdx)) { - auto &subsheet = m_img->getSubSheet(m_subSheetIdx); - m_changes.emplace_back(static_cast<uint32_t>(idx), subsheet.getPixel(m_img->bpp, idx)); + auto &subsheet = m_img.getSubSheet(m_subSheetIdx); + m_changes.emplace_back(static_cast<uint32_t>(idx), subsheet.getPixel(m_img.bpp, idx)); m_palIdx = palIdx; } @@ -120,25 +120,25 @@ class DrawCommand: public TileSheetCommand { TileSheet::SubSheetIdx subSheetIdx, const ox::Vector<std::size_t> &idxList, int palIdx) noexcept: - m_img(&img), + m_img(img), m_subSheetIdx(std::move(subSheetIdx)) { - auto &subsheet = m_img->getSubSheet(m_subSheetIdx); + auto &subsheet = m_img.getSubSheet(m_subSheetIdx); for (const auto idx : idxList) { - m_changes.emplace_back(static_cast<uint32_t>(idx), subsheet.getPixel(m_img->bpp, idx)); + m_changes.emplace_back(static_cast<uint32_t>(idx), subsheet.getPixel(m_img.bpp, idx)); } m_palIdx = palIdx; } constexpr auto append(std::size_t idx) noexcept { - auto &subsheet = m_img->getSubSheet(m_subSheetIdx); - if (m_changes.back().value->idx != idx && subsheet.getPixel(m_img->bpp, idx) != m_palIdx) { + auto &subsheet = m_img.getSubSheet(m_subSheetIdx); + if (m_changes.back().value->idx != idx && subsheet.getPixel(m_img.bpp, idx) != m_palIdx) { // duplicate entries are bad auto existing = ox::find_if(m_changes.cbegin(), m_changes.cend(), [idx](const auto &c) { return c.idx == idx; }); if (existing == m_changes.cend()) { - m_changes.emplace_back(static_cast<uint32_t>(idx), subsheet.getPixel(m_img->bpp, idx)); - subsheet.setPixel(m_img->bpp, idx, static_cast<uint8_t>(m_palIdx)); + m_changes.emplace_back(static_cast<uint32_t>(idx), subsheet.getPixel(m_img.bpp, idx)); + subsheet.setPixel(m_img.bpp, idx, static_cast<uint8_t>(m_palIdx)); return true; } } @@ -154,16 +154,16 @@ class DrawCommand: public TileSheetCommand { } void redo() noexcept final { - auto &subsheet = m_img->getSubSheet(m_subSheetIdx); + auto &subsheet = m_img.getSubSheet(m_subSheetIdx); for (const auto &c : m_changes) { - subsheet.setPixel(m_img->bpp, c.idx, static_cast<uint8_t>(m_palIdx)); + subsheet.setPixel(m_img.bpp, c.idx, static_cast<uint8_t>(m_palIdx)); } } void undo() noexcept final { - auto &subsheet = m_img->getSubSheet(m_subSheetIdx); + auto &subsheet = m_img.getSubSheet(m_subSheetIdx); for (const auto &c : m_changes) { - subsheet.setPixel(m_img->bpp, c.idx, static_cast<uint8_t>(c.oldPalIdx)); + subsheet.setPixel(m_img.bpp, c.idx, static_cast<uint8_t>(c.oldPalIdx)); } }