foxygit / fake-08-xbox360 Log in
commit 69781229e915944c2e667f406617a84efe387353
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Tue Mar 8 19:43:06 2022 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Tue Mar 8 19:43:06 2022 -0700

    WIP- refactor print to new location

    allow for p8scii control codes to pause, play audio, etc.
    appears to build and run, but tests need work
---
 source/graphics.cpp    | 127 ------------------------------------------
 source/graphics.h      |   4 --
 source/picoluaapi.cpp  |  13 +++--
 source/picoluaapi.h    |   3 +-
 source/printHelper.cpp | 147 +++++++++++++++++++++++++++++++++++++++++++++++++
 source/printHelper.h   |  16 ++++++
 source/vm.cpp          |   2 +-
 7 files changed, 174 insertions(+), 138 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index d791280..e4cc78d 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1247,133 +1247,6 @@ fix32 Graphics::fillp(fix32 pat) {
 	return z8::fix32::frombits(prev);
 }

-int Graphics::print(std::string str) {
-	int result = this->print(str, _memory->drawState.text_x, _memory->drawState.text_y);
-
-	return result;
-}
-
-int Graphics::print(std::string str, int x, int y) {
-	return this->print(str, x, y, _memory->drawState.color);
-}
-
-uint8_t p0CharToNum(uint8_t hexChar){
-	uint8_t num = 0;
-	if (hexChar > 47 && hexChar < 58){
-		num = hexChar - 48;
-	}
-	if (hexChar > 96) {
-		num = hexChar - 87;
-	}
-
-	return num;
-
-}
-
-//based on tac08 impl
-int Graphics::print(std::string str, int x, int y, uint8_t c) {
-	color(c);
-
-	_memory->drawState.text_x = x;
-	_memory->drawState.text_y = y;
-
-	uint8_t effectiveC = getDrawPalMappedColor(c);
-
-	//font sprite sheet has text as color 7, with 0 as transparent. We need to override
-	//these values and restore them after
-	uint8_t prevDrawPal[16];
-	for(uint8_t i = 0; i < 16; i++) {
-		prevDrawPal[i] = _memory->drawState.drawPaletteMap[i];
-		_memory->drawState.drawPaletteMap[i] = i;
-	}
-
-	_memory->drawState.drawPaletteMap[7] = effectiveC;
-	_memory->drawState.drawPaletteMap[0] = 16; //transparent
-
-
-	for (size_t n = 0; n < str.length(); n++) {
-		uint8_t ch = str[n];
-		if (ch == 1) { // "\*{p0}" repeat the next character p0 times
-			uint8_t timesChr = str[++n];
-			int times = p0CharToNum(timesChr);
-
-			ch = str[++n];
-			for(int i = 0; i < times; i++) {
-				x = drawCharacter(ch, x, y);
-			}
-		}
-		else if (ch == 2) { // "\#{p0}" draw text on a solid background color
-			uint8_t bgColChar = str[++n];
-			uint8_t bgCol = p0CharToNum(bgColChar);
-
-			_memory->drawState.drawPaletteMap[0] = bgCol;
-		}
-		else if (ch == 3) { // "\-{p0}" move text cursor horizontally by 16-p0 pixels
-			uint8_t pixelCountChar = str[++n];
-			int deltaX = p0CharToNum(pixelCountChar) - 16;
-
-			x += deltaX;
-			//update memory cursor state?
-		}
-		else if (ch == 4) { // "\|{p0}" move text cursor vertically by 16-p0 pixels
-			uint8_t pixelCountChar = str[++n];
-			int deltaY = p0CharToNum(pixelCountChar) - 16;
-
-			y += deltaY;
-			//update memory cursor state?
-		}
-		else if (ch == 5) { // "\+{p0}" move text cursor vertically by 16-p0 pixels
-			uint8_t pixelCountXChar = str[++n];
-			uint8_t pixelCountYChar = str[++n];
-			int deltaX = p0CharToNum(pixelCountXChar) - 16;
-			int deltaY = p0CharToNum(pixelCountYChar) - 16;
-
-			x += deltaX;
-			y += deltaY;
-			//update memory cursor state?
-		}
-		else if (ch == 6) { // "\^" special command
-			uint8_t commandChar = str[++n];
-			if (commandChar > 48 && commandChar < 58){
-				//pause for x num frames
-			}
-
-		}
-		else if (ch == 12) { //"\f{p0}" draw text with this foreground color
-			uint8_t fgColChar = str[++n];
-			uint8_t fgCol = p0CharToNum(fgColChar);
-			color(fgCol);
-			_memory->drawState.drawPaletteMap[7] = getDrawPalMappedColor(fgCol);
-		}
-		else if (ch == '\n') {
-			x = _memory->drawState.text_x;
-			y += 6;
-		}
-		else if (ch == '\t') {
-			while (x % 16 > 0) {
-				x++;
-			}
-		}
-		else if (ch == '\b') {
-			x -= 4;
-		}
-		else if (ch == '\r') {
-			x = _memory->drawState.text_x;
-		}
-		else if (ch >= 0x10 && ch < 0x80) {
-			x = drawCharacter(ch, x, y);
-		}
-	}
-
-	for(int i = 0; i < 16; i++) {
-		_memory->drawState.drawPaletteMap[i] = prevDrawPal[i];
-	}
-
-	//todo: auto scrolling
-	_memory->drawState.text_y += 6;
-
-	return x;
-}

 int Graphics::drawCharacter(uint8_t ch, int x, int y) {
 	if (ch >= 0x10 && ch < 0x80) {
diff --git a/source/graphics.h b/source/graphics.h
index a03f676..85d9eb7 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -108,10 +108,6 @@ class Graphics {

 	fix32 fillp(fix32 pat);

-	int print(std::string str);
-	int print(std::string str, int x, int y);
-	int print(std::string str, int x, int y, uint8_t c);
-
 	int drawCharacter(uint8_t ch, int x, int y);

 	void spr(
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 3838c2d..2995960 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -9,6 +9,7 @@ using namespace std;
 #include "Input.h"
 #include "vm.h"
 #include "logger.h"
+#include "printHelper.h"

 //extern "C" {
   #include <lua.h>
@@ -21,11 +22,13 @@ Input* _inputForLuaApi;
 Vm* _vmForLuaApi;
 Audio* _audioForLuaApi;

-void initPicoApi(Graphics* graphics, Input* input, Vm* vm, Audio* audio){
+void initPicoApi(PicoRam* memory, Graphics* graphics, Input* input, Vm* vm, Audio* audio){
     _graphicsForLuaApi = graphics;
     _inputForLuaApi = input;
     _vmForLuaApi = vm;
     _audioForLuaApi = audio;
+
+    initPrintHelper(memory, _graphicsForLuaApi, _vmForLuaApi, _audioForLuaApi);
 }

 int noop(const char * name) {
@@ -318,19 +321,19 @@ int print(lua_State *L){
     }

     if (numArgs < 2) {
-        newx = _graphicsForLuaApi->print(str);
+        newx = print(str);
     }
     else if (numArgs == 2) {
         uint8_t c = lua_tonumber(L,2);

         _graphicsForLuaApi->color(c);
-        newx = _graphicsForLuaApi->print(str);
+        newx = print(str);
     }
     else if (numArgs == 3) {
         int x = lua_tonumber(L,2);
         int y = lua_tonumber(L,3);

-        newx = _graphicsForLuaApi->print(str, x, y);
+        newx = print(str, x, y);
     }
     else {
         int x = lua_tonumber(L,2);
@@ -338,7 +341,7 @@ int print(lua_State *L){

         uint8_t c = lua_tonumber(L,4);

-        newx = _graphicsForLuaApi->print(str, x, y, c);
+        newx = print(str, x, y, c);
     }

     lua_pushinteger(L, newx);
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 24cf749..226dd83 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -11,9 +11,10 @@
 #include "graphics.h"
 #include "Input.h"
 #include "vm.h"
+#include "PicoRam.h"

 //this can probably go away when I'm loading actual carts and just have to expose api to lua
-void initPicoApi(Graphics* graphics, Input* input, Vm* vm, Audio* audio);
+void initPicoApi(PicoRam* memory, Graphics* graphics, Input* input, Vm* vm, Audio* audio);

 //graphics api
 int cls(lua_State *L);
diff --git a/source/printHelper.cpp b/source/printHelper.cpp
new file mode 100644
index 0000000..e2271a8
--- /dev/null
+++ b/source/printHelper.cpp
@@ -0,0 +1,147 @@
+#include <string>
+
+#include "printHelper.h"
+
+#include <string>
+#include "graphics.h"
+#include "vm.h"
+#include "Audio.h"
+
+PicoRam* _ph_mem;
+Graphics* _ph_graphics;
+Vm* _ph_vm;
+Audio* _ph_audio;
+
+void initPrintHelper(PicoRam* memory, Graphics* graphics, Vm* vm, Audio* audio) {
+    _ph_mem = memory;
+    _ph_graphics = graphics;
+    _ph_vm = vm;
+    _ph_audio = audio;
+}
+
+uint8_t p0CharToNum(uint8_t hexChar){
+	uint8_t num = 0;
+	if (hexChar > 47 && hexChar < 58){
+		num = hexChar - 48;
+	}
+	if (hexChar > 96) {
+		num = hexChar - 87;
+	}
+
+	return num;
+
+}
+
+int print(std::string str) {
+	int result = print(str, _ph_mem->drawState.text_x, _ph_mem->drawState.text_y);
+
+	return result;
+}
+
+int print(std::string str, int x, int y) {
+	return print(str, x, y, _ph_mem->drawState.color);
+}
+
+int print(std::string str, int x, int y, uint8_t c) {
+	_ph_graphics->color(c);
+
+	_ph_mem->drawState.text_x = x;
+	_ph_mem->drawState.text_y = y;
+
+	uint8_t effectiveC = _ph_graphics->getDrawPalMappedColor(c);
+
+	//font sprite sheet has text as color 7, with 0 as transparent. We need to override
+	//these values and restore them after
+	uint8_t prevDrawPal[16];
+	for(uint8_t i = 0; i < 16; i++) {
+		prevDrawPal[i] = _ph_mem->drawState.drawPaletteMap[i];
+		_ph_mem->drawState.drawPaletteMap[i] = i;
+	}
+
+	_ph_mem->drawState.drawPaletteMap[7] = effectiveC;
+	_ph_mem->drawState.drawPaletteMap[0] = 16; //transparent
+
+
+	for (size_t n = 0; n < str.length(); n++) {
+		uint8_t ch = str[n];
+		if (ch == 1) { // "\*{p0}" repeat the next character p0 times
+			uint8_t timesChr = str[++n];
+			int times = p0CharToNum(timesChr);
+
+			ch = str[++n];
+			for(int i = 0; i < times; i++) {
+				x = _ph_graphics->drawCharacter(ch, x, y);
+			}
+		}
+		else if (ch == 2) { // "\#{p0}" draw text on a solid background color
+			uint8_t bgColChar = str[++n];
+			uint8_t bgCol = p0CharToNum(bgColChar);
+
+			_ph_mem->drawState.drawPaletteMap[0] = bgCol;
+		}
+		else if (ch == 3) { // "\-{p0}" move text cursor horizontally by 16-p0 pixels
+			uint8_t pixelCountChar = str[++n];
+			int deltaX = p0CharToNum(pixelCountChar) - 16;
+
+			x += deltaX;
+			//update memory cursor state?
+		}
+		else if (ch == 4) { // "\|{p0}" move text cursor vertically by 16-p0 pixels
+			uint8_t pixelCountChar = str[++n];
+			int deltaY = p0CharToNum(pixelCountChar) - 16;
+
+			y += deltaY;
+			//update memory cursor state?
+		}
+		else if (ch == 5) { // "\+{p0}" move text cursor vertically by 16-p0 pixels
+			uint8_t pixelCountXChar = str[++n];
+			uint8_t pixelCountYChar = str[++n];
+			int deltaX = p0CharToNum(pixelCountXChar) - 16;
+			int deltaY = p0CharToNum(pixelCountYChar) - 16;
+
+			x += deltaX;
+			y += deltaY;
+			//update memory cursor state?
+		}
+		else if (ch == 6) { // "\^" special command
+			uint8_t commandChar = str[++n];
+			if (commandChar > 48 && commandChar < 58){
+				//pause for x num frames
+			}
+
+		}
+		else if (ch == 12) { //"\f{p0}" draw text with this foreground color
+			uint8_t fgColChar = str[++n];
+			uint8_t fgCol = p0CharToNum(fgColChar);
+			_ph_graphics->color(fgCol);
+			_ph_mem->drawState.drawPaletteMap[7] = _ph_graphics->getDrawPalMappedColor(fgCol);
+		}
+		else if (ch == '\n') {
+			x = _ph_mem->drawState.text_x;
+			y += 6;
+		}
+		else if (ch == '\t') {
+			while (x % 16 > 0) {
+				x++;
+			}
+		}
+		else if (ch == '\b') {
+			x -= 4;
+		}
+		else if (ch == '\r') {
+			x = _ph_mem->drawState.text_x;
+		}
+		else if (ch >= 0x10 && ch < 0x80) {
+			x = _ph_graphics->drawCharacter(ch, x, y);
+		}
+	}
+
+	for(int i = 0; i < 16; i++) {
+		_ph_mem->drawState.drawPaletteMap[i] = prevDrawPal[i];
+	}
+
+	//todo: auto scrolling
+	_ph_mem->drawState.text_y += 6;
+
+	return x;
+}
\ No newline at end of file
diff --git a/source/printHelper.h b/source/printHelper.h
new file mode 100644
index 0000000..bb94cc3
--- /dev/null
+++ b/source/printHelper.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <string>
+#include "graphics.h"
+#include "vm.h"
+#include "Audio.h"
+#include "PicoRam.h"
+
+
+void initPrintHelper(PicoRam* memory, Graphics* graphics, Vm* vm, Audio* audio);
+
+int print(std::string str);
+
+int print(std::string str, int x, int y);
+
+int print(std::string str, int x, int y, uint8_t c);
\ No newline at end of file
diff --git a/source/vm.cpp b/source/vm.cpp
index d5589fd..bf86701 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -80,7 +80,7 @@ Vm::Vm(

     //this can probably go away when I'm loading actual carts and just have to expose api to lua
     Logger_Write("Initializing global api\n");
-    initPicoApi(_graphics, _input, this, _audio);
+    initPicoApi(_memory, _graphics, _input, this, _audio);
     //initGlobalApi(_graphics);

 }