foxygit / fake-08-xbox360 Log in
commit 56551b444eafc3dbfa028d317c8d9079c71d244e
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Wed Oct 18 21:58:30 2023 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Wed Oct 18 21:58:30 2023 -0600

    Fix regressions in print()

    fix cursor x position after printing a wide char
    ignore palette mapping color 0 for the background when printing
---
 source/filehelpers.cpp    | 14 ++++++++++++
 source/filehelpers.h      |  2 ++
 source/graphics.cpp       |  6 +++++-
 source/printHelper.cpp    | 12 +++++------
 test/endtoendtests.cpp    | 54 ++++++++++++++++++++++++++++++++++++++++++++++-
 test/printHelperTests.cpp | 15 +++++++++++++
 6 files changed, 95 insertions(+), 8 deletions(-)

diff --git a/source/filehelpers.cpp b/source/filehelpers.cpp
index 9af5385..ffdbb12 100644
--- a/source/filehelpers.cpp
+++ b/source/filehelpers.cpp
@@ -1,6 +1,8 @@
 #include <string>
 #include <fstream>
 #include <vector>
+#include <filesystem>
+namespace fs = std::filesystem;

 //http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html
 std::string get_file_contents(std::string filename){
@@ -126,3 +128,15 @@ bool isCPostFile (std::string const &fullString) {
        fullString.rfind("cpost", 0) == 0;
 }

+std::vector<std::string> get_cart_files_in_dir(std::string directory){
+    std::vector<std::string> files;
+    for (const auto & entry : fs::directory_iterator(directory)) {
+        std::string path = entry.path().string();
+        if (isCartFile(path)) {
+            files.push_back(path.substr(path.find_last_of("/\\") + 1));
+        }
+    }
+
+    return files;
+}
+
diff --git a/source/filehelpers.h b/source/filehelpers.h
index 1482ca4..0251204 100644
--- a/source/filehelpers.h
+++ b/source/filehelpers.h
@@ -20,3 +20,5 @@ bool isHiddenFile (std::string const &fullString);
 bool isAbsolutePath (std::string const &path);

 std::string getFileExtension(std::string const &path);
+
+std::vector<std::string> get_cart_files_in_dir(std::string directory);
diff --git a/source/graphics.cpp b/source/graphics.cpp
index cf4942e..56fee12 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1324,6 +1324,10 @@ int Graphics::drawCharacter(
 			? forceCharWidth > -1 && forceCharWidth < 4 ? forceCharWidth : defaultCharWidth
 			: forceCharWidth > -1 && forceCharWidth < 4 ? (forceCharWidth + 4) : defaultWideCharWidth;

+		if (ch >= 0x80) {
+			extraCharWidth = defaultWideCharWidth - defaultCharWidth;
+		}
+
 		uint8_t charHeight = forceCharHeight > -1 && forceCharHeight < 5 ? forceCharHeight : defaultCharHeight;

 		auto result = drawCharacterFromBytes(
@@ -1336,7 +1340,7 @@ int Graphics::drawCharacter(
 			charWidth,
 			charHeight
 		);
-		extraCharWidth = get<0>(result);
+		extraCharWidth += get<0>(result);
 	}

 	if ((printMode & PRINT_MODE_ON) == PRINT_MODE_ON){
diff --git a/source/printHelper.cpp b/source/printHelper.cpp
index c7b435b..2b423a9 100644
--- a/source/printHelper.cpp
+++ b/source/printHelper.cpp
@@ -144,7 +144,7 @@ int print(std::string str, int x, int y, uint8_t c) {
     int lineHeight = 0;
     int forceCharWidth = -1;
     int forceCharHeight = -1;
-    uint8_t bgColor = 0;
+    uint8_t bgColor = 0xff;
     uint8_t fgColor = _ph_mem->drawState.color;
     uint8_t charBytes[8];
     uint8_t audioStrBytes[32];
@@ -189,7 +189,7 @@ int print(std::string str, int x, int y, uint8_t c) {
                     x,
                     y,
                     drawPal[fgColor & 0x0f],
-                    drawPal[bgColor & 0x0f],
+                    bgColor == 0xff ? 0 : drawPal[bgColor & 0x0f],
                     printMode,
                     forceCharWidth,
                     forceCharHeight);
@@ -334,7 +334,7 @@ int print(std::string str, int x, int y, uint8_t c) {
                     x,
                     y,
                     drawPal[fgColor & 0x0f],
-                    drawPal[bgColor & 0x0f],
+                    bgColor == 0xff ? 0 : drawPal[bgColor & 0x0f],
                     printMode,
                     8,
                     charHeight);
@@ -431,7 +431,7 @@ int print(std::string str, int x, int y, uint8_t c) {
                 prevX + xOffset,
                 prevY + yOffset,
                 drawPal[fgColor & 0x0f],
-                drawPal[bgColor & 0x0f],
+                bgColor == 0xff ? 0 : drawPal[bgColor & 0x0f],
                 printMode,
                 forceCharWidth,
                 forceCharHeight);
@@ -466,7 +466,7 @@ int print(std::string str, int x, int y, uint8_t c) {
 		}
 		else if (ch >= 0x10) {
             lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
-            if (bgColor != 0) {
+            if (bgColor != 0xff) {
                 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;
@@ -478,7 +478,7 @@ int print(std::string str, int x, int y, uint8_t c) {
                 x,
                 y,
                 drawPal[fgColor & 0x0f],
-                drawPal[bgColor & 0x0f],
+                bgColor == 0xff ? 0 : drawPal[bgColor & 0x0f],
                 printMode,
                 forceCharWidth,
                 forceCharHeight);
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 44009bc..4cfb290 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -22,7 +22,7 @@ bool verifyScreenshot(Vm* vm, Host* host, std::string screenshotFilename) {
         error = lodepng::decode(image, width, height, png);
     }
     if (error) {
-        CHECK_MESSAGE(error == 0, "Unable to decode screenshot png");
+        CHECK_MESSAGE(error == 0, "Unable to decode screenshot png %s", screenshotFilename.c_str());
         return false;
     }

@@ -75,6 +75,45 @@ TEST_CASE("Loading and running carts") {
     Host* host = new Host();
     Vm* vm = new Vm(host);

+    /*
+    SUBCASE("test carts") {
+        // char cwd[PATH_MAX];
+        // if (getcwd(cwd, sizeof(cwd)) != NULL) {
+        //     printf("Current working dir: %s\n", cwd);
+        // }
+        auto screenshots = get_cart_files_in_dir("carts/screenshots");
+
+        for (auto screenshot : screenshots) {
+            if (screenshot.length() < 8) {
+                continue;
+            }
+            auto last8Chars = screenshot.substr(screenshot.length() - 8);
+            if (last8Chars != "_f01.png") {
+                continue;
+            }
+            auto cartfile = screenshot.substr(0, screenshot.length() - 8) + ".p8";
+            vm->LoadCart(cartfile, false);
+            vm->vm_reset();
+
+            SUBCASE("No error reported"){
+                CHECK(vm->GetBiosError() == "");
+            }
+            if (getFileExtension(cartfile) == ".p8") {
+                SUBCASE(cartfile.c_str()){
+                    vm->UpdateAndDraw();
+
+                    std::stringstream ss;
+                    ss << "carts/screenshots/" << screenshot;
+
+                    CHECK(verifyScreenshot(vm, host, ss.str()));
+                }
+            }
+
+            vm->CloseCart();
+        }
+    }
+    */
+
     SUBCASE("Load simple cart"){
         vm->LoadCart("cartparsetest.p8", false);

@@ -762,7 +801,20 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("bold text with wide char test"){
+        vm->LoadCart("boldtexttest.p8", false);

+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot"){
+            vm->UpdateAndDraw();
+
+            CHECK(verifyScreenshot(vm, host, "carts/screenshots/boldtexttest_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
     delete host;
diff --git a/test/printHelperTests.cpp b/test/printHelperTests.cpp
index e4c9194..95ac09a 100644
--- a/test/printHelperTests.cpp
+++ b/test/printHelperTests.cpp
@@ -833,7 +833,22 @@ TEST_CASE("Print helper functions") {

         checkPoints(graphics, expectedPoints);
     }
+    SUBCASE("pal mapping color 0 to another color does not color background") {
+        graphics->cls();
+        graphics->pal(0, 10, 0);
+
+        print("0", 0, 0, 10);

+        std::vector<coloredPoint> expectedPoints = {
+            {0, 0, 10}, {1, 0, 10}, {2, 0, 10},
+            {0, 1, 10}, {1, 1, 0}, {2, 1, 10},
+            {0, 2, 10}, {1, 2, 0}, {2, 2, 10},
+            {0, 3, 10}, {1, 3, 0}, {2, 3, 10},
+            {0, 4, 10}, {1, 4, 10}, {2, 4, 10},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }

     delete stubHost;
     delete graphics;