foxygit / fake-08-xbox360 Log in
commit cdbff740529e3ed4ff1d1af5281010f918311c9e
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sun Mar 6 20:02:17 2022 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sun Mar 6 20:02:17 2022 -0700

    Implement tab, backspace, and tab control codes
---
 source/graphics.cpp    | 20 ++++++++++++++++----
 test/graphicstests.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index f520989..6adf18c 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1280,7 +1280,22 @@ int Graphics::print(std::string str, int x, int y, uint8_t c) {

 	for (size_t n = 0; n < str.length(); n++) {
 		uint8_t ch = str[n];
-		if (ch >= 0x10 && ch < 0x80) {
+		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) {
 			int index = ch - 0x10;
 			copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8, 4, 5, false, false);
 			x += 4;
@@ -1288,9 +1303,6 @@ int Graphics::print(std::string str, int x, int y, uint8_t c) {
 			int index = ch - 0x80;
 			copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5, false, false);
 			x += 8;
-		} else if (ch == '\n') {
-			x = _memory->drawState.text_x;
-			y += 6;
 		}
 	}

diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index bea62ce..a9b362f 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -2399,6 +2399,51 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK_EQ(picoRam.data[0xFFFF], 9);
         CHECK_EQ(picoRam.userData[0x7FFF], 9);
     }
+    SUBCASE("p8scii tab character advances to next x multiple of 16 (default tab size)") {
+        graphics->cls();
+
+        graphics->print("a\t:", 0, 0);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {17, 0, 0},
+            {17, 1, 6},
+            {17, 2, 0},
+            {17, 3, 6},
+            {17, 4, 0}
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("p8scii backspace character moves cursor backward 4 pixels (doesn't erase)") {
+        graphics->cls();
+
+        graphics->print("i\b-", 0, 0);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {0, 0, 6}, {1, 0, 6}, {2, 0, 6},
+            {0, 1, 0}, {1, 1, 6}, {2, 1, 0},
+            {0, 2, 6}, {1, 2, 6}, {2, 2, 6},
+            {0, 3, 0}, {1, 3, 6}, {2, 3, 0},
+            {0, 4, 6}, {1, 4, 6}, {2, 4, 6},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("p8scii carriage return character moves cursor back to current cursor x(doesn't erase)") {
+        graphics->cls();
+
+        graphics->print("i\r-", 10, 0);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {10, 0, 6}, {11, 0, 6}, {12, 0, 6},
+            {10, 1, 0}, {11, 1, 6}, {12, 1, 0},
+            {10, 2, 6}, {11, 2, 6}, {12, 2, 6},
+            {10, 3, 0}, {11, 3, 6}, {12, 3, 0},
+            {10, 4, 6}, {11, 4, 6}, {12, 4, 6},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }