[keel,nostalgia] Make core calls take Context references instead of pointers

This commit is contained in:
Gary Talent 2023-12-01 22:53:21 -06:00
parent 72c130d8a9
commit 453e08497d
16 changed files with 116 additions and 117 deletions

View File

@ -103,11 +103,7 @@ class AssetRef: public ox::SignalHandler {
return nullptr; return nullptr;
} }
constexpr const T &operator*() const & noexcept { constexpr const T &operator*() const noexcept {
return *m_ctr->get();
}
constexpr const T &&operator*() const && noexcept {
return *m_ctr->get(); return *m_ctr->get();
} }

View File

@ -27,7 +27,7 @@ class Context {
}; };
ox::Result<ox::UniquePtr<Context>> init(turbine::Context *tctx, const InitParams& = {}) noexcept; ox::Result<ox::UniquePtr<Context>> init(turbine::Context &tctx, InitParams const& = {}) noexcept;
} }

View File

@ -36,44 +36,44 @@ oxModelBegin(Sprite)
oxModelEnd() oxModelEnd()
[[nodiscard]] [[nodiscard]]
uint8_t bgStatus(Context *ctx) noexcept; uint8_t bgStatus(Context &ctx) noexcept;
void setBgStatus(Context *ctx, uint32_t status) noexcept; void setBgStatus(Context &ctx, uint32_t status) noexcept;
bool bgStatus(Context *ctx, unsigned bg) noexcept; bool bgStatus(Context &ctx, unsigned bg) noexcept;
void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept; void setBgStatus(Context &ctx, unsigned bg, bool status) noexcept;
void setBgCbb(Context *ctx, unsigned bgIdx, unsigned cbb) noexcept; void setBgCbb(Context &ctx, unsigned bgIdx, unsigned cbb) noexcept;
/** /**
* @param section describes which section of the selected TileSheetSpace to use (e.g. MEM_PALLETE_BG[section]) * @param section describes which section of the selected TileSheetSpace to use (e.g. MEM_PALLETE_BG[section])
*/ */
ox::Error loadBgTileSheet( ox::Error loadBgTileSheet(
Context *ctx, Context &ctx,
unsigned cbb, unsigned cbb,
ox::FileAddress const&tilesheetAddr, ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr = nullptr) noexcept; ox::FileAddress const&paletteAddr = nullptr) noexcept;
ox::Error loadSpriteTileSheet( ox::Error loadSpriteTileSheet(
Context *ctx, Context &ctx,
ox::FileAddress const&tilesheetAddr, ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept; ox::FileAddress const&paletteAddr) noexcept;
ox::Error initConsole(Context *ctx) noexcept; ox::Error initConsole(Context &ctx) noexcept;
void puts(Context *ctx, int column, int row, ox::CRStringView str) 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; void setTile(Context &ctx, unsigned bgIdx, int column, int row, uint8_t tile) noexcept;
void clearTileLayer(Context *ctx, unsigned bgIdx) noexcept; void clearTileLayer(Context &ctx, unsigned bgIdx) noexcept;
void hideSprite(Context *ctx, unsigned) noexcept; void hideSprite(Context &ctx, unsigned) noexcept;
void setSprite(Context *ctx, unsigned idx, int x, int y, unsigned tileIdx, void setSprite(Context &ctx, unsigned idx, int x, int y, unsigned tileIdx,
unsigned spriteShape = 0, unsigned spriteSize = 0, unsigned flipX = 0) noexcept; unsigned spriteShape = 0, unsigned spriteSize = 0, unsigned flipX = 0) noexcept;
void setSprite(Context *ctx, Sprite const&s) noexcept; void setSprite(Context &ctx, Sprite const&s) noexcept;
} }

View File

@ -11,12 +11,12 @@ namespace nostalgia::core {
GbaContext::GbaContext(turbine::Context *tctx) noexcept: turbineCtx(tctx) { GbaContext::GbaContext(turbine::Context *tctx) noexcept: turbineCtx(tctx) {
} }
ox::Error initGfx(Context *ctx, InitParams const&) noexcept; ox::Error initGfx(Context &ctx, InitParams const&) noexcept;
ox::Result<ox::UniquePtr<Context>> init(turbine::Context *tctx, InitParams const&params) noexcept { ox::Result<ox::UniquePtr<Context>> init(turbine::Context &tctx, InitParams const&params) noexcept {
auto ctx = ox::make_unique<GbaContext>(tctx); auto ctx = ox::make_unique<GbaContext>(&tctx);
oxReturnError(initGfx(ctx.get(), params)); oxReturnError(initGfx(*ctx, params));
return ox::UPtr<Context>(ctx.release()); return ox::UPtr<Context>(std::move(ctx));
} }
} }

