[nostalgia/core] Cleanup

This commit is contained in:
Gary Talent 2023-06-19 20:28:27 -05:00
parent 56e980385c
commit 690935e4ec
5 changed files with 57 additions and 47 deletions

View File

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

View File

@ -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());
}

View File

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

View File

@ -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,
static ox::Error loadBgTileSheet(
const ox::MemFS &rom,
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));
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++) {

View File

@ -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) {