foxygit / fake-08-xbox360 Log in
commit 13454f92f5266212d81381b2da3bc950351c8519
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jan 3 18:50:01 2026 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Jan 3 18:50:01 2026 -0700

    Correctly handle negative color values in various lua functions
---
 source/picoluaapi.cpp                            |  42 +++++++++++------------
 test/carts/neg_scrn_pal_test.p8                  |  18 ++++++++++
 test/carts/screenshots/neg_scrn_pal_test_f01.png | Bin 0 -> 286 bytes
 test/endtoendtests.cpp                           |  16 +++++++++
 test/vmtests.cpp                                 |  36 +++++++++++++++++++
 5 files changed, 91 insertions(+), 21 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 7f7f2ce..3dccbd6 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -90,10 +90,10 @@ int pget(lua_State *L){

 int color(lua_State *L){
     uint8_t prev = 0;
-    uint8_t c = 0;
+    int16_t c = 0;
     if (lua_gettop(L) > 0) {
         c = lua_tonumber(L,1);
-        prev = _graphicsForLuaApi->color((uint8_t)c);
+        prev = _graphicsForLuaApi->color(c);
     }
     else {
         prev = _graphicsForLuaApi->color();
@@ -122,7 +122,7 @@ int line (lua_State *L){
     else if (lua_gettop(L) == 3) {
         int x1 = lua_tonumber(L,1);
         int y1 = lua_tonumber(L,2);
-        uint8_t c = lua_tonumber(L,3);
+        int16_t c = lua_tonumber(L,3);

         _graphicsForLuaApi->line(x1, y1, c);
     }
@@ -139,7 +139,7 @@ int line (lua_State *L){
         int y1 = lua_tonumber(L,2);
         int x2 = lua_tonumber(L,3);
         int y2 = lua_tonumber(L,4);
-        uint8_t c = lua_tonumber(L,5);
+        int16_t c = lua_tonumber(L,5);

         _graphicsForLuaApi->line(x1, y1, x2, y2, c);
     }
@@ -182,7 +182,7 @@ int circ(lua_State *L){
     }
     else if (lua_gettop(L) > 3){
         int r = lua_tonumber(L,3);
-        uint8_t c = lua_tonumber(L,4);
+        int16_t c = lua_tonumber(L,4);

         _graphicsForLuaApi->circ(ox, oy, r, c);
     }
@@ -203,7 +203,7 @@ int circfill(lua_State *L){
     }
     else if (lua_gettop(L) > 3){
         int r = lua_tonumber(L,3);
-        uint8_t c = lua_tonumber(L,4);
+        int16_t c = lua_tonumber(L,4);

         _graphicsForLuaApi->circfill(ox, oy, r, c);
     }
@@ -224,7 +224,7 @@ int oval(lua_State *L){

         }
         else {
-            uint8_t c = lua_tonumber(L,5);
+            int16_t c = lua_tonumber(L,5);

             _graphicsForLuaApi->oval(x1, y1, x2, y2, c);
         }
@@ -245,7 +245,7 @@ int ovalfill(lua_State *L){

         }
         else {
-            fix32 c = lua_tonumber(L,5);
+            int16_t c = lua_tonumber(L,5);

             _graphicsForLuaApi->ovalfill(x1, y1, x2, y2, c);
         }
@@ -267,7 +267,7 @@ int rect(lua_State *L){

         }
         else {
-            uint8_t c = lua_tonumber(L,5);
+            int16_t c = lua_tonumber(L,5);

             _graphicsForLuaApi->rect(x1, y1, x2, y2, c);
         }
@@ -288,7 +288,7 @@ int rectfill(lua_State *L){

         }
         else {
-            fix32 c = lua_tonumber(L,5);
+            int16_t c = lua_tonumber(L,5);

             _graphicsForLuaApi->rectfill(x1, y1, x2, y2, c);
         }
@@ -310,7 +310,7 @@ int rrect(lua_State *L){
             _graphicsForLuaApi->rrect(x, y, w, h, r);
         }
         else {
-            uint8_t c = lua_tonumber(L,6);
+            int16_t c = lua_tonumber(L,6);

             _graphicsForLuaApi->rrect(x, y, w, h, r, c);
         }
@@ -331,7 +331,7 @@ int rrectfill(lua_State *L){
             _graphicsForLuaApi->rrectfill(x, y, w, h, r);
         }
         else {
-            fix32 c = lua_tonumber(L,6);
+            int16_t c = lua_tonumber(L,6);

             _graphicsForLuaApi->rrectfill(x, y, w, h, r, c);
         }
@@ -378,7 +378,7 @@ int print(lua_State *L){
         newx = print(str);
     }
     else if (numArgs == 2) {
-        uint8_t c = lua_tonumber(L,2);
+        int16_t c = lua_tonumber(L,2);

         _graphicsForLuaApi->color(c);
         newx = print(str);
@@ -393,7 +393,7 @@ int print(lua_State *L){
         int x = lua_tonumber(L,2);
         int y = lua_tonumber(L,3);

-        uint8_t c = lua_tonumber(L,4);
+        int16_t c = lua_tonumber(L,4);

         newx = print(str, x, y, c);
     }
@@ -523,7 +523,7 @@ int sget(lua_State *L) {
 int sset(lua_State *L) {
     int x = lua_tonumber(L,1);
     int y = lua_tonumber(L,2);
-    uint8_t c = lua_tonumber(L,3);
+    int16_t c = lua_tonumber(L,3);
     _graphicsForLuaApi->sset(x, y, c);

     return 0;
@@ -584,7 +584,7 @@ int mget(lua_State *L) {
 int mset(lua_State *L) {
     int celx = lua_tonumber(L,1);
     int cely = lua_tonumber(L,2);
-    uint8_t snum = lua_tonumber(L, 3);
+    int16_t snum = lua_tonumber(L, 3);

     _graphicsForLuaApi->mset(celx, cely, snum);

@@ -621,7 +621,7 @@ int gfx_map(lua_State *L) {
     if (argc > 5) {
         celh = lua_tonumber(L,6);
     }
-    uint8_t layer = 0;
+    int16_t layer = 0;

     if (argc > 6){
         layer = lua_tonumber(L,7);
@@ -640,9 +640,9 @@ int pal(lua_State *L) {
         return 0;
     }

-    uint8_t p = 0;
-    uint8_t c0 = 0;
-    uint8_t c1 = 0;
+    int16_t p = 0;
+    int16_t c0 = 0;
+    int16_t c1 = 0;

     if (lua_istable(L, 1)){
         if (numArgs > 1) {
@@ -729,7 +729,7 @@ int cursor(lua_State *L) {
         prev = _graphicsForLuaApi->cursor(x, y);
     }
     else{
-        uint8_t c = lua_tonumber(L,3);
+        int16_t c = lua_tonumber(L,3);

         prev =_graphicsForLuaApi->cursor(x, y, c);
     }
diff --git a/test/carts/neg_scrn_pal_test.p8 b/test/carts/neg_scrn_pal_test.p8
new file mode 100644
index 0000000..ea5546f
--- /dev/null
+++ b/test/carts/neg_scrn_pal_test.p8
@@ -0,0 +1,18 @@
+pico-8 cartridge // http://www.pico-8.com
+version 43
+__lua__
+function _init()
+poke( 0x5f2e, 1 )
+pal(2,-2,1)
+end
+
+function _draw()
+cls(2)
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/neg_scrn_pal_test_f01.png b/test/carts/screenshots/neg_scrn_pal_test_f01.png
new file mode 100644
index 0000000..f49cc4c
Binary files /dev/null and b/test/carts/screenshots/neg_scrn_pal_test_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 8894bb4..28b3e83 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -1024,6 +1024,22 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("color with negative number test"){
+        vm->LoadCart("neg_scrn_pal_test.p8", false);
+        vm->vm_run();
+        vm->Step();
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("screen matches screenshot"){
+            vm->Step();
+
+            CHECK(verifyScreenshot(vm, host, "carts/screenshots/neg_scrn_pal_test_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
     delete host;
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 746379c..d7f6c3f 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -959,6 +959,42 @@ TEST_CASE("Vm memory functions") {
         vm->togglePauseMenu();
         CHECK(vm->IsPaused() == false);
     }
+    SUBCASE("picoluaapi handles negative colors correctly"){
+        SUBCASE("pset negative number gets set correctly"){
+            vm->LoadCart("cartparsetest.p8");
+
+            vm->vm_run();
+            vm->Step();
+
+            bool result = vm->ExecuteLua(
+                "function psetnegativetest()\n"
+                " pset(0, 0, -1)\n"
+                " return pget(0, 0) == 15\n"
+                "end\n",
+                "psetnegativetest");
+
+            CHECK(result);
+
+        }
+        SUBCASE("pal negative number gets set correctly"){
+            vm->LoadCart("cartpalnegativetest.p8");
+
+            vm->vm_run();
+            vm->Step();
+
+
+            bool result = vm->ExecuteLua(
+                "function palnegativetest()\n"
+                " pal(0, -1, 0)\n"
+                " pset(0, 0, 0)\n"
+                " return pget(0, 0) == 15\n"
+                "end\n",
+                "palnegativetest");
+
+            CHECK(result);
+
+        }
+    }

     delete stubHost;
     delete graphics;