View File

@ -71,42 +71,43 @@ constexpr ox::Error model(auto *io, ox::CommonPtrWith<GbaTileMapTarget> auto *t)
return io->template field<uint8_t, decltype(handleTileMap)>("tileMap", handleTileMap); return io->template field<uint8_t, decltype(handleTileMap)>("tileMap", handleTileMap);
} }
ox::Error initGfx(Context*, InitParams const&) noexcept { ox::Error initGfx(Context&, InitParams const&) noexcept {
for (auto bgCtl = &REG_BG0CTL; bgCtl <= &REG_BG3CTL; bgCtl += 2) { for (auto bgCtl = &REG_BG0CTL; bgCtl <= &REG_BG3CTL; bgCtl += 2) {
teagba::bgSetSbb(bgCtl, 28); teagba::bgSetSbb(bgCtl, 28);
} }
return {}; return {};
} }
void shutdownGfx(Context*) noexcept { uint8_t bgStatus(Context&) noexcept {
}
uint8_t bgStatus(Context*) noexcept {
return (REG_DISPCTL >> 8u) & 0b1111u; return (REG_DISPCTL >> 8u) & 0b1111u;
} }
void setBgStatus(Context*, uint32_t status) noexcept { void setBgStatus(Context&, uint32_t status) noexcept {
constexpr auto BgStatus = 8; constexpr auto BgStatus = 8;
REG_DISPCTL = (REG_DISPCTL & ~0b111100000000u) | status << BgStatus; REG_DISPCTL = (REG_DISPCTL & ~0b111100000000u) | status << BgStatus;
} }
bool bgStatus(Context*, unsigned bg) noexcept { bool bgStatus(Context&, unsigned bg) noexcept {
return (REG_DISPCTL >> (8 + bg)) & 1; return (REG_DISPCTL >> (8 + bg)) & 1;
} }
void setBgStatus(Context*, unsigned bg, bool status) noexcept { void setBgStatus(Context&, unsigned bg, bool status) noexcept {
constexpr auto Bg0Status = 8; constexpr auto Bg0Status = 8;
const auto mask = static_cast<uint32_t>(status) << (Bg0Status + bg); const auto mask = static_cast<uint32_t>(status) << (Bg0Status + bg);
REG_DISPCTL = REG_DISPCTL | ((REG_DISPCTL & ~mask) | mask); REG_DISPCTL = REG_DISPCTL | ((REG_DISPCTL & ~mask) | mask);
} }
void setBgCbb(Context*, unsigned bgIdx, unsigned cbb) noexcept { static void setBgCbb(Context*, unsigned bgIdx, unsigned cbb) noexcept {
auto &bgCtl = regBgCtl(bgIdx); auto &bgCtl = regBgCtl(bgIdx);
const auto &cbbData = g_cbbData[cbb]; const auto &cbbData = g_cbbData[cbb];
teagba::bgSetBpp(&bgCtl, cbbData.bpp); teagba::bgSetBpp(&bgCtl, cbbData.bpp);
teagba::bgSetCbb(&bgCtl, cbb); teagba::bgSetCbb(&bgCtl, cbb);
} }
void setBgCbb(Context&, unsigned bgIdx, unsigned cbb) noexcept {
setBgCbb(nullptr, bgIdx, cbb);
}
static ox::Error loadBgTileSheet( static ox::Error loadBgTileSheet(
const ox::MemFS &rom, const ox::MemFS &rom,
unsigned cbb, unsigned cbb,
@ -136,20 +137,20 @@ static ox::Error loadBgTileSheet(
} }
ox::Error loadBgTileSheet( ox::Error loadBgTileSheet(
Context *ctx, Context &ctx,
unsigned cbb, unsigned cbb,
ox::FileAddress const&tilesheetAddr, ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept { ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx); auto &gctx = static_cast<GbaContext&>(ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom()); auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
return loadBgTileSheet(rom, cbb, tilesheetAddr, paletteAddr); return loadBgTileSheet(rom, cbb, tilesheetAddr, paletteAddr);
} }
ox::Error loadSpriteTileSheet( ox::Error loadSpriteTileSheet(
Context *ctx, Context &ctx,
ox::FileAddress const&tilesheetAddr, ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept { ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx); auto &gctx = static_cast<GbaContext&>(ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom()); auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
oxRequire(tsStat, gctx.rom().stat(tilesheetAddr)); oxRequire(tsStat, gctx.rom().stat(tilesheetAddr));
oxRequire(ts, rom.directAccess(tilesheetAddr)); oxRequire(ts, rom.directAccess(tilesheetAddr));
@ -166,8 +167,8 @@ ox::Error loadSpriteTileSheet(
return {}; return {};
} }
ox::Error loadBgPalette(Context *ctx, unsigned, ox::FileAddress const&paletteAddr) noexcept { ox::Error loadBgPalette(Context &ctx, unsigned, ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx); auto &gctx = static_cast<GbaContext&>(ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom()); auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
GbaPaletteTarget target; GbaPaletteTarget target;
target.palette = MEM_BG_PALETTE; target.palette = MEM_BG_PALETTE;
@ -177,8 +178,8 @@ ox::Error loadBgPalette(Context *ctx, unsigned, ox::FileAddress const&paletteAdd
return {}; return {};
} }
ox::Error loadSpritePalette(Context *ctx, unsigned cbb, ox::FileAddress const&paletteAddr) noexcept { ox::Error loadSpritePalette(Context &ctx, unsigned cbb, ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx); auto &gctx = static_cast<GbaContext&>(ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom()); auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
GbaPaletteTarget target; GbaPaletteTarget target;
target.palette = &MEM_SPRITE_PALETTE[cbb]; target.palette = &MEM_SPRITE_PALETTE[cbb];
@ -188,25 +189,16 @@ ox::Error loadSpritePalette(Context *ctx, unsigned cbb, ox::FileAddress const&pa
return {}; return {};
} }
// Do NOT rely on Context in the GBA version of this function. ox::Error initConsole(Context &ctx) noexcept {
ox::Error initConsole(Context *ctx) noexcept {
constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng"); constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng");
constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal"); constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal");
setBgStatus(ctx, 0b0001); 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)); oxReturnError(loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr));
setBgCbb(ctx, 0, 0); setBgCbb(ctx, 0, 0);
return {}; return {};
} }
}
void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept { void puts(Context &ctx, int column, int row, ox::CRStringView str) noexcept {
const auto col = static_cast<unsigned>(column); const auto col = static_cast<unsigned>(column);
for (auto i = 0u; i < str.bytes(); i++) { for (auto i = 0u; i < str.bytes(); i++) {
const auto c = charMap[static_cast<std::size_t>(str[i])]; const auto c = charMap[static_cast<std::size_t>(str[i])];
@ -214,18 +206,18 @@ void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept {
} }
} }
void setTile(Context*, unsigned bgIdx, int column, int row, uint8_t tile) noexcept { void setTile(Context&, unsigned bgIdx, int column, int row, uint8_t tile) noexcept {
const auto tileIdx = static_cast<std::size_t>(row * GbaTileColumns + column); const auto tileIdx = static_cast<std::size_t>(row * GbaTileColumns + column);
MEM_BG_MAP[bgIdx][tileIdx] = tile; MEM_BG_MAP[bgIdx][tileIdx] = tile;
} }
// Do NOT use Context in the GBA version of this function. // Do NOT use Context in the GBA version of this function.
void clearTileLayer(Context*, unsigned bgIdx) noexcept { void clearTileLayer(Context&, unsigned bgIdx) noexcept {
memset(MEM_BG_MAP[bgIdx].data(), 0, GbaTileRows * GbaTileColumns); memset(MEM_BG_MAP[bgIdx].data(), 0, GbaTileRows * GbaTileColumns);
} }
[[maybe_unused]] [[maybe_unused]]
void hideSprite(Context*, unsigned idx) noexcept { void hideSprite(Context&, unsigned idx) noexcept {
//oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow"); //oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow");
teagba::GbaSpriteAttrUpdate oa; teagba::GbaSpriteAttrUpdate oa;
oa.attr0 = 2 << 8; oa.attr0 = 2 << 8;
@ -233,7 +225,7 @@ void hideSprite(Context*, unsigned idx) noexcept {
teagba::addSpriteUpdate(oa); teagba::addSpriteUpdate(oa);
} }
void setSprite(Context*, void setSprite(Context&,
unsigned idx, unsigned idx,
int x, int x,
int y, int y,

View File

@ -7,5 +7,5 @@
#include <nostalgia/core/context.hpp> #include <nostalgia/core/context.hpp>
namespace nostalgia::core { namespace nostalgia::core {
ox::Error initGfx(Context *ctx, InitParams const&) noexcept; ox::Error initGfx(Context &ctx, InitParams const&) noexcept;
} }

View File

@ -3,33 +3,44 @@
*/ */
#include <nostalgia/core/core.hpp> #include <ox/std/def.hpp>
#include <keel/media.hpp>
#include <turbine/turbine.hpp>
#include <teagba/addresses.hpp> #include <teagba/addresses.hpp>
#include <teagba/bios.hpp> #include <teagba/bios.hpp>
#include <teagba/gfx.hpp>
#include <nostalgia/core/core.hpp>
#include "gfx.hpp" #include "gfx.hpp"
#define HEAP_BEGIN (reinterpret_cast<char*>(MEM_EWRAM_BEGIN))
#define HEAP_SIZE ((MEM_EWRAM_END - MEM_EWRAM_BEGIN) / 2)
#define HEAP_END (reinterpret_cast<char*>(MEM_EWRAM_BEGIN + HEAP_SIZE))
namespace ox { namespace ox {
using namespace nostalgia::core; using namespace nostalgia::core;
void panic(const char *file, int line, const char *panicMsg, ox::Error const&err) noexcept { void panic(const char *file, int line, const char *panicMsg, ox::Error const&err) noexcept {
oxIgnoreError(initGfx(nullptr, {})); // reset heap to make sure we have enough memory to allocate context data
oxIgnoreError(initConsole(nullptr)); ox::heapmgr::initHeap(HEAP_BEGIN, HEAP_END);
// enable only BG 0 auto tctx = turbine::init(keel::loadRomFs("").unwrap(), "Nostalgia").unwrap();
REG_DISPCTL = teagba::DispCtl_Bg0; auto ctx = init(*tctx).unwrap();
clearTileLayer(nullptr, 0); oxIgnoreError(initGfx(*ctx, {}));
oxIgnoreError(initConsole(*ctx));
setBgStatus(*ctx, 0, true);
clearTileLayer(*ctx, 0);
ox::BString<23> serr = "Error code: "; ox::BString<23> serr = "Error code: ";
serr += static_cast<int64_t>(err); serr += static_cast<int64_t>(err);
puts(nullptr, 32 + 1, 1, "SADNESS..."); puts(*ctx, 32 + 1, 1, "SADNESS...");
puts(nullptr, 32 + 1, 4, "UNEXPECTED STATE:"); puts(*ctx, 32 + 1, 4, "UNEXPECTED STATE:");
puts(nullptr, 32 + 2, 6, panicMsg); puts(*ctx, 32 + 2, 6, panicMsg);
if (err) { if (err) {
puts(nullptr, 32 + 2, 8, serr); puts(*ctx, 32 + 2, 8, serr);
} }
puts(nullptr, 32 + 1, 15, "PLEASE RESTART THE SYSTEM"); puts(*ctx, 32 + 1, 15, "PLEASE RESTART THE SYSTEM");
// print to terminal if in mGBA // print to terminal if in mGBA
oxErrf("\033[31;1;1mPANIC:\033[0m [{}:{}]: {}\n", file, line, panicMsg); oxErrf("\033[31;1;1mPANIC:\033[0m [{}:{}]: {}\n", file, line, panicMsg);
if (err.msg) { if (err.msg) {

View File

@ -137,7 +137,7 @@ ox::Array<char, 128> charMap = {
0, // ~ 0, // ~
}; };
void setSprite(Context *c, const Sprite &s) noexcept { void setSprite(Context &c, const Sprite &s) noexcept {
setSprite(c, s.idx, s.x, s.y, s.tileIdx, s.spriteShape, s.spriteSize, s.flipX); setSprite(c, s.idx, s.x, s.y, s.tileIdx, s.spriteShape, s.spriteSize, s.flipX);
} }

View File

@ -16,9 +16,9 @@ GlContext::~GlContext() noexcept {
shutdownGfx(*this); shutdownGfx(*this);
} }
ox::Result<ox::UniquePtr<Context>> init(turbine::Context *tctx, InitParams const&params) noexcept { ox::Result<ox::UniquePtr<Context>> init(turbine::Context &tctx, InitParams const&params) noexcept {
auto ctx = ox::make_unique<GlContext>(*tctx); auto ctx = ox::make_unique<GlContext>(tctx);
oxReturnError(initGfx(ctx.get(), params)); oxReturnError(initGfx(*ctx, params));
return ox::UPtr<Context>(ctx.release()); return ox::UPtr<Context>(ctx.release());
} }

View File

@ -368,7 +368,7 @@ static void loadSpriteTexture(
} }
ox::Error initGfx( ox::Error initGfx(
Context *ctx, Context &ctx,
InitParams const&initParams) noexcept { InitParams const&initParams) noexcept {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -376,7 +376,7 @@ ox::Error initGfx(
const auto bgFshad = ox::sfmt(renderer::bgfshadTmpl, glutils::GlslVersion); const auto bgFshad = ox::sfmt(renderer::bgfshadTmpl, glutils::GlslVersion);
const auto spriteVshad = ox::sfmt(renderer::spritevshadTmpl, glutils::GlslVersion); const auto spriteVshad = ox::sfmt(renderer::spritevshadTmpl, glutils::GlslVersion);
const auto spriteFshad = ox::sfmt(renderer::spritefshadTmpl, glutils::GlslVersion); const auto spriteFshad = ox::sfmt(renderer::spritefshadTmpl, glutils::GlslVersion);
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
oxReturnError(glutils::buildShaderProgram(bgVshad.c_str(), bgFshad.c_str()).moveTo(&gctx.bgShader)); oxReturnError(glutils::buildShaderProgram(bgVshad.c_str(), bgFshad.c_str()).moveTo(&gctx.bgShader));
oxReturnError( oxReturnError(
glutils::buildShaderProgram(spriteVshad.c_str(), spriteFshad.c_str()).moveTo(&gctx.spriteShader)); glutils::buildShaderProgram(spriteVshad.c_str(), spriteFshad.c_str()).moveTo(&gctx.spriteShader));
@ -430,15 +430,15 @@ static ox::Result<TileSheetData> loadTileSheet(
} }
ox::Error loadBgTileSheet( ox::Error loadBgTileSheet(
Context *ctx, Context &ctx,
uint_t cbb, uint_t cbb,
ox::FileAddress const&tilesheetAddr, ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept { ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
auto &kctx = gctx.turbineCtx.keelCtx; auto &kctx = gctx.turbineCtx.keelCtx;
oxRequire(tilesheet, readObj<CompactTileSheet>(kctx, tilesheetAddr)); oxRequire(tilesheet, readObj<CompactTileSheet>(kctx, tilesheetAddr));
oxRequire(palette, readObj<Palette>(kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette)); oxRequire(palette, readObj<Palette>(kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette));
oxRequire(tsd, loadTileSheet(*ctx, *tilesheet).to([](TileSheetData const&t) -> TileSheetData { oxRequire(tsd, loadTileSheet(ctx, *tilesheet).to([](TileSheetData const&t) -> TileSheetData {
return { return {
.pixels = resizeTileSheetData(t.pixels, t.size(), Scale), .pixels = resizeTileSheetData(t.pixels, t.size(), Scale),
.width = t.width * Scale, .width = t.width * Scale,
@ -451,20 +451,20 @@ ox::Error loadBgTileSheet(
} }
ox::Error loadSpriteTileSheet( ox::Error loadSpriteTileSheet(
Context *ctx, Context &ctx,
ox::FileAddress const&tilesheetAddr, ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept { ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
auto &kctx = gctx.turbineCtx.keelCtx; auto &kctx = gctx.turbineCtx.keelCtx;
oxRequire(tilesheet, readObj<CompactTileSheet>(kctx, tilesheetAddr)); oxRequire(tilesheet, readObj<CompactTileSheet>(kctx, tilesheetAddr));
oxRequire(palette, readObj<Palette>(kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette)); oxRequire(palette, readObj<Palette>(kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette));
oxRequire(tsd, loadTileSheet(*ctx, *tilesheet)); oxRequire(tsd, loadTileSheet(ctx, *tilesheet));
renderer::loadSpriteTexture(gctx, tsd.pixels.data(), tsd.width, tsd.height); renderer::loadSpriteTexture(gctx, tsd.pixels.data(), tsd.width, tsd.height);
renderer::loadSpritePalette(gctx, *palette); renderer::loadSpritePalette(gctx, *palette);
return {}; return {};
} }
ox::Error initConsole(Context *ctx) noexcept { ox::Error initConsole(Context &ctx) noexcept {
constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng"); constexpr ox::FileAddress TilesheetAddr("/TileSheets/Charset.ng");
constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal"); constexpr ox::FileAddress PaletteAddr("/Palettes/Charset.npal");
setBgStatus(ctx, 0b0001); setBgStatus(ctx, 0b0001);
@ -472,7 +472,7 @@ ox::Error initConsole(Context *ctx) noexcept {
return loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr); return loadBgTileSheet(ctx, 0, TilesheetAddr, PaletteAddr);
} }
void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept { void puts(Context &ctx, int column, int row, ox::CRStringView str) noexcept {
const auto col = static_cast<uint_t>(column); const auto col = static_cast<uint_t>(column);
for (auto i = 0u; i < str.bytes(); ++i) { for (auto i = 0u; i < str.bytes(); ++i) {
setTile( setTile(
@ -484,14 +484,14 @@ void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept {
} }
} }
void setBgCbb(Context *ctx, uint_t bgIdx, uint_t cbbIdx) noexcept { void setBgCbb(Context &ctx, uint_t bgIdx, uint_t cbbIdx) noexcept {
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
auto &bg = gctx.backgrounds[bgIdx]; auto &bg = gctx.backgrounds[bgIdx];
bg.cbbIdx = cbbIdx; bg.cbbIdx = cbbIdx;
} }
uint8_t bgStatus(Context *ctx) noexcept { uint8_t bgStatus(Context &ctx) noexcept {
const auto &gctx = glctx(*ctx); const auto &gctx = glctx(ctx);
uint8_t out = 0; uint8_t out = 0;
for (uint_t i = 0; i < gctx.cbbs.size(); ++i) { for (uint_t i = 0; i < gctx.cbbs.size(); ++i) {
out |= static_cast<uint8_t>(static_cast<uint_t>(gctx.backgrounds[i].enabled) << i); out |= static_cast<uint8_t>(static_cast<uint_t>(gctx.backgrounds[i].enabled) << i);
@ -499,33 +499,33 @@ uint8_t bgStatus(Context *ctx) noexcept {
return out; return out;
} }
void setBgStatus(Context *ctx, uint32_t status) noexcept { void setBgStatus(Context &ctx, uint32_t status) noexcept {
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
for (uint_t i = 0; i < gctx.cbbs.size(); ++i) { for (uint_t i = 0; i < gctx.cbbs.size(); ++i) {
gctx.backgrounds[i].enabled = (status >> i) & 1; gctx.backgrounds[i].enabled = (status >> i) & 1;
} }
} }
bool bgStatus(Context *ctx, uint_t bg) noexcept { bool bgStatus(Context &ctx, uint_t bg) noexcept {
const auto &gctx = glctx(*ctx); const auto &gctx = glctx(ctx);
return gctx.backgrounds[bg].enabled; return gctx.backgrounds[bg].enabled;
} }
void setBgStatus(Context *ctx, uint_t bg, bool status) noexcept { void setBgStatus(Context &ctx, uint_t bg, bool status) noexcept {
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
gctx.backgrounds[bg].enabled = status; gctx.backgrounds[bg].enabled = status;
} }
void clearTileLayer(Context *ctx, uint_t bgIdx) noexcept { void clearTileLayer(Context &ctx, uint_t bgIdx) noexcept {
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
auto &bg = gctx.cbbs[static_cast<std::size_t>(bgIdx)]; auto &bg = gctx.cbbs[static_cast<std::size_t>(bgIdx)];
initBackgroundBufferObjects(bg); initBackgroundBufferObjects(bg);
bg.updated = true; bg.updated = true;
} }
void hideSprite(Context *ctx, uint_t idx) noexcept { void hideSprite(Context &ctx, uint_t idx) noexcept {
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
auto vbo = &gctx.spriteBlocks.vertices[idx * renderer::SpriteVertexVboLength]; auto vbo = &gctx.spriteBlocks.vertices[idx * renderer::SpriteVertexVboLength];
auto ebo = &gctx.spriteBlocks.elements[idx * renderer::SpriteVertexEboLength]; auto ebo = &gctx.spriteBlocks.elements[idx * renderer::SpriteVertexEboLength];
renderer::setSpriteBufferObject(idx * renderer::SpriteVertexVboRows, 0, renderer::setSpriteBufferObject(idx * renderer::SpriteVertexVboRows, 0,
@ -534,7 +534,7 @@ void hideSprite(Context *ctx, uint_t idx) noexcept {
} }
void setSprite( void setSprite(
Context *ctx, Context &ctx,
uint_t idx, uint_t idx,
int x, int x,
int y, int y,
@ -565,7 +565,7 @@ void setSprite(
const auto dim = dimensions[(spriteShape << 2) | spriteSize]; const auto dim = dimensions[(spriteShape << 2) | spriteSize];
const auto uX = static_cast<int>(x) % 255; const auto uX = static_cast<int>(x) % 255;
const auto uY = static_cast<int>(y + 8) % 255 - 8; const auto uY = static_cast<int>(y + 8) % 255 - 8;
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
auto i = 0u; auto i = 0u;
const auto set = [&](int xIt, int yIt) { const auto set = [&](int xIt, int yIt) {
const auto fX = static_cast<float>(uX + xIt * 8) / 8; const auto fX = static_cast<float>(uX + xIt * 8) / 8;
@ -601,7 +601,7 @@ void setSprite(
} }
void setTile( void setTile(
Context *ctx, Context &ctx,
uint_t bgIdx, uint_t bgIdx,
int column, int column,
int row, int row,
@ -610,7 +610,7 @@ void setTile(
"nostalgia.core.gfx.setTile", "nostalgia.core.gfx.setTile",
"bgIdx: {}, column: {}, row: {}, tile: {}", "bgIdx: {}, column: {}, row: {}, tile: {}",
bgIdx, column, row, tile); bgIdx, column, row, tile);
auto &gctx = glctx(*ctx); auto &gctx = glctx(ctx);
const auto z = static_cast<uint_t>(bgIdx); const auto z = static_cast<uint_t>(bgIdx);
const auto y = static_cast<uint_t>(row); const auto y = static_cast<uint_t>(row);
const auto x = static_cast<uint_t>(column); const auto x = static_cast<uint_t>(column);

View File

@ -63,6 +63,6 @@ class Drawer: public turbine::gl::Drawer {
} }
namespace nostalgia::core { namespace nostalgia::core {
ox::Error initGfx(Context *ctx, InitParams const&) noexcept; ox::Error initGfx(Context &ctx, InitParams const&) noexcept;
void shutdownGfx(Context &ctx) noexcept; void shutdownGfx(Context &ctx) noexcept;
} }

View File

@ -13,12 +13,12 @@ class Scene {
const SceneStatic &m_sceneStatic; const SceneStatic &m_sceneStatic;
public: public:
explicit Scene(const SceneStatic &sceneStatic) noexcept; explicit Scene(SceneStatic const&sceneStatic) noexcept;
ox::Error setupDisplay(core::Context &ctx) const noexcept; ox::Error setupDisplay(core::Context &ctx) const noexcept;
private: private:
void setupLayer(core::Context*, const ox::Vector<uint16_t> &layer, unsigned layerNo) const noexcept; void setupLayer(core::Context&, ox::Vector<uint16_t> const&layer, unsigned layerNo) const noexcept;
}; };

View File

@ -8,7 +8,7 @@
namespace nostalgia::scene { namespace nostalgia::scene {
Scene::Scene(const SceneStatic &sceneStatic) noexcept: Scene::Scene(SceneStatic const&sceneStatic) noexcept:
m_sceneStatic(sceneStatic) { m_sceneStatic(sceneStatic) {
} }
@ -18,19 +18,19 @@ ox::Error Scene::setupDisplay(core::Context &ctx) const noexcept {
} }
const auto &palette = m_sceneStatic.palettes[0]; const auto &palette = m_sceneStatic.palettes[0];
oxReturnError(core::loadBgTileSheet( oxReturnError(core::loadBgTileSheet(
&ctx, 0, m_sceneStatic.tilesheet, palette)); ctx, 0, m_sceneStatic.tilesheet, palette));
// disable all backgrounds // disable all backgrounds
core::setBgStatus(&ctx, 0); core::setBgStatus(ctx, 0);
for (auto layerNo = 0u; const auto &layer : m_sceneStatic.tileMapIdx) { for (auto layerNo = 0u; const auto &layer : m_sceneStatic.tileMapIdx) {
setupLayer(&ctx, layer, layerNo); setupLayer(ctx, layer, layerNo);
++layerNo; ++layerNo;
} }
return {}; return {};
} }
void Scene::setupLayer( void Scene::setupLayer(
core::Context *ctx, core::Context &ctx,
const ox::Vector<uint16_t> &layer, ox::Vector<uint16_t> const&layer,
unsigned layerNo) const noexcept { unsigned layerNo) const noexcept {
core::setBgStatus(ctx, layerNo, true); core::setBgStatus(ctx, layerNo, true);
core::setBgCbb(ctx, layerNo, 0); core::setBgCbb(ctx, layerNo, 0);

View File

@ -11,7 +11,7 @@ namespace nostalgia::scene {
SceneEditorView::SceneEditorView(turbine::Context *tctx, const SceneStatic &sceneStatic): SceneEditorView::SceneEditorView(turbine::Context *tctx, const SceneStatic &sceneStatic):
m_sceneStatic(sceneStatic), m_sceneStatic(sceneStatic),
m_scene(m_sceneStatic) { m_scene(m_sceneStatic) {
oxThrowError(core::init(tctx, {.glInstallDrawer = false}).moveTo(&m_cctx)); oxThrowError(core::init(*tctx, {.glInstallDrawer = false}).moveTo(&m_cctx));
} }
ox::Error SceneEditorView::setupScene() noexcept { ox::Error SceneEditorView::setupScene() noexcept {

View File

@ -34,7 +34,7 @@ static void keyEventHandler(turbine::Context &tctx, turbine::Key key, bool down)
ox::Error run(ox::UniquePtr<ox::FileSystem> &&fs) noexcept { ox::Error run(ox::UniquePtr<ox::FileSystem> &&fs) noexcept {
oxRequireM(tctx, turbine::init(std::move(fs), "Nostalgia")); oxRequireM(tctx, turbine::init(std::move(fs), "Nostalgia"));
oxRequireM(cctx, core::init(tctx.get())); oxRequireM(cctx, core::init(*tctx));
constexpr ox::FileAddress SceneAddr("/Scenes/Chester.nscn"); constexpr ox::FileAddress SceneAddr("/Scenes/Chester.nscn");
oxRequire(scn, keel::readObj<scene::SceneStatic>(tctx->keelCtx, SceneAddr)); oxRequire(scn, keel::readObj<scene::SceneStatic>(tctx->keelCtx, SceneAddr));
turbine::setUpdateHandler(*tctx, updateHandler); turbine::setUpdateHandler(*tctx, updateHandler);

View File

@ -65,7 +65,7 @@ static ox::Error run(const ox::ClArgs &args) noexcept {
oxReturnError(ox::FileSystem32::format(dstBuff.data(), dstBuff.size())); oxReturnError(ox::FileSystem32::format(dstBuff.data(), dstBuff.size()));
ox::FileSystem32 dst(dstBuff); ox::FileSystem32 dst(dstBuff);
oxRequire(ctx, keel::init(ox::make_unique<ox::PassThroughFS>(argSrc), "nost-pack")); oxRequire(ctx, keel::init(ox::make_unique<ox::PassThroughFS>(argSrc), "nost-pack"));
keel::TypeStore ts(*ctx->rom, "/.nostalgia/type_descriptors"); keel::TypeStore ts(*ctx->rom, ox::String("/.nostalgia/type_descriptors"));
oxReturnError(generateTypes(&ts)); oxReturnError(generateTypes(&ts));
oxReturnError(keel::pack(*ctx, ts, dst)); oxReturnError(keel::pack(*ctx, ts, dst));
oxRequireM(pl, keel::GbaPreloader::make()); oxRequireM(pl, keel::GbaPreloader::make());