From 690935e4ecb6410094ab75b6538f92e0cbb8ca05 Mon Sep 17 00:00:00 2001
From: Gary Talent <gary@drinkingtea.net>
Date: Mon, 19 Jun 2023 20:28:27 -0500
Subject: [PATCH] [nostalgia/core] Cleanup

---
 .../core/include/nostalgia/core/gfx.hpp       |  4 +-
 .../modules/core/src/gba/context.cpp          |  6 +-
 .../modules/core/src/gba/context.hpp          |  4 +-
 src/nostalgia/modules/core/src/gba/gfx.cpp    | 74 ++++++++++---------
 src/nostalgia/modules/core/src/opengl/gfx.cpp | 16 ++--
 5 files changed, 57 insertions(+), 47 deletions(-)

diff --git a/src/nostalgia/modules/core/include/nostalgia/core/gfx.hpp b/src/nostalgia/modules/core/include/nostalgia/core/gfx.hpp
index ad94d845..2ac9ef9a 100644
--- a/src/nostalgia/modules/core/include/nostalgia/core/gfx.hpp
+++ b/src/nostalgia/modules/core/include/nostalgia/core/gfx.hpp
@@ -48,8 +48,6 @@ bool bgStatus(Context *ctx, unsigned bg) noexcept;
 
 void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept;
 
