foxygit / fake-08-xbox360 Log in
commit 69772fd2e38fcc348412440bfe9f14732ac53324
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Mar 20 06:57:04 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Mar 20 06:57:04 2021 -0600

    Make clip() return previous value
---
 source/graphics.cpp    | 15 ++++++++++++---
 source/graphics.h      |  4 ++--
 source/picoluaapi.cpp  | 14 +++++++++++---
 test/graphicstests.cpp | 14 ++++++++++++++
 4 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 0aaae79..7bcb53d 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1120,17 +1120,26 @@ std::tuple<int16_t, int16_t> Graphics::camera(int16_t x, int16_t y) {
 	return prev;
 }

-void Graphics::clip() {
-	this->clip(0, 0, 128, 128);
+std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> Graphics::clip() {
+	return this->clip(0, 0, 128, 128);
 }

-void Graphics::clip(int x, int y, int w, int h) {
+std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> Graphics::clip(int x, int y, int w, int h) {
+	auto prev = std::make_tuple(
+		_memory->drawState.clip_xb,
+		_memory->drawState.clip_xe,
+		_memory->drawState.clip_yb,
+		_memory->drawState.clip_ye
+	);
+
 	int xe = x + w;
 	int ye = y + h;
 	_memory->drawState.clip_xb = clampCoordToScreenDims(x);
 	_memory->drawState.clip_yb = clampCoordToScreenDims(y);
 	_memory->drawState.clip_xe = clampCoordToScreenDims(xe);
 	_memory->drawState.clip_ye = clampCoordToScreenDims(ye);
+
+	return prev;
 }


diff --git a/source/graphics.h b/source/graphics.h
index fcb4d3f..f0d323b 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -181,8 +181,8 @@ class Graphics {
 	std::tuple<int16_t, int16_t> camera();
 	std::tuple<int16_t, int16_t> camera(int16_t x, int16_t y);

-	void clip();
-	void clip(int x, int y, int w, int h);
+	std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> clip();
+	std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> clip(int x, int y, int w, int h);

 	uint8_t mget(int celx, int cely);
 	void mset(int celx, int cely, uint8_t snum);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index a6ec960..1f34d6e 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -474,19 +474,27 @@ 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);
         int w = lua_tonumber(L,3);
         int h = lua_tonumber(L,4);

-        _graphicsForLuaApi->clip(x, y, w, h);
+        prev = _graphicsForLuaApi->clip(x, y, w, h);
     }
     else {
-        _graphicsForLuaApi->clip();
+        prev = _graphicsForLuaApi->clip();
     }

-    return 0;
+    lua_pushnumber(L, get<0>(prev));
+    lua_pushnumber(L, get<1>(prev));
+    lua_pushnumber(L, get<2>(prev));
+    lua_pushnumber(L, get<3>(prev));
+
+    return 4;
 }

 int mget(lua_State *L) {
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index c043c7e..18d0665 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1313,6 +1313,20 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK_EQ(picoRam.drawState.clip_yb, 75);
         CHECK_EQ(picoRam.drawState.clip_ye, 100);
     }
+    SUBCASE("clip() returns prev value")
+    {
+        picoRam.drawState.clip_xb = 28;
+        picoRam.drawState.clip_xe = 29;
+        picoRam.drawState.clip_yb = 50;
+        picoRam.drawState.clip_ye = 51;
+
+        auto prev = graphics->clip();
+
+        CHECK_EQ(std::get<0>(prev), 28);
+        CHECK_EQ(std::get<1>(prev), 29);
+        CHECK_EQ(std::get<2>(prev), 50);
+        CHECK_EQ(std::get<3>(prev), 51);
+    }
     SUBCASE("clip values applies to pset")
     {
        graphics->clip(100, 100, 28, 28);