foxygit / fake-08-xbox360 Log in
commit d482dcb35ce8d994dd95c713c76fb5447bd34049
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Thu Apr 1 20:38:18 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Thu Apr 1 20:38:18 2021 -0600

    Fix bugs printing, update z8lua as well

    handle nil like pico 8 does in split
---
 libs/z8lua                                  |   2 +-
 source/graphics.cpp                         |  21 +++++++++------
 source/graphics.h                           |   2 +-
 source/p8GlobalLuaFunctions.h               |  18 -------------
 source/picoluaapi.cpp                       |  23 +++++++++++-----
 test/carts/boldtexttest.p8                  |  36 +++++++++++++++++++++++++
 test/carts/nilpairstest.p8                  |  24 +++++++++++++++++
 test/carts/peek4test.p8                     |  19 ++++++-------
 test/carts/screenshots/boldtexttest_f01.png | Bin 0 -> 550 bytes
 test/carts/screenshots/nilpairstest_f01.png | Bin 0 -> 481 bytes
 test/carts/screenshots/peek4test_f01.png    | Bin 999 -> 0 bytes
 test/carts/screenshots/peek4test_f02.png    | Bin 0 -> 1061 bytes
 test/carts/screenshots/splittest_f01.png    | Bin 0 -> 604 bytes
 test/carts/splittest.p8                     |  25 +++++++++++++++++
 test/endtoendtests.cpp                      |  40 +++++++++++++++++++++++++---
 test/graphicstests.cpp                      |  19 +++++++++++++
 16 files changed, 182 insertions(+), 47 deletions(-)

diff --git a/libs/z8lua b/libs/z8lua
index 64a8b2e..2fa711b 160000
--- a/libs/z8lua
+++ b/libs/z8lua
@@ -1 +1 @@
-Subproject commit 64a8b2edce26df38402e2ceb3af628c8477b1d35
+Subproject commit 2fa711ba48b9ec7a20911831bfe031098d6e7b75
diff --git a/source/graphics.cpp b/source/graphics.cpp
index b48928a..0de9d9d 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1017,20 +1017,24 @@ int Graphics::print(std::string str, int x, int y) {
 }

 //based on tac08 impl
