foxygit / fake-08-xbox360 Log in
commit 0ed7214659424f248f58f4810155f2cf65d839ed
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Mar 20 06:33:48 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Mar 20 06:33:48 2021 -0600

    Fix camera() to return previous value
---
 source/graphics.cpp    | 10 +++++++---
 source/graphics.h      |  5 +++--
 source/picoluaapi.cpp  |  8 ++++++--
 test/graphicstests.cpp | 11 +++++++++++
 4 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 992b97d..d639d79 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1107,13 +1107,17 @@ void Graphics::sset(uint8_t x, uint8_t y, uint8_t c){
 	setPixelNibble(x, y, c, _memory->spriteSheetData);
 }

-void Graphics::camera() {
-	this->camera(0, 0);
+std::tuple<int16_t, int16_t> Graphics::camera() {
+	return this->camera(0, 0);
 }

-void Graphics::camera(int x, int y) {
+std::tuple<int16_t, int16_t> Graphics::camera(int16_t x, int16_t y) {
+	std::tuple<int16_t, int16_t> prev (_memory->drawState.camera_x, _memory->drawState.camera_y);
+
 	_memory->drawState.camera_x = x;
 	_memory->drawState.camera_y = y;
+
+	return prev;
 }

 void Graphics::clip() {
diff --git a/source/graphics.h b/source/graphics.h
index 42539b3..152448e 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -1,6 +1,7 @@
 #pragma once

 #include <string>
+#include <tuple>
 #include "hostVmShared.h"
 #include "PicoRam.h"
 #include <fix32.h>
@@ -177,8 +178,8 @@ class Graphics {
 	uint8_t sget(uint8_t x, uint8_t y);
 	void sset(uint8_t x, uint8_t y, uint8_t c);

-	void camera();
-	void camera(int x, int y);
+	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);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 1db565c..55d05bc 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -1,6 +1,7 @@

 #include <string>
 #include <vector>
+#include <tuple>
 using namespace std;

 #include "picoluaapi.h"
@@ -464,9 +465,12 @@ int camera(lua_State *L) {
         y = lua_tointeger(L,2);
     }

-    _graphicsForLuaApi->camera(x, y);
+    auto prev = _graphicsForLuaApi->camera(x, y);

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

 int clip(lua_State *L) {
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index ae464aa..0b019a7 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1,5 +1,6 @@
 #include <string>
 #include <vector>
+#include <tuple>

 #include "doctest.h"
 #include "../source/graphics.h"
@@ -1094,6 +1095,16 @@ TEST_CASE("graphics class behaves as expected") {
        CHECK_EQ(picoRam.drawState.camera_x, 0);
        CHECK_EQ(picoRam.drawState.camera_y, 0);
    }
+   SUBCASE("camera({x}, {y}) returns previous values")
+   {
+       uint8_t x = 33;
+       uint8_t y = 120;
+       graphics->camera(x, y);
+       auto prev = graphics->camera(120, 31);
+
+       CHECK_EQ(std::get<0>(prev), x);
+       CHECK_EQ(std::get<1>(prev), y);
+   }
    SUBCASE("camera values applies to pget")
    {
        uint8_t col = 13;