foxygit / fake-08-xbox360 Log in
commit 14c9739ee8a6597db9f51c3442aefdbf28e5165d
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Mar 20 07:08:42 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Mar 20 07:08:42 2021 -0600

    make color return prev value
---
 source/graphics.cpp    | 18 +++++++++++-------
 source/graphics.h      |  4 ++--
 source/picoluaapi.cpp  | 16 ++++++++++++----
 test/graphicstests.cpp | 14 ++++++++++++++
 4 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 7bcb53d..4abe46f 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -481,19 +481,23 @@ uint8_t Graphics::pget(int x, int y){
 	return 0;
 }

-void Graphics::color(){
-	color(6);
+uint8_t Graphics::color(){
+	return color(6);
 }

-void Graphics::color(uint8_t col){
-	this->_memory->drawState.color = col;
+uint8_t Graphics::color(uint8_t col){
+	uint8_t prev = _memory->drawState.color;
+
+	_memory->drawState.color = col;
+
+	return prev;
 }

 void Graphics::line () {
 	//just invalidate line state
-	this->_memory->drawState.line_x = 0;
-	this->_memory->drawState.line_y = 0;
-	this->_memory->drawState.lineInvalid = 1;
+	_memory->drawState.line_x = 0;
+	_memory->drawState.line_y = 0;
+	_memory->drawState.lineInvalid = 1;
 }

 void Graphics::line (uint8_t col){
diff --git a/source/graphics.h b/source/graphics.h
index f0d323b..50f839c 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -112,8 +112,8 @@ class Graphics {
 	void pset(int x, int y, uint8_t col);
 	uint8_t pget(int x, int y);

-	void color();
-	void color(uint8_t c);
+	uint8_t color();
+	uint8_t color(uint8_t c);

 	void line ();
 	void line (uint8_t col);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 1f34d6e..97df3d6 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -82,11 +82,19 @@ int pget(lua_State *L){
 }

 int color(lua_State *L){
-    fix32 c = lua_tonumber(L,1);
+    uint8_t prev = 0;
+    uint8_t c = 0;
+    if (lua_gettop(L) > 0) {
+        c = lua_tonumber(L,1);
+        prev = _graphicsForLuaApi->color((uint8_t)c);
+    }
+    else {
+        prev = _graphicsForLuaApi->color();
+    }

-    _graphicsForLuaApi->color((uint8_t)c);
+    lua_pushinteger(L, prev);

-    return 0;
+    return 1;
 }

 int line (lua_State *L){
@@ -476,7 +484,7 @@ int camera(lua_State *L) {
 int clip(lua_State *L) {

     std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> prev;
-
+
     if (lua_gettop(L) >= 4) {
         int x = lua_tonumber(L,1);
         int y = lua_tonumber(L,2);
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 18d0665..04140c5 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -173,6 +173,20 @@ TEST_CASE("graphics class behaves as expected") {

         CHECK(picoRam.drawState.color == 12);
     }
+    SUBCASE("color() resets color to 6") {
+        picoRam.drawState.color = 12;
+
+        graphics->color();
+
+        CHECK(picoRam.drawState.color == 6);
+    }
+    SUBCASE("color() returns previous") {
+        auto prev = picoRam.drawState.color = 13;
+
+        graphics->color(12);
+
+        CHECK(prev == 13);
+    }
     SUBCASE("line() clears line state") {
         picoRam.drawState.line_x = 10;
 	    picoRam.drawState.line_y = 30;