foxygit / fake-08-xbox360 Log in
commit fc9f839e14878188c2e43c45db6c70039d240a56
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Thu Aug 18 19:10:55 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Thu Aug 18 19:10:55 2022 -0600

    Fix a couple obscure print issues

    *keep track of line height correctly
    *honor character size when smaller than default
---
 source/graphics.cpp       | 17 ++++++++++--
 source/graphics.h         |  9 +++++-
 source/printHelper.cpp    | 32 +++++++++++++--------
 test/graphicstests.cpp    | 71 +++++++++++++++++++++++++++++++++++++++++++++--
 test/printHelperTests.cpp | 43 ++++++++++++++++++++++++++++
 5 files changed, 153 insertions(+), 19 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 5f93f26..8e70090 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1273,8 +1273,15 @@ fix32 Graphics::fillp(fix32 pat) {
 }


-int Graphics::drawCharacter(uint8_t ch, int x, int y, uint8_t printMode) {
+int Graphics::drawCharacter(
+	uint8_t ch,
+	int x,
+	int y,
+	uint8_t printMode,
+	int forceCharWidth,
+	int forceCharHeight) {
 	int extraCharWidth = 0;
+
 	if ((printMode & PRINT_MODE_ON) == PRINT_MODE_ON){
 		int scrW = 4;
 		int scrH = 5;
@@ -1306,10 +1313,14 @@ int Graphics::drawCharacter(uint8_t ch, int x, int y, uint8_t printMode) {
 	else{
 		if (ch >= 0x10 && ch < 0x80) {
 			int index = ch - 0x10;
-			copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8, 4, 5, false, false);
+			int width = forceCharWidth > -1 && forceCharWidth < 4 ? forceCharWidth : 4;
+			int height = forceCharHeight > -1 && forceCharHeight < 5 ? forceCharHeight : 5;
+			copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8, width, height, false, false);
 		} else if (ch >= 0x80) {
 			int index = ch - 0x80;
-			copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5, false, false);
+			int width = forceCharWidth > -1 && forceCharWidth < 4 ? (forceCharWidth + 4) : 8;
+			int height = forceCharHeight > -1 && forceCharHeight < 5 ? forceCharHeight : 5;
+			copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, width, height, false, false);
 			extraCharWidth = 4;
 		}
 	}
