foxygit / fake-08-xbox360 Log in
commit d90a79a8b3efcc9dfb862baaca5fa5fc17a64178
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sun Mar 21 12:21:40 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sun Mar 21 12:21:40 2021 -0600

    make pal and palt return previous
---
 source/graphics.cpp    | 12 ++++++++++--
 source/graphics.h      |  4 ++--
 source/picoluaapi.cpp  | 17 ++++++++++++-----
 test/graphicstests.cpp | 12 ++++++++++++
 4 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index e326cdc..b48928a 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1204,17 +1204,22 @@ void Graphics::pal() {
 	this->palt();
 }

-void Graphics::pal(uint8_t c0, uint8_t c1, uint8_t p){
+uint8_t Graphics::pal(uint8_t c0, uint8_t c1, uint8_t p){
 	//0-15 alowed
 	c0 &= 0x0f;
+	uint8_t prev = 0;
 	if (p == 0) {
 		//for draw palette we have to preserve the transparency bit
+		prev = _memory->drawState.drawPaletteMap[c0] & 0xf;
 		_memory->drawState.drawPaletteMap[c0] = (_memory->drawState.drawPaletteMap[c0] & 0x10) | (c1 & 0xf);
 	} else if (p == 1) {
 		//0-15, or 127-143 allowed
+		prev = _memory->drawState.screenPaletteMap[c0] & 0xf;
 		c1 &= 0x8f;
 		_memory->drawState.screenPaletteMap[c0] = c1;
 	}
+
+	return prev;
 }

 void Graphics::palt() {
@@ -1224,14 +1229,17 @@ void Graphics::palt() {
 	}
 }

-void Graphics::palt(uint8_t c, bool t){
+bool Graphics::palt(uint8_t c, bool t){
 	c = c & 15;
+	bool prev = _memory->drawState.drawPaletteMap[c] & 0xf0;
 	if (t) {
 		_memory->drawState.drawPaletteMap[c] |= 1UL << 4;
 	}
 	else {
 		_memory->drawState.drawPaletteMap[c] &= ~(1UL << 4);
 	}
+
+	return prev;
 }


diff --git a/source/graphics.h b/source/graphics.h
index 1372f1d..aab886c 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -191,9 +191,9 @@ class Graphics {
 	void map(int celx, int cely, int sx, int sy, int celw, int celh, uint8_t layer);

 	void pal();
-	void pal(uint8_t c0, uint8_t c1, uint8_t p);
+	uint8_t pal(uint8_t c0, uint8_t c1, uint8_t p);
 	void palt();
-	void palt(uint8_t c0, bool t);
+	bool palt(uint8_t c0, bool t);

 	std::tuple<uint8_t, uint8_t> cursor();
 	std::tuple<uint8_t, uint8_t> cursor(int x, int y);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 2817ac4..baae4f3 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -561,8 +561,11 @@ int pal(lua_State *L) {
     int numArgs = lua_gettop(L);
     if (numArgs == 0) {
         _graphicsForLuaApi->pal();
+
+        //I think this always returns 0, not nil
+        lua_pushnumber(L, 0);

-        return 0;
+        return 1;
     }

     uint8_t p = 0;
@@ -600,9 +603,11 @@ int pal(lua_State *L) {
         p = lua_tonumber(L,3);
     }

-    _graphicsForLuaApi->pal(c0, c1, p);
+    uint8_t prev =_graphicsForLuaApi->pal(c0, c1, p);

-    return 0;
+    lua_pushnumber(L, prev);
+
+    return 1;
 }

 int palt(lua_State *L) {
@@ -615,9 +620,11 @@ int palt(lua_State *L) {
     uint8_t c = lua_tonumber(L,1);
     bool t = lua_toboolean(L,2);

-    _graphicsForLuaApi->palt(c, t);
+    uint8_t prev = _graphicsForLuaApi->palt(c, t);

-    return 0;
+    lua_pushnumber(L, prev);
+
+    return 1;
 }

 int cursor(lua_State *L) {
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 571610a..72335e8 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1678,6 +1678,18 @@ TEST_CASE("graphics class behaves as expected") {

         CHECK_EQ(4, result);
     }
+    SUBCASE("pal({c0}, {c1}, 0) returns previous") {
+        graphics->pal(14, 2, 0);
+        auto result = graphics->pal(14, 8, 0);
+
+        CHECK_EQ(2, result);
+    }
+    SUBCASE("pal({c0}, {c1}, 1) returns previous") {
+        graphics->pal(13, 4, 1);
+        auto result = graphics->pal(13, 6, 1);
+
+        CHECK_EQ(4, result);
+    }
     SUBCASE("pal({c0}, {c1}, {p}) 17 remapped to 1") {
         graphics->pal(13, 17, 1);