foxygit / fake-08-xbox360 Log in
commit 179d0838f59470a71511405ee238a6601c7e57e1
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jul 9 17:13:03 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Jul 9 17:13:03 2022 -0600

    One off character printing seems to be working

    More work to be done, but this is at a decent enough spot to add
---
 source/graphics.cpp                          |  62 ++++++++++++++++++++++-----
 source/graphics.h                            |   9 +++-
 source/printHelper.cpp                       |  57 ++++++++++++++----------
 test/carts/one_off_chars.p8                  |  25 +++++++++++
 test/carts/screenshots/one_off_chars_f01.png | Bin 0 -> 931 bytes
 test/endtoendtests.cpp                       |  14 ++++++
 test/printHelperTests.cpp                    |  58 +++++++++++++++++++++++++
 7 files changed, 191 insertions(+), 34 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 632d1e6..5f93f26 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1317,23 +1317,65 @@ int Graphics::drawCharacter(uint8_t ch, int x, int y, uint8_t printMode) {
 	return extraCharWidth;
 }

-int Graphics::drawCharacterFromBytes(std::vector<uint8_t> chBytes, int x, int y, uint8_t printMode) {
+std::tuple<int, int> Graphics::drawCharacterFromBytes(
+	uint8_t chBytes[],
+	int x,
+	int y,
+	uint8_t fgColor,
+	uint8_t bgColor,
+	uint8_t printMode) {
+
+	applyCameraToPoint(&x, &y);
+	uint8_t *screenBuffer = GetP8FrameBuffer();
+
 	int extraCharWidth = 0;
-	if (chBytes.size() != 8){
-		return 0;
-	}
+	int extraCharHeight = 0;
+	//these may need to get passed in later when drawing normal chars
+	int charWidth = 8;
+	int charHeight = 8;
+	int wFactor = 1;
+	int hFactor = 1;
 	//TODO: character modes
+	bool evenPxOnly = false;

-	for (size_t i = 0; i < chBytes.size(); i++) {
-		for(uint8_t bitn = 0; bitn < 8; bitn++) {
-			bool on = BITMASK(bitn) & chBytes[i];
-			if (on) {
-				pset(x + bitn, y+ i);
+	if ((printMode & PRINT_MODE_ON) == PRINT_MODE_ON){
+		if ((printMode & PRINT_MODE_WIDE) == PRINT_MODE_WIDE) {
+			wFactor = 2;
+			extraCharWidth = 8;
+		}
+		if((printMode & PRINT_MODE_TALL) == PRINT_MODE_TALL) {
+			hFactor = 2;
+			extraCharHeight = 8;
+		}
+		if((printMode & PRINT_MODE_STRIPEY) == PRINT_MODE_STRIPEY) {
+			//draw every other pixel-- also kinda broken on pico 8 0.2.4
+			evenPxOnly = true;
+		}
+		//TODO: other print modes
+	}
+
+	if (bgColor != 0) {
+		uint8_t prevPenColor = _memory->drawState.color;
+		rectfill(x-1, y-1, x + charWidth*wFactor - 1, y + charHeight*hFactor - 1, bgColor);
+		_memory->drawState.color = prevPenColor;
+	}
+
+	for (size_t i = 0; i < charHeight * hFactor; i++) {
+		for(uint8_t bitn = 0; bitn < charWidth * wFactor; bitn++) {
+			bool on = BITMASK(bitn / wFactor) & chBytes[i / hFactor];
+			on &= wFactor == 1 || !evenPxOnly || (i % 2 == 0);
+			on &= wFactor == 1 || !evenPxOnly || (bitn % 2 == 0);
+			const int pixX = x + bitn;
+			const int pixY = y + i;
+			if (on && isWithinClip(pixX, pixY)) {
+				setPixelNibble(pixX, pixY, fgColor, screenBuffer);
 			}
 		}
 	}

-	return extraCharWidth;
+	std::tuple<int, int> retVal (extraCharWidth, extraCharHeight);
+
+	return retVal;
 }

 void Graphics::spr(
diff --git a/source/graphics.h b/source/graphics.h
index 428c7fe..c476bc4 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -2,7 +2,6 @@

 #include <string>
 #include <tuple>
-#include <vector>
 #include "hostVmShared.h"
 #include "PicoRam.h"
 #include <fix32.h>
@@ -111,7 +110,13 @@ class Graphics {
 	fix32 fillp(fix32 pat);

 	int drawCharacter(uint8_t ch, int x, int y, uint8_t printMode = 0);
-	int drawCharacterFromBytes(std::vector<uint8_t> chBytes, int x, int y, uint8_t printMode = 0);
+	std::tuple<int, int> drawCharacterFromBytes(
+		uint8_t chBytes[],
+		int x,
+		int y,
+		uint8_t fgColor,
+		uint8_t bgColor,
+		uint8_t printMode);

 	void spr(
 		int n,
diff --git a/source/printHelper.cpp b/source/printHelper.cpp
index 4618f6b..79dcfab 100644
--- a/source/printHelper.cpp
+++ b/source/printHelper.cpp
@@ -12,18 +12,15 @@ Graphics* _ph_graphics;
 Vm* _ph_vm;
 Audio* _ph_audio;

-std::vector<uint8_t> oneOffCharToBytes(std::string hex) {
-  std::vector<uint8_t> bytes;
+void oneOffCharToBytes(std::string hex, uint8_t byteBuff[]) {
   char buff[3];
   buff[2] = 0;
   for (unsigned int i = 0; i < hex.length(); i += 2) {
-    buff[0] = hex[i+ 1];
-    buff[1] = hex[i];
+    buff[0] = hex[i];
+    buff[1] = hex[i+1];
     uint8_t byte = (uint8_t) strtol(buff, NULL, 16);
-    bytes.push_back(byte);
+    byteBuff[i/2] = byte;
   }
-
-  return bytes;
 }

 void initPrintHelper(PicoRam* memory, Graphics* graphics, Vm* vm, Audio* audio) {
@@ -100,7 +97,8 @@ int print(std::string str, int x, int y, uint8_t c) {
     int charHeight = 6;
     int lineHeight = 6;
     uint8_t bgColor = 0;
-    std::vector<uint8_t> charBytes;
+    uint8_t fgColor = _ph_mem->drawState.color;
+    uint8_t charBytes[8];

     uint8_t printMode = _ph_mem->hwState.printAttributes;

@@ -257,19 +255,34 @@ int print(std::string str, int x, int y, uint8_t c) {
                 printMode |= PRINT_MODE_ON;
                 printMode |= PRINT_MODE_SOLID_BG;
             }
-            else if (commandChar == ':'){
-                std::string hexStr = str.substr(n, 16);
-                n+=16;
-                charBytes = oneOffCharToBytes(hexStr);
+            else if (commandChar == ':' || commandChar == '.'){
+                lineHeight = 8;
+                charHeight = 8;
+                if (commandChar == ':') {
+                    std::string hexStr = str.substr(n+1, 16);
+                    n+=16;
+                    oneOffCharToBytes(hexStr, charBytes);
+                }
+                else {
+                    std::string binStr = str.substr(n+1, 8);
+                    n+=8;
+                    for(size_t i = 0; i < 8; i++) {
+                        charBytes[i] = binStr[i];
+                    }
+                }

                 //TODO: combine with other text rendering
-                x += charWidth + _ph_graphics->drawCharacterFromBytes(charBytes, x, y, printMode);
-            }
-            else if (commandChar == '.'){
-                std::string binStr = str.substr(n, 8);
-                n+=8;
-                //todo: convert bin string to char bytes
-
+                auto values = _ph_graphics->drawCharacterFromBytes(
+                    charBytes,
+                    x,
+                    y,
+                    prevDrawPal[fgColor & 0x0f],
+                    bgColor,
+                    printMode);
+
+                x += 8 + get<0>(values);
+                charHeight = charHeight + get<1>(values);
+                lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
             }
             else if (commandChar == '-'){
                 uint8_t turnOffModeChar = str[++n];
@@ -309,9 +322,9 @@ int print(std::string str, int x, int y, uint8_t c) {
 		}
 		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);
+			fgColor = p0CharToNum(fgColChar);
+			_ph_graphics->color(fgColor);
+			_ph_mem->drawState.drawPaletteMap[7] = _ph_graphics->getDrawPalMappedColor(fgColor);
 		}
 		else if (ch == '\n') {
 			x = _ph_mem->drawState.text_x;
diff --git a/test/carts/one_off_chars.p8 b/test/carts/one_off_chars.p8
new file mode 100644
index 0000000..d6f6fb3
--- /dev/null
+++ b/test/carts/one_off_chars.p8
@@ -0,0 +1,25 @@
+pico-8 cartridge // http://www.pico-8.com
+version 35
+__lua__
+function _draw()
+ cls(1)
+ pal()
+ ?"\f7\#4\^p\^:447cb67c3e7f0106"
+ ?"\f8hey\^:0ff00000000000ff"
+ ?"\f6wat\^:447cb67c3e7f0106"
+ ?"\^.d|へ|>○\1\6cat"
+ ?"\^:041f051f141f0400other string"
+ pal(4,14)
+ ?"\#4\^:1f1b111b1f0e0400"
+ ?"\f3\^:0804061f0c040200"
+ ?"\^:081c226b221c0800"
+ pal(3,15)
+ ?"\^w\^:447cb67c3e7f0106"
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/one_off_chars_f01.png b/test/carts/screenshots/one_off_chars_f01.png
new file mode 100644
index 0000000..425e66c
Binary files /dev/null and b/test/carts/screenshots/one_off_chars_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index f15cbf0..afd2bec 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -668,6 +668,20 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("One off char printing"){
+        vm->LoadCart("one_off_chars.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot"){
+            vm->UpdateAndDraw();
+
+            CHECK(verifyScreenshot(vm, host, "carts/screenshots/one_off_chars_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
     delete host;
diff --git a/test/printHelperTests.cpp b/test/printHelperTests.cpp
index ccc3ea0..30b021c 100644
--- a/test/printHelperTests.cpp
+++ b/test/printHelperTests.cpp
@@ -551,6 +551,64 @@ TEST_CASE("Print helper functions") {

         checkPoints(graphics, expectedPoints);
     }
+    SUBCASE("p8scii special control code for one off character(\\^:) (colored)") {
+        graphics->cls();
+
+        print("\x0c""2\x06"":447cb67c3e7f0106", 0, 0);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {0, 0, 0},
+            {1, 0, 0},
+            {2, 0, 2},
+            {3, 0, 0},
+            {4, 0, 0},
+            {5, 0, 0},
+            {6, 0, 2},
+            {7, 0, 0},
+            {0, 1, 0},
+            {1, 1, 0},
+            {2, 1, 2},
+            {3, 1, 2},
+            {4, 1, 2},
+            {5, 1, 2},
+            {6, 1, 2},
+            {7, 1, 0},
+            {0, 2, 0},
+            {1, 2, 2},
+            {2, 2, 2},
+            {3, 2, 0},
+            {4, 2, 2},
+            {5, 2, 2},
+            {6, 2, 0},
+            {7, 2, 2},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("p8scii special control code for one off character(\\^:) (pinballed with bg)") {
+        graphics->cls();
+
+        print("\x02""4\x06""p\x06"":447cb67c3e7f0106", 0, 0);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {0, 0, 4},
+            {1, 0, 4},
+            {2, 0, 4},
+            {3, 0, 4},
+            {4, 0, 6},
+            {5, 0, 4},
+            {6, 0, 4},
+            {7, 0, 4},
+            {8, 0, 4},
+            {9, 0, 4},
+            {10, 0, 4},
+            {11, 0, 4},
+            {12, 0, 6},
+            {13, 0, 4},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
     SUBCASE("Poke default print mode but not turned on") {
         graphics->cls();