diff --git a/source/graphics.h b/source/graphics.h
index c476bc4..c593c3e 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -109,7 +109,14 @@ class Graphics {

 	fix32 fillp(fix32 pat);

-	int drawCharacter(uint8_t ch, int x, int y, uint8_t printMode = 0);
+	int drawCharacter(
+		uint8_t ch,
+		int x,
+		int y,
+		uint8_t printMode = 0,
+		int forceCharWidth = -1,
+		int forceCharHeight = -1);
+
 	std::tuple<int, int> drawCharacterFromBytes(
 		uint8_t chBytes[],
 		int x,
diff --git a/source/printHelper.cpp b/source/printHelper.cpp
index 39260a7..90fc26c 100644
--- a/source/printHelper.cpp
+++ b/source/printHelper.cpp
@@ -86,6 +86,9 @@ int print(std::string str, int x, int y) {
 }

 int print(std::string str, int x, int y, uint8_t c) {
+    if (y == 83) {
+        y +=0;
+    }
 	_ph_graphics->color(c);

 	_ph_mem->drawState.text_x = x;
@@ -95,7 +98,9 @@ int print(std::string str, int x, int y, uint8_t c) {
     int tabStopWidth = 4;
     int charWidth = 4;
     int charHeight = 6;
-    int lineHeight = 6;
+    int lineHeight = 0;
+    int forceCharWidth = -1;
+    int forceCharHeight = -1;
     uint8_t bgColor = 0;
     uint8_t fgColor = _ph_mem->drawState.color;
     uint8_t charBytes[8];
@@ -132,7 +137,8 @@ int print(std::string str, int x, int y, uint8_t c) {

 			ch = str[++n];
 			for(int i = 0; i < times; i++) {
-				x += charWidth +  _ph_graphics->drawCharacter(ch, x, y, printMode);
+                //TODO: combine with other draw character call - fix missing bg? lineheight?
+				x += charWidth +  _ph_graphics->drawCharacter(ch, x, y, printMode, forceCharWidth, forceCharHeight);
 			}
 		}
 		else if (ch == 2) { // "\#{p0}" draw text on a solid background color
@@ -210,15 +216,15 @@ int print(std::string str, int x, int y, uint8_t c) {
             }
             else if (commandChar == 'x'){
                 uint8_t charWidthChar = str[++n];
-                charWidth = p0CharToNum(charWidthChar);
+                forceCharWidth = p0CharToNum(charWidthChar);
+                charWidth = forceCharWidth;
             }
             else if (commandChar == 'y'){
                 //this behaves in a way I wouldn't expect in pico 8- it seems to apply to the next print statment
                 //but not if there is a \n in the string. Not sure if this is a bug or not
                 uint8_t charHeightChar = str[++n];
-                charHeight = p0CharToNum(charHeightChar);
-                //lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
-                lineHeight = charHeight;
+                forceCharHeight = p0CharToNum(charHeightChar);
+                charHeight = forceCharHeight;
             }
             else if (commandChar == 'w'){
                 printMode |= PRINT_MODE_ON;
@@ -229,7 +235,6 @@ int print(std::string str, int x, int y, uint8_t c) {
                 printMode |= PRINT_MODE_ON;
                 printMode |= PRINT_MODE_TALL;
                 charHeight = 12;
-                lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
             }
             else if (commandChar == '='){
                 printMode |= PRINT_MODE_ON;
@@ -256,8 +261,7 @@ int print(std::string str, int x, int y, uint8_t c) {
                 printMode |= PRINT_MODE_SOLID_BG;
             }
             else if (commandChar == ':' || commandChar == '.'){
-                lineHeight = 8;
-                charHeight = 8;
+                charHeight = forceCharHeight > 0 ? forceCharHeight : 8;
                 if (commandChar == ':') {
                     std::string hexStr = str.substr(n+1, 16);
                     n+=16;
@@ -294,7 +298,6 @@ int print(std::string str, int x, int y, uint8_t c) {
                     else if (turnOffModeChar == 't') {
                         printMode &= ~(PRINT_MODE_TALL);
                         charHeight = 6;
-                        lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
                     }
                     else if (turnOffModeChar == '=') {
                         printMode &= ~(PRINT_MODE_STRIPEY);
@@ -305,7 +308,6 @@ int print(std::string str, int x, int y, uint8_t c) {
                         printMode &= ~(PRINT_MODE_STRIPEY);
                         charWidth = 4;
                         charHeight = 6;
-                        lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
                     }
                     else if (turnOffModeChar == 'i') {
                         printMode &= ~(PRINT_MODE_INVERTED);
@@ -329,7 +331,9 @@ int print(std::string str, int x, int y, uint8_t c) {
 		}
 		else if (ch == '\n') {
 			x = homeX;
+            lineHeight = lineHeight > 0 ? lineHeight : 6;
 			y += lineHeight;
+            lineHeight = 0;
 		}
 		else if (ch == '\t') {
 			while (x % (tabStopWidth*4) > 0) {
@@ -343,12 +347,13 @@ int print(std::string str, int x, int y, uint8_t c) {
 			x = homeX;
 		}
 		else if (ch >= 0x10) {
+            lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
             if (bgColor != 0) {
                 uint8_t prevPenColor = _ph_mem->drawState.color;
                 _ph_graphics->rectfill(x-1, y-1, x + charWidth-1, y + lineHeight-1, bgColor);
                 _ph_mem->drawState.color = prevPenColor;
             }
-			x += charWidth + _ph_graphics->drawCharacter(ch, x, y, printMode);
+			x += charWidth + _ph_graphics->drawCharacter(ch, x, y, printMode, forceCharWidth, forceCharHeight);
             while (framesToPause > 0){
                 _ph_vm->vm_flip();

@@ -359,7 +364,9 @@ int print(std::string str, int x, int y, uint8_t c) {
         //soft wrap if enabled
         if (rhsWrap > 0 && x >= rhsWrap) {
             x = homeX;
+            lineHeight = lineHeight > 0 ? lineHeight : 6;
 			y += lineHeight;
+            lineHeight = 0;
         }
 	}

@@ -367,6 +374,7 @@ int print(std::string str, int x, int y, uint8_t c) {
 		_ph_mem->drawState.drawPaletteMap[i] = prevDrawPal[i];
 	}

+    lineHeight = lineHeight > 0 ? lineHeight : 6;
 	//todo: auto scrolling
 	_ph_mem->drawState.text_y = y + lineHeight;

diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index d296939..eb76264 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1975,7 +1975,7 @@ TEST_CASE("graphics class behaves as expected") {
         checkPoints(graphics, expectedPoints);
     }
     */
-    SUBCASE("Remap spritesheet to screen"){
+    SUBCASE("Remap spritesheet to screen (after drawing character to screen)"){
         graphics->cls();

         //emulate print("zo",8,0)
@@ -2241,11 +2241,76 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK_EQ(picoRam.data[0xFFFF], 9);
         CHECK_EQ(picoRam.userData[0x7FFF], 9);
     }
-
+    SUBCASE("drawCharacter with forced width and height for wide character"){
+        graphics->cls();
+
+        //emulate print("\^x0\^y1▤a",10,10,9)
+        picoRam.drawState.drawPaletteMap[7] = 9;
+        graphics->drawCharacter(152, 10, 10, 0, 0, 1);
+        //the a should be invisible (0 width)
+        graphics->drawCharacter(65, 10, 10, 0, 0, 1);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {9,10,0},
+            {10,10,9},
+            {11,10,9},
+            {12,10,9},
+            {13,10,9},
+            {14,10,0},
+            {9,11,0},
+            {10,11,0},
+            {11,11,0},
+            {12,11,0},
+            {13,11,0},
+            {14,11,0},
+            {9,12,0},
+            {10,12,0},
+            {11,12,0},
+            {12,12,0},
+            {13,12,0},
+            {14,12,0},

+       };

+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("drawCharacter with forced width and height for normal character"){
+        graphics->cls();

-
+        //emulate print("\^x3\^y3a",10,10,9)
+        picoRam.drawState.drawPaletteMap[7] = 9;
+        graphics->drawCharacter(97, 10, 10, 0, 3, 3);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {9,10,0},
+            {10,10,9},
+            {11,10,9},
+            {12,10,9},
+            {13,10,0},
+            {14,10,0},
+            {9,11,0},
+            {10,11,9},
+            {11,11,0},
+            {12,11,9},
+            {13,11,0},
+            {14,11,0},
+            {9,12,0},
+            {10,12,9},
+            {11,12,9},
+            {12,12,9},
+            {13,12,0},
+            {14,12,0},
+            {9,14,0},
+            {10,14,0},
+            {11,14,0},
+            {12,14,0},
+            {13,14,0},
+            {14,14,0},
+
+       };
+
+        checkPoints(graphics, expectedPoints);
+    }

     //general teardown
     delete graphics;
diff --git a/test/printHelperTests.cpp b/test/printHelperTests.cpp
index 734d8d0..c42413c 100644
--- a/test/printHelperTests.cpp
+++ b/test/printHelperTests.cpp
@@ -694,6 +694,49 @@ TEST_CASE("Print helper functions") {

         checkPoints(graphics, expectedPoints);
     }
+    SUBCASE("p8scii special control code for char width(\\^x) and char height (\\^y) limit rendering ") {
+        graphics->cls();
+
+        print("\x06""x2\x06""y3a", 0, 0);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {0, 0, 6},
+            {1, 0, 6},
+            {2, 0, 0},
+            {3, 0, 0},
+
+            {0, 1, 6},
+            {1, 1, 0},
+            {2, 1, 0},
+            {3, 1, 0},
+
+            {0, 2, 6},
+            {1, 2, 6},
+            {2, 2, 0},
+            {3, 2, 0},
+
+            {0, 3, 0},
+            {1, 3, 0},
+            {2, 3, 0},
+            {3, 3, 0},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("p8scii special control code for char height (\\^y) sets line height correctly when lower") {
+        graphics->cls();
+
+        print("\x06""y3a", 0, 0);
+
+        CHECK_EQ(memory->drawState.text_y, 3);
+    }
+    SUBCASE("p8scii special control code for char height (\\^y) sets line height correctly when higher") {
+        graphics->cls();
+
+        print("\x06""y9a", 0, 0);
+
+        CHECK_EQ(memory->drawState.text_y, 9);
+    }


     delete stubHost;