-ox::Error initConsole(Context *ctx) noexcept;
-
 void setBgCbb(Context *ctx, unsigned bgIdx, unsigned cbb) noexcept;
 
 /**
@@ -62,6 +60,8 @@ ox::Error loadSpriteTileSheet(Context *ctx,
                               const ox::FileAddress &tilesheetAddr,
                               const ox::FileAddress &paletteAddr) noexcept;
 
+ox::Error initConsole(Context *ctx) noexcept;
+
 void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept;
 
 void setTile(Context *ctx, unsigned bgIdx, int column, int row, uint8_t tile) noexcept;
diff --git a/src/nostalgia/modules/core/src/gba/context.cpp b/src/nostalgia/modules/core/src/gba/context.cpp
index ca8f74ed..7d51c219 100644
--- a/src/nostalgia/modules/core/src/gba/context.cpp
+++ b/src/nostalgia/modules/core/src/gba/context.cpp
@@ -8,11 +8,13 @@
 
 namespace nostalgia::core {
 
+GbaContext::GbaContext(turbine::Context *tctx) noexcept: turbineCtx(tctx) {
+}
+
 ox::Error initGfx(Context *ctx, const InitParams&) noexcept;
 
 ox::Result<ox::UniquePtr<Context>> init(turbine::Context *tctx, const InitParams &params) noexcept {
-	auto ctx = ox::make_unique<GbaContext>();
-	ctx->turbineCtx = tctx;
+	auto ctx = ox::make_unique<GbaContext>(tctx);
 	oxReturnError(initGfx(ctx.get(), params));
 	return ox::UPtr<Context>(ctx.release());
 }
diff --git a/src/nostalgia/modules/core/src/gba/context.hpp b/src/nostalgia/modules/core/src/gba/context.hpp
index 2a20c070..637eb71b 100644
--- a/src/nostalgia/modules/core/src/gba/context.hpp
+++ b/src/nostalgia/modules/core/src/gba/context.hpp
@@ -10,7 +10,9 @@ namespace nostalgia::core {
 
 struct GbaContext: public core::Context {
 
-	turbine::Context *turbineCtx = nullptr;
+	turbine::Context const*turbineCtx = nullptr;
+
+	explicit GbaContext(turbine::Context *tctx) noexcept;
 
 	[[nodiscard]]
 	const auto &rom() const noexcept {
diff --git a/src/nostalgia/modules/core/src/gba/gfx.cpp b/src/nostalgia/modules/core/src/gba/gfx.cpp
index 110e33cb..8d4949d3 100644
--- a/src/nostalgia/modules/core/src/gba/gfx.cpp
+++ b/src/nostalgia/modules/core/src/gba/gfx.cpp
@@ -80,7 +80,7 @@ void shutdownGfx(Context*) noexcept {
 }
 
 uint8_t bgStatus(Context*) noexcept {
-	return (REG_DISPCTL >> 8) & 0b1111;
+	return (REG_DISPCTL >> 8u) & 0b1111u;
 }
 
 void setBgStatus(Context*, uint32_t status) noexcept {
@@ -98,28 +98,6 @@ void setBgStatus(Context*, unsigned bg, bool status) noexcept {
 	REG_DISPCTL = REG_DISPCTL | ((REG_DISPCTL & ~mask) | mask);
 }
 
-// Do NOT rely on Context in the GBA version of this function.
-ox::Error initConsole(Context *ctx) noexcept {
-	constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng");
-	constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal");
-	setBgStatus(ctx, 0b0001);
-	if (!ctx) {
-		const auto gctx = new(ox_alloca(sizeof(GbaContext))) GbaContext;
-		oxRequire(rom, keel::loadRom());
-		auto romFs = new(ox_alloca(sizeof(ox::FileSystem32)))
-				ox::FileSystem32(ox::FileStore32(rom, 32 * ox::units::MB));
-		oxRequireM(tctx, turbine::init(ox::UPtr<ox::FileSystem>(romFs), ""));
-		gctx->turbineCtx = tctx.release();
-		oxReturnError(loadBgTileSheet(gctx, 0, TilesheetAddr, PaletteAddr));
-		setBgCbb(gctx, 0, 0);
-		return {};
-	} else {
-		oxReturnError(loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr));
-		setBgCbb(ctx, 0, 0);
-		return {};
-	}
-}
-
 void setBgCbb(Context*, unsigned bgIdx, unsigned cbb) noexcept {
 	auto &bgCtl = regBgCtl(bgIdx);
 	const auto &cbbData = g_cbbData[cbb];
@@ -127,13 +105,13 @@ void setBgCbb(Context*, unsigned bgIdx, unsigned cbb) noexcept {
 	teagba::bgSetCbb(&bgCtl, cbb);
 }
 
-ox::Error loadBgTileSheet(Context *ctx,
-                          unsigned cbb,
-                          const ox::FileAddress &tilesheetAddr,
-                          const ox::FileAddress &paletteAddr) noexcept {
-	auto &gctx = static_cast<GbaContext&>(*ctx);
-	oxRequire(tsStat, gctx.rom().stat(tilesheetAddr));
-	oxRequire(ts, static_cast<const ox::MemFS&>(gctx.rom()).directAccess(tilesheetAddr));
+static ox::Error loadBgTileSheet(
+		const ox::MemFS &rom,
+		unsigned cbb,
+		const ox::FileAddress &tilesheetAddr,
+		const ox::FileAddress &paletteAddr) noexcept {
+	oxRequire(tsStat, rom.stat(tilesheetAddr));
+	oxRequire(ts, rom.directAccess(tilesheetAddr));
 	GbaTileMapTarget target;
 	target.pal.palette = MEM_BG_PALETTE;
 	target.cbbData = &g_cbbData[cbb];
@@ -141,8 +119,8 @@ ox::Error loadBgTileSheet(Context *ctx,
 	oxReturnError(ox::readMC(ts, static_cast<std::size_t>(tsStat.size), &target));
 	// load external palette if available
 	if (paletteAddr) {
-		oxRequire(palStat, gctx.rom().stat(paletteAddr));
-		oxRequire(pal, static_cast<const ox::MemFS&>(gctx.rom()).directAccess(paletteAddr));
+		oxRequire(palStat, rom.stat(paletteAddr));
+		oxRequire(pal, rom.directAccess(paletteAddr));
 		oxReturnError(ox::readMC(pal, static_cast<std::size_t>(palStat.size), &target.pal));
 	}
 	// update bpp of all bgs with the updated cbb
@@ -155,6 +133,15 @@ ox::Error loadBgTileSheet(Context *ctx,
 	return {};
 }
 
+ox::Error loadBgTileSheet(Context *ctx,
+                          unsigned cbb,
+                          const ox::FileAddress &tilesheetAddr,
+                          const ox::FileAddress &paletteAddr) noexcept {
+	auto &gctx = static_cast<GbaContext&>(*ctx);
+	auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
+	return loadBgTileSheet(rom, cbb, tilesheetAddr, paletteAddr);
+}
+
 ox::Error loadSpriteTileSheet(Context *ctx,
                               const ox::FileAddress &tilesheetAddr,
                               const ox::FileAddress &paletteAddr) noexcept {
@@ -186,14 +173,33 @@ ox::Error loadBgPalette(Context *ctx, unsigned, const ox::FileAddress &paletteAd
 
 ox::Error loadSpritePalette(Context *ctx, unsigned cbb, const ox::FileAddress &paletteAddr) noexcept {
 	auto &gctx = static_cast<GbaContext&>(*ctx);
+	auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
 	GbaPaletteTarget target;
 	target.palette = &MEM_SPRITE_PALETTE[cbb];
-	oxRequire(palStat, gctx.rom().stat(paletteAddr));
-	oxRequire(pal, static_cast<const ox::MemFS&>(gctx.rom()).directAccess(paletteAddr));
+	oxRequire(palStat, rom.stat(paletteAddr));
+	oxRequire(pal, rom.directAccess(paletteAddr));
 	oxReturnError(ox::readMC(pal, static_cast<std::size_t>(palStat.size), &target));
 	return {};
 }
 
+// Do NOT rely on Context in the GBA version of this function.
+ox::Error initConsole(Context *ctx) noexcept {
+	constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng");
+	constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal");
+	setBgStatus(ctx, 0b0001);
+	if (!ctx) {
+		oxRequire(rom, keel::loadRom());
+		const ox::FileSystem32 romFs(ox::FileStore32(rom, 32 * ox::units::MB));
+		oxReturnError(loadBgTileSheet(romFs, 0, TilesheetAddr, PaletteAddr));
+		setBgCbb(nullptr, 0, 0);
+		return {};
+	} else {
+		oxReturnError(loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr));
+		setBgCbb(ctx, 0, 0);
+		return {};
+	}
+}
+
 void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept {
 	const auto col = static_cast<unsigned>(column);
 	for (auto i = 0u; i < str.bytes(); i++) {
diff --git a/src/nostalgia/modules/core/src/opengl/gfx.cpp b/src/nostalgia/modules/core/src/opengl/gfx.cpp
index 4cc219fe..b2305d96 100644
--- a/src/nostalgia/modules/core/src/opengl/gfx.cpp
+++ b/src/nostalgia/modules/core/src/opengl/gfx.cpp
@@ -363,14 +363,6 @@ void shutdownGfx(Context &ctx) noexcept {
 	turbine::gl::removeDrawer(gctx.turbineCtx, &gctx.drawer);
 }
 
-ox::Error initConsole(Context *ctx) noexcept {
-	constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng");
-	constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal");
-	setBgStatus(ctx, 0b0001);
-	setBgCbb(ctx, 0, 0);
-	return loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr);
-}
-
 struct TileSheetData {
 	ox::Vector<uint32_t> pixels;
 	int width = 0;
@@ -430,6 +422,14 @@ ox::Error loadSpriteTileSheet(
 	return {};
 }
 
+ox::Error initConsole(Context *ctx) noexcept {
+	constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng");
+	constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal");
+	setBgStatus(ctx, 0b0001);
+	setBgCbb(ctx, 0, 0);
+	return loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr);
+}
+
 void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept {
 	const auto col = static_cast<unsigned>(column);
 	for (auto i = 0u; i < str.bytes(); ++i) {