diff --git a/src/nostalgia/core/gba/gfx.cpp b/src/nostalgia/core/gba/gfx.cpp index 480e2c53..27dbff09 100644 --- a/src/nostalgia/core/gba/gfx.cpp +++ b/src/nostalgia/core/gba/gfx.cpp @@ -158,6 +158,47 @@ static char charMap[128] = { 0, // ~ }; +struct GbaPaletteTarget { + uint16_t *palette = nullptr; +}; + +struct GbaTileMapTarget { + static constexpr auto Fields = 4; + uint8_t bpp = 0; + ox::FileAddress defaultPalette; + GbaPaletteTarget pal; + uint16_t *tileMap = nullptr; +}; + +template +ox::Error modelRead(T *io, GbaPaletteTarget *t) { + io->setTypeInfo("nostalgia::core::NostalgiaPalette", NostalgiaPalette::Fields); + oxReturnError(io->template field("colors", [t](auto i, uint16_t c) { + t->palette[i] = c; + return OxError(0); + })); + return OxError(0); +} + +template +ox::Error modelRead(T *io, GbaTileMapTarget *t) { + io->setTypeInfo("nostalgia::core::NostalgiaGraphic", NostalgiaGraphic::Fields); + oxReturnError(io->field("bpp", &t->bpp)); + oxReturnError(io->field("defaultPalette", &t->defaultPalette)); + oxReturnError(io->field("pal", &t->pal)); + uint16_t intermediate; + oxReturnError(io->template field("tileMap", [t, &intermediate](auto i, uint8_t tile) { + if (i & 1) { // i is odd + intermediate |= tile >> 8; + t->tileMap[i / 2] = intermediate; + } else { // i is even + intermediate = tile & 0xff; + } + return OxError(0); + })); + return OxError(0); +} + ox::Error initGfx(Context*) { /* Sprite Mode ----\ */ /* ---\| */ diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index 4075e17b..460a6789 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -35,6 +35,13 @@ struct NostalgiaGraphic { ox::Vector tiles; }; +template +ox::Error model(T *io, NostalgiaPalette *pal) { + io->setTypeInfo("nostalgia::core::NostalgiaPalette", NostalgiaPalette::Fields); + oxReturnError(io->field("colors", &pal->colors)); + return OxError(0); +} + template ox::Error model(T *io, NostalgiaGraphic *ng) { io->setTypeInfo("nostalgia::core::NostalgiaGraphic", NostalgiaGraphic::Fields); @@ -45,13 +52,6 @@ ox::Error model(T *io, NostalgiaGraphic *ng) { return OxError(0); } -template -ox::Error model(T *io, NostalgiaPalette *pal) { - io->setTypeInfo("nostalgia::core::NostalgiaPalette", NostalgiaPalette::Fields); - oxReturnError(io->field("colors", &pal->colors)); - return OxError(0); -} - [[nodiscard]] ox::Error initGfx(Context *ctx); [[nodiscard]] ox::Error shutdownGfx();