From 507c894c15a9f840117c864f37d04c944942d568 Mon Sep 17 00:00:00 2001
From: Gary Talent <gtalent2@gmail.com>
Date: Thu, 12 Oct 2017 04:50:33 -0500
Subject: [PATCH] Add Context to core

---
 src/nostalgia/common/CMakeLists.txt |  1 -
 src/nostalgia/core/context.hpp      | 21 +++++++++++++++++++++
 src/nostalgia/core/core.cpp         |  9 +++++----
 src/nostalgia/core/core.hpp         |  5 ++++-
 src/nostalgia/core/gba/gfx.cpp      | 24 +++++++++++++++---------
 src/nostalgia/core/gfx.hpp          |  8 +++++---
 src/nostalgia/core/qt/gfx.cpp       | 11 ++++++-----
 src/nostalgia/player/main.cpp       | 11 ++++++-----
 8 files changed, 62 insertions(+), 28 deletions(-)
 create mode 100644 src/nostalgia/core/context.hpp

diff --git a/src/nostalgia/common/CMakeLists.txt b/src/nostalgia/common/CMakeLists.txt
index 4d7375ad..64f222ae 100644
--- a/src/nostalgia/common/CMakeLists.txt
+++ b/src/nostalgia/common/CMakeLists.txt
@@ -1,4 +1,3 @@
-cmake_minimum_required(VERSION 2.8.11)
 
 add_library(
 	NostalgiaCommon
diff --git a/src/nostalgia/core/context.hpp b/src/nostalgia/core/context.hpp
new file mode 100644
index 00000000..27e5d9ff
--- /dev/null
+++ b/src/nostalgia/core/context.hpp
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2016-2017 gtalent2@gmail.com
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <ox/fs/filesystem.hpp>
+
+namespace nostalgia {
+namespace core {
+
+// User Input Output
+struct Context {
+	ox::FileSystem32 *rom = nullptr;
+};
+
+}
+}
+
diff --git a/src/nostalgia/core/core.cpp b/src/nostalgia/core/core.cpp
index 5b00ff84..489ca583 100644
--- a/src/nostalgia/core/core.cpp
+++ b/src/nostalgia/core/core.cpp
@@ -5,14 +5,15 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
-#include <ox/std/types.hpp>
-#include "gfx.hpp"
+
+#include "core.hpp"
 
 namespace nostalgia {
 namespace core {
 
-ox::Error init() {
-	auto err = initGfx();
+ox::Error init(Context *ctx) {
+	ox::Error err = 0;
+	err = initGfx(ctx);
 	return err;
 }
 
diff --git a/src/nostalgia/core/core.hpp b/src/nostalgia/core/core.hpp
index f56c0a3c..1d0d2e83 100644
--- a/src/nostalgia/core/core.hpp
+++ b/src/nostalgia/core/core.hpp
@@ -5,14 +5,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
+
 #pragma once
 
+#include <ox/fs/filesystem.hpp>
+
 #include "gfx.hpp"
 
 namespace nostalgia {
 namespace core {
 
-ox::Error init();
+ox::Error init(Context *ctx);
 
 }
 }
diff --git a/src/nostalgia/core/gba/gfx.cpp b/src/nostalgia/core/gba/gfx.cpp
index b3ac3f8e..9886b839 100644
--- a/src/nostalgia/core/gba/gfx.cpp
+++ b/src/nostalgia/core/gba/gfx.cpp
@@ -116,7 +116,7 @@ static char charMap[128] = {
 	26, // Z
 };
 
-ox::Error initGfx() {
+ox::Error initGfx(Context *ctx) {
 	/* Sprite Mode ----\ */
 	/*             ---\| */
 	/* Background 0 -\|| */
@@ -127,31 +127,37 @@ ox::Error initGfx() {
 	return 0;
 }
 
-void initConsole() {
-	auto charsetInode = 101;
+ox::Error initConsole(Context *ctx) {
+	const auto CharsetInode = 101;
+	const auto PaletteStart = sizeof(GbaImageDataHeader);
+	ox::Error err = 0;
 	auto fs = (FileStore32*) findMedia();
 
 	GbaImageDataHeader imgData;
-	fs->read(101, 0, sizeof(imgData), &imgData, nullptr);
 
 	REG_BG0CNT = (28 << 8) | 1;
 	if (fs) {
+		err |= fs->read(CharsetInode, 0, sizeof(imgData), &imgData, nullptr);
+
 		// load palette
-		fs->read(charsetInode, sizeof(GbaImageDataHeader),
+		err |= fs->read(CharsetInode, PaletteStart,
 					512, (uint16_t*) &MEM_PALLETE_BG[0], nullptr);
 
 		if (imgData.bpp == 4) {
-			fs->read(charsetInode, __builtin_offsetof(GbaImageData, tiles),
+			err |= fs->read(CharsetInode, __builtin_offsetof(GbaImageData, tiles),
 			         sizeof(Tile) * imgData.tileCount, (uint16_t*) &TILE_ADDR[0][1], nullptr);
-		} else {
+		} else if (imgData.bpp == 4) {
 			REG_BG0CNT |= (1 << 7); // set to use 8 bits per pixel
-			fs->read(charsetInode, __builtin_offsetof(GbaImageData, tiles),
+			err |= fs->read(CharsetInode, __builtin_offsetof(GbaImageData, tiles),
 			         sizeof(Tile8) * imgData.tileCount, (uint16_t*) &TILE8_ADDR[0][1], nullptr);
 		}
+	} else {
+		err = 1;
 	}
+	return err;
 }
 
-void puts(int loc, const char *str) {
+void puts(Context *ctx, int loc, const char *str) {
 	for (int i = 0; str[i]; i++) {
 		MEM_BG_MAP[28][loc + i] = charMap[(int) str[i]];
 	}
diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp
index 98037475..e86c264a 100644
--- a/src/nostalgia/core/gfx.hpp
+++ b/src/nostalgia/core/gfx.hpp
@@ -9,14 +9,16 @@
 
 #include <ox/std/types.hpp>
 
+#include "context.hpp"
+
 namespace nostalgia {
 namespace core {
 
-ox::Error initGfx();
+ox::Error initGfx(Context *ctx);
 
-void initConsole();
+ox::Error initConsole(Context *ctx);
 
-void puts(int loc, const char *str);
+void puts(Context *ctx, int loc, const char *str);
 
 }
 }
diff --git a/src/nostalgia/core/qt/gfx.cpp b/src/nostalgia/core/qt/gfx.cpp
index 21b92fe7..0c090adb 100644
--- a/src/nostalgia/core/qt/gfx.cpp
+++ b/src/nostalgia/core/qt/gfx.cpp
@@ -6,19 +6,20 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
-#include <ox/std/std.hpp>
+#include "../gfx.hpp"
 
 namespace nostalgia {
 namespace core {
 
-ox::Error initGfx() {
-	return 0;
+ox::Error initGfx(Context *ctx) {
+	return 1;
 }
 
-void initConsole() {
+ox::Error initConsole(Context *ctx) {
+	return 1;
 }
 
-void puts(int loc, const char *str) {
+void puts(Context *ctx, int loc, const char *str) {
 }
 
 }
diff --git a/src/nostalgia/player/main.cpp b/src/nostalgia/player/main.cpp
index 9b30a539..c2de8ac1 100644
--- a/src/nostalgia/player/main.cpp
+++ b/src/nostalgia/player/main.cpp
@@ -8,13 +8,14 @@
 
 #include <nostalgia/core/core.hpp>
 
-using namespace nostalgia;
+using namespace nostalgia::core;
 
 int main() {
-	core::init();
-	core::initConsole();
-	core::puts(9 * 32 + 8, "HELLO,WORLD!");
-	core::puts(10 * 32 + 8, "01234 56789");
+	Context ctx;
+	init(&ctx);
+	initConsole(&ctx);
+	puts(&ctx, 9 * 32 + 8, "HELLO,WORLD!");
+	puts(&ctx, 10 * 32 + 8, "01234 56789");
 	while (1);
 	return 0;
 }