foxygit / fake-08-xbox360 Log in
commit 44eaaf0daa483a660f54daa76a74c43b9ee96d37
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Tue Aug 9 17:21:55 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Tue Aug 9 17:21:55 2022 -0600

    Fix sprite sheet size in IS_VALID_SPR_IDX

    also add check to sset and sget
---
 source/graphics.cpp         | 10 ++++++++--
 source/nibblehelpers.h      |  2 +-
 test/graphicstests.cpp      | 43 +++++++++++++++++++++++++++++++++++++++++++
 test/nibblehelperstests.cpp |  8 ++++----
 4 files changed, 56 insertions(+), 7 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 3181a03..9efec49 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1457,11 +1457,17 @@ void Graphics::fset(uint8_t n, uint8_t v){
 }

 uint8_t Graphics::sget(uint8_t x, uint8_t y){
-	return getPixelNibble(x, y, GetP8SpriteSheetBuffer());
+	if (IS_VALID_SPR_IDX(x, y)) {
+		return getPixelNibble(x, y, GetP8SpriteSheetBuffer());
+	}
+	return 0;
 }

 void Graphics::sset(uint8_t x, uint8_t y, uint8_t c){
-	setPixelNibble(x, y, c, GetP8SpriteSheetBuffer());
+	if (IS_VALID_SPR_IDX(x, y)) {
+		setPixelNibble(x, y, c, GetP8SpriteSheetBuffer());
+	}
+	return;
 }

 std::tuple<int16_t, int16_t> Graphics::camera() {
diff --git a/source/nibblehelpers.h b/source/nibblehelpers.h
index 0c2d5e2..8ff137a 100644
--- a/source/nibblehelpers.h
+++ b/source/nibblehelpers.h
@@ -5,7 +5,7 @@
 //for 1 byte (8 bit) indexes, 128x64
 //should be the equivalent of return y * 64 + (x / 2);
 #define COMBINED_IDX(x, y) ((y) << 6) | ((x) >> 1)
-#define IS_VALID_SPR_IDX(x, y) (y >= 0 && y < 64 && x >= 0 && x < 128)
+#define IS_VALID_SPR_IDX(x, y) (y >= 0 && y < 128 && x >= 0 && x < 128)
 //I think this should work if you cast the buffer to a uint32_t* pointer, but not tested
 //for 4 byte (32 bit) inexes, 16x8
 //should be the equivalent of return y * 16 + (x / 8);
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 388005e..57341e0 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -791,6 +791,26 @@ TEST_CASE("graphics class behaves as expected") {

         checkPoints(graphics, expectedPoints);
     }
+    SUBCASE("spr(...) draws last sprite (sprite 255)") {
+        graphics->cls();
+        for(int y = 0; y < 128; y++) {
+            for(int x = 0; x < 128; x++) {
+                graphics->sset(x, y, (x+y)%15+1);
+            }
+        }
+
+        graphics->spr(255, 0, 120, 1, 1, false, false);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {0, 120, 1},
+            {0, 121, 2},
+            {0, 122, 3},
+            {0, 123, 4},
+            {0, 124, 5},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
     SUBCASE("sspr(...) draws to screen at odd numbered location") {
         graphics->cls();

@@ -1073,6 +1093,18 @@ TEST_CASE("graphics class behaves as expected") {

        CHECK_EQ(result, 12);
    }
+   SUBCASE("sget returns 0 when out of bounds") {
+        graphics->cls();
+        for(int y = 0; y < 128; y++) {
+            for(int x = 0; x < 128; x++) {
+                graphics->sset(x, y, (x+y)%15+1);
+            }
+        }
+
+        auto result = graphics->sget(128, 128);
+
+       CHECK_EQ(result, 0);
+    }
    SUBCASE("sset sets value in ram for even x pixel")
    {
        uint8_t x = 60;
@@ -1095,6 +1127,17 @@ TEST_CASE("graphics class behaves as expected") {

        CHECK_EQ(result, 192);
    }
+   SUBCASE("sset does nothing for out of bounds pixel")
+   {
+       uint8_t x = 128;
+       uint8_t y = 0;
+       graphics->sset(x, y, 14); //14 = 1110
+
+       int combinedIdx = y * 64 + (x / 2);
+       auto result = picoRam.spriteSheetData[combinedIdx];
+
+       CHECK_EQ(result, 0);
+   }
    SUBCASE("camera({x}, {y}) sets values in memory")
    {
        uint8_t x = 33;
diff --git a/test/nibblehelperstests.cpp b/test/nibblehelperstests.cpp
index a2ced47..be23fe8 100644
--- a/test/nibblehelperstests.cpp
+++ b/test/nibblehelperstests.cpp
@@ -24,15 +24,15 @@ TEST_CASE("Get Combined pixel index") {
         CHECK(isValidSprIdx(1, 0));
     }
     SUBCASE("isValidSprIdx 127, 127 true") {
-        CHECK(isValidSprIdx(127, 63));
+        CHECK(isValidSprIdx(127, 127));
     }
     SUBCASE("isValidSprIdx -1,0 false") {
         CHECK_FALSE(isValidSprIdx(-1, 0));
     }
     SUBCASE("isValidSprIdx 128, 127 false") {
-        CHECK_FALSE(isValidSprIdx(128, 63));
+        CHECK_FALSE(isValidSprIdx(128, 127));
     }
-    SUBCASE("isValidSprIdx 127, 64 false") {
-        CHECK_FALSE(isValidSprIdx(127, 64));
+    SUBCASE("isValidSprIdx 127, 128 false") {
+        CHECK_FALSE(isValidSprIdx(127, 128));
     }
 }