From 7105c19c723d02bbd30a5a6a449d62857c0a695c Mon Sep 17 00:00:00 2001
From: Gary Talent <gtalent2@gmail.com>
Date: Fri, 1 Nov 2019 01:31:11 -0500
Subject: [PATCH] [nostalgia/core/gba] Add hardware target types for loading
 tile maps and palettes

---
 src/nostalgia/core/gba/gfx.cpp | 41 ++++++++++++++++++++++++++++++++++
 src/nostalgia/core/gfx.hpp     | 14 ++++++------
 2 files changed, 48 insertions(+), 7 deletions(-)

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<typename T>
+ox::Error modelRead(T *io, GbaPaletteTarget *t) {
+	io->setTypeInfo("nostalgia::core::NostalgiaPalette", NostalgiaPalette::Fields);
+	oxReturnError(io->template field<uint16_t>("colors", [t](auto i, uint16_t c) {
+		t->palette[i] = c;
+		return OxError(0);
+	}));
+	return OxError(0);
+}
+
+template<typename T>
+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<uint8_t>("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<uint8_t> tiles;
 };
 
+template<typename T>
+ox::Error model(T *io, NostalgiaPalette *pal) {
+	io->setTypeInfo("nostalgia::core::NostalgiaPalette", NostalgiaPalette::Fields);
+	oxReturnError(io->field("colors", &pal->colors));
+	return OxError(0);
+}
+
 template<typename T>
 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<typename T>
-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();