foxygit / fake-08-xbox360 Log in
commit 24cc5eb53b67f32f04c322361880cd9558afb5d3
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sun Mar 21 11:50:39 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sun Mar 21 11:50:39 2021 -0600

    Add support for passing a table to pal
---
 source/picoluaapi.cpp                       |  32 ++++++++++++++++++++++++----
 test/carts/paltabletest.p8                  |  20 +++++++++++++++++
 test/carts/screenshots/paltabletest_f01.png | Bin 0 -> 356 bytes
 test/endtoendtests.cpp                      |  14 ++++++++++++
 4 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 013c7e6..2817ac4 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -558,19 +558,43 @@ int map(lua_State *L) {
 }

 int pal(lua_State *L) {
-    if (lua_gettop(L) == 0) {
+    int numArgs = lua_gettop(L);
+    if (numArgs == 0) {
         _graphicsForLuaApi->pal();

         return 0;
     }

+    uint8_t p = 0;
+    uint8_t c0 = 0;
+    uint8_t c1 = 0;
+
+    if (lua_istable(L, 1)){
+        if (numArgs > 1) {
+            p = lua_tonumber(L,2);
+        }
+
+        /* table is in the stack at index 't' */
+        lua_pushnil(L);  /* first key */
+        while (lua_next(L, 1) != 0) {
+            if (lua_isnumber(L, -2) && lua_isnumber(L, -1)) {
+                c0 = lua_tonumber(L, -2);
+                c1 = lua_tonumber(L, -1);

-    uint8_t c0 = lua_tonumber(L,1);
-    uint8_t c1 = c0;
+                _graphicsForLuaApi->pal(c0, c1, p);
+            }
+            lua_pop(L, 1);
+        }
+
+        return 0;
+    }
+
+
+    c0 = lua_tonumber(L,1);
+    c1 = c0;
     if (lua_gettop(L) > 1) {
         c1 = lua_tonumber(L,2);
     }
-    uint8_t p = 0;

     if (lua_gettop(L) > 2){
         p = lua_tonumber(L,3);
diff --git a/test/carts/paltabletest.p8 b/test/carts/paltabletest.p8
new file mode 100644
index 0000000..9529896
--- /dev/null
+++ b/test/carts/paltabletest.p8
@@ -0,0 +1,20 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _draw()
+ cls()
+	pal(10,11)
+	rectfill(5,5,10,10,10)
+
+	pal({[12]=9, [14]=8})
+
+	rectfill(15,15,20,20,12)
+	rectfill(25,25,30,30,14)
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/paltabletest_f01.png b/test/carts/screenshots/paltabletest_f01.png
new file mode 100644
index 0000000..5b0d4f2
Binary files /dev/null and b/test/carts/screenshots/paltabletest_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 3e31e72..01692d0 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -450,6 +450,20 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("pal (with table) test cart"){
+        vm->LoadCart("carts/paltabletest.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot"){
+            vm->UpdateAndDraw();
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/paltabletest_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
     delete host;