-int Graphics::print(std::string str, int x, int y, uint16_t c) {
+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 prevCol7Map = _memory->drawState.drawPaletteMap[7];
-	bool prevCol0Transp = isColorTransparent(0);
+	uint8_t prevDrawPal[16];
+	for(uint8_t i = 0; i < 16; i++) {
+		prevDrawPal[i] = _memory->drawState.drawPaletteMap[i];
+		_memory->drawState.drawPaletteMap[i] = i;
+	}

-	pal(7, c, 0);
-	palt(7, false);
-	palt(0, true);
+	_memory->drawState.drawPaletteMap[7] = effectiveC;
+	_memory->drawState.drawPaletteMap[0] = 16; //transparent


 	for (size_t n = 0; n < str.length(); n++) {
@@ -1049,8 +1053,9 @@ int Graphics::print(std::string str, int x, int y, uint16_t c) {
 		}
 	}

-	_memory->drawState.drawPaletteMap[7] = prevCol7Map;
-	palt(0, prevCol0Transp);
+	for(int i = 0; i < 16; i++) {
+		_memory->drawState.drawPaletteMap[i] = prevDrawPal[i];
+	}

 	//todo: auto scrolling

diff --git a/source/graphics.h b/source/graphics.h
index aab886c..14934d8 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -147,7 +147,7 @@ class Graphics {

 	int print(std::string str);
 	int print(std::string str, int x, int y);
-	int print(std::string str, int x, int y, uint16_t c);
+	int print(std::string str, int x, int y, uint8_t c);

 	void spr(
 		int n,
diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index 43e474f..d2ebe73 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -84,24 +84,6 @@ function deli(c,i)
     end
 end

---TODO: move this to lua impl
-origpairs = pairs
-pairs = function(x)
- if x ~= nil then
-	return origpairs(x)
- end
-
- return function() end
-end
-
-origipairs = ipairs
-ipairs = function(x)
- if x ~= nil then
-	return origipairs(x)
- end
-
- return function() end
-end

 ---------------------------------
 --Coroutines
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index baae4f3..acebceb 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -291,11 +291,13 @@ int rectfill(lua_State *L){
 }

 int print(lua_State *L){
-    if (lua_gettop(L) == 0){
+    int numArgs = lua_gettop(L);
+    if (numArgs == 0){
         return 0;
     }

     const char * str = "";
+    int newx = 0;

     //todo: handle other cases, maybe move this somewhere else
     //learned this from zepto8 https://github.com/samhocevar/zepto8/blob/27f83fe0626d4823fe2a33568d8310d8def84ae9/src/pico8/vm.cpp
@@ -315,14 +317,20 @@ int print(lua_State *L){
         str = "[function]";
     }

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

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

         uint8_t c = lua_tonumber(L,4);

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

-    return 0;
+    lua_pushinteger(L, newx);
+    return 1;
 }

 int spr(lua_State *L) {
diff --git a/test/carts/boldtexttest.p8 b/test/carts/boldtexttest.p8
new file mode 100644
index 0000000..1a47c22
--- /dev/null
+++ b/test/carts/boldtexttest.p8
@@ -0,0 +1,36 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function brd(f)
+ apal(2)
+ camera(-1,0) f()
+ camera(1,0) f()
+ camera(0,-1) f()
+ camera(0,1) f()
+ pal()
+ camera() f()
+end
+
+function apal(n)
+ for i=0,15 do pal(i,n) end
+end
+
+
+function _draw()
+	cls(10)
+
+	local s="press ❎ to start"
+ function f()
+  print(s,64-2*#s,88,15)
+ end
+ brd(f)
+
+end
+
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/nilpairstest.p8 b/test/carts/nilpairstest.p8
new file mode 100644
index 0000000..0d07c21
--- /dev/null
+++ b/test/carts/nilpairstest.p8
@@ -0,0 +1,24 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _draw()
+ cls()
+ func = pairs(nil)
+ func()
+
+ print("func: " .. tostr(func))
+ print("func(): " .. (tostr(func()) or "nil"))
+ print(pairs(nil))
+	for k,v in pairs(nil)
+	do
+		print(k)
+	end
+	print("end of loop")
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/peek4test.p8 b/test/carts/peek4test.p8
index f288014..22410ab 100644
--- a/test/carts/peek4test.p8
+++ b/test/carts/peek4test.p8
@@ -1,10 +1,10 @@
 pico-8 cartridge // http://www.pico-8.com
-version 29
+version 32
 __lua__
-orig1 = peek4(0x500)
-orig2 = peek4(0x504)
-orig3 = peek4(0x508)
-orig4 = peek4(0x50c)
+orig1 = peek4(0x5f00)
+orig2 = peek4(0x5f04)
+orig3 = peek4(0x5f08)
+orig4 = peek4(0x5f0c)
 cartdata("dummy")

 function set_layer(top)
@@ -23,11 +23,12 @@ end

 function _draw()
  cls()
+ color(15)

- print(orig1)
-	print(orig2)
-	print(orig3)
-	print(orig4)
+ print(orig1,15)
+	print(orig2,15)
+	print(orig3,15)
+	print(orig4,15)

  print("")
  set_layer(true)
diff --git a/test/carts/screenshots/boldtexttest_f01.png b/test/carts/screenshots/boldtexttest_f01.png
new file mode 100644
index 0000000..032718c
Binary files /dev/null and b/test/carts/screenshots/boldtexttest_f01.png differ
diff --git a/test/carts/screenshots/nilpairstest_f01.png b/test/carts/screenshots/nilpairstest_f01.png
new file mode 100644
index 0000000..cad6695
Binary files /dev/null and b/test/carts/screenshots/nilpairstest_f01.png differ
diff --git a/test/carts/screenshots/peek4test_f01.png b/test/carts/screenshots/peek4test_f01.png
deleted file mode 100644
index 0e92107..0000000
Binary files a/test/carts/screenshots/peek4test_f01.png and /dev/null differ
diff --git a/test/carts/screenshots/peek4test_f02.png b/test/carts/screenshots/peek4test_f02.png
new file mode 100644
index 0000000..e6e3e5a
Binary files /dev/null and b/test/carts/screenshots/peek4test_f02.png differ
diff --git a/test/carts/screenshots/splittest_f01.png b/test/carts/screenshots/splittest_f01.png
new file mode 100644
index 0000000..337bd64
Binary files /dev/null and b/test/carts/screenshots/splittest_f01.png differ
diff --git a/test/carts/splittest.p8 b/test/carts/splittest.p8
new file mode 100644
index 0000000..d83cf23
--- /dev/null
+++ b/test/carts/splittest.p8
@@ -0,0 +1,25 @@
+pico-8 cartridge // http://www.pico-8.com
+version 32
+__lua__
+function _draw()
+ cls()
+ print("ex 1", 3)
+	foreach (split("1,2,3"), print) -- returns {1,2,3}
+ print("ex 2", 4)
+ foreach (split("one:two:3",":",false), print) -- returns {"one","two","3"}
+ print("ex 3", 5)
+ foreach (split("1,,2,"), print) -- returns {1,"",2,""}
+ print("ex 4", 7)
+ foreach (split(nil), print)
+ print(tostr(split(nil)))
+ local nilsplit = split(nil)
+ print(nilsplit)
+ print(type(nilsplit))
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index daea8dd..7c934ff 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -48,10 +48,14 @@ bool verifyScreenshot(Vm* vm, std::string screenshotFilename) {
         uint8_t c = getPixelNibble(x, y, picoFb);
         Color col = paletteColors[screenPaletteMap[c]];

-        bool pixelMatches = r == col.Red && g == col.Green && b == col.Blue;
+        //black varies by pico 8 version. allow for either so I don't have to
+        //retake all the screenshots
+        bool pixelIsBlack = (r == 0 && g == 0 && b == 0) || (r == 2 && g == 4 && b == 8);
+        bool pixelMatches =
+            (pixelIsBlack && c == 0) ||
+            (r == col.Red && g == col.Green && b == col.Blue);

         if (!pixelMatches) {
-
             printf("Non-matching pixel at idx %d (%d,%d): rgb: %d,%d,%d\n", pixIdx, x, y, r, g, b);

             printf("fake 08 color is %d rgb: %d,%d,%d\n", c, col.Red, col.Green, col.Blue);
@@ -444,9 +448,11 @@ TEST_CASE("Loading and running carts") {
             CHECK(vm->GetBiosError() == "");
         }
         SUBCASE("sceen matches screenshot"){
+            //first frame doesn't have uniform colors
+            vm->UpdateAndDraw();
             vm->UpdateAndDraw();

-            CHECK(verifyScreenshot(vm, "carts/screenshots/peek4test_f01.png"));
+            CHECK(verifyScreenshot(vm, "carts/screenshots/peek4test_f02.png"));
         }

         vm->CloseCart();
@@ -465,6 +471,34 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("pairs() with nil arg test"){
+        vm->LoadCart("carts/nilpairstest.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot"){
+            vm->UpdateAndDraw();
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/nilpairstest_f01.png"));
+        }
+
+        vm->CloseCart();
+    }
+    SUBCASE("split() test"){
+        vm->LoadCart("carts/splittest.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot"){
+            vm->UpdateAndDraw();
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/splittest_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
     delete host;
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 72335e8..93ecee3 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -703,6 +703,25 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK(picoRam.drawState.text_y == 18);
         CHECK(picoRam.drawState.color == 14);
     }
+    SUBCASE("print({str}) uses pal mapped color") {
+        graphics->cls();
+        graphics->color(2);
+        graphics->pal(2, 12, 0);
+
+        graphics->print("t");
+
+        std::vector<coloredPoint> expectedPoints = {
+            {0, 0, 12},
+            {1, 0, 12},
+            {2, 0, 12},
+            {1, 1, 12},
+            {1, 2, 12},
+            {1, 3, 12},
+            {1, 4, 12}
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
     SUBCASE("spr(...) draws to screen at location") {
         graphics->cls();
         for(uint8_t i = 0; i < 16; i++) {