foxygit / fake-08-xbox360 Log in
commit fefb349ef57a773cc1d0609bfecbd363c1ca8f02
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Wed Sep 2 21:05:20 2020 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Sep 15 17:09:08 2020 -0600

    fix off by one bug copying sprites starting from odd x locations
---
 source/graphics.cpp    |   9 ++--
 test/graphicstests.cpp | 129 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 133 insertions(+), 5 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index b309d8e..4aae104 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -122,17 +122,16 @@ void Graphics::copySpriteToScreen(
 		dy = -dy;
 	}

-	//todo: honor x and y flipping
-
 	for (int y = 0; y < scr_h; y++) {
 		uint8_t* spr = spritebuffer + ((spr_y + y * dy) & 0x7f) * 64;

 		if (!flip_x) {
 			for (int x = 0; x < scr_w; x++) {
-				int combinedPixIdx = spr_x / 2 + x / 2;
+				int abs_spr_x = spr_x + x;
+				int combinedPixIdx = abs_spr_x / 2;
 				uint8_t bothPix = spr[combinedPixIdx];

-				uint8_t c = x % 2 == 0
+				uint8_t c = abs_spr_x % 2 == 0
 					? bothPix & 0x0f //just first 4 bits
 					: bothPix >> 4;  //just last 4 bits

@@ -173,7 +172,7 @@ void Graphics::copyStretchSpriteToScreen(
 	bool flip_x,
 	bool flip_y)
 {
-	if (false || (spr_h == scr_h && spr_w == scr_w)) {
+	if (spr_h == scr_h && spr_w == scr_w) {
 		// use faster non stretch blitter if sprite is not stretched
 		copySpriteToScreen(spritebuffer, scr_x, scr_y, spr_x, spr_y, scr_w, scr_h, flip_x, flip_y);
 		return;
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 874e543..a9621b2 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -29,6 +29,17 @@ void checkPoints(Graphics* graphics, std::vector<coloredPoint> expectedPoints) {
     CHECK(isCorrect);
 }

+void debugScreen(Graphics* graphics) {
+    for (int x=0; x < 128; x++) {
+        for (int y = 0; y < 128; y++) {
+ 	        uint8_t c = graphics->pget(x,y);
+            if (c != 0) {
+                printf("%d,%d,%d\n", x, y, c);
+            }
+        }
+    }
+}
+
 TEST_CASE("graphics class behaves as expected") {
     //general setup
     std::string fontdata = get_font_data();
@@ -709,6 +720,124 @@ TEST_CASE("graphics class behaves as expected") {

         checkPoints(graphics, expectedPoints);
     }
+    SUBCASE("spr(...) draws to screen at odd numbered location") {
+        graphics->cls();
+
+        for(uint8_t i = 0; i < 16; i++) {
+            for(uint8_t j = 0; j < 16; j++) {
+                graphics->sset(i, j, i);
+            }
+        }
+
+        graphics->spr(0, 51, 11, 1, 1, false, false);
+
+        //diagonal across sprite
+        std::vector<coloredPoint> expectedPoints = {
+            {50, 10, 0},
+            {51, 11, 0},
+            {52, 12, 1},
+            {53, 13, 2},
+            {54, 14, 3},
+            {55, 15, 4},
+            {56, 16, 5},
+            {57, 17, 6},
+            {58, 18, 7},
+            {59, 19, 0},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("sspr(...) draws to screen at odd numbered location") {
+        graphics->cls();
+
+        for(uint8_t i = 0; i < 16; i++) {
+            for(uint8_t j = 0; j < 16; j++) {
+                graphics->sset(i, j, i);
+            }
+        }
+
+        graphics->sspr(0, 0, 8, 8, 51, 11, 8, 8, false, false);
+
+        //diagonal across sprite
+        std::vector<coloredPoint> expectedPoints = {
+            {50, 10, 0},
+            {51, 11, 0},
+            {52, 12, 1},
+            {53, 13, 2},
+            {54, 14, 3},
+            {55, 15, 4},
+            {56, 16, 5},
+            {57, 17, 6},
+            {58, 18, 7},
+            {59, 19, 0},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("sspr(...) draws from odd numbered sprite sheet location") {
+        graphics->cls();
+
+        for(uint8_t i = 0; i < 16; i++) {
+            for(uint8_t j = 0; j < 16; j++) {
+                graphics->sset(i, j, i);
+            }
+        }
+
+        graphics->sspr(1, 1, 4, 4, 51, 11, 4, 4, false, false);
+
+        //diagonal across sprite
+        std::vector<coloredPoint> expectedPoints = {
+            {51, 10, 0},
+            {51, 11, 1},
+            {51, 12, 1},
+            {51, 13, 1},
+            {51, 14, 1},
+            {52, 11, 2},
+            {52, 12, 2},
+            {52, 13, 2},
+            {52, 14, 2},
+            {53, 11, 3},
+            {53, 12, 3},
+            {53, 13, 3},
+            {53, 14, 3},
+            {54, 11, 4},
+            {54, 12, 4},
+            {54, 13, 4},
+            {54, 14, 4},
+            {55, 11, 0},
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }
+    SUBCASE("sspr(...) draws non-square to screen at location") {
+        graphics->cls();
+
+        for(uint8_t i = 0; i < 16; i++) {
+            for(uint8_t j = 0; j < 16; j++) {
+                graphics->sset(i, j, i);
+            }
+        }
+
+        graphics->sspr(3, 2, 3, 4, 100, 50, 3, 4, false, false);
+
+        std::vector<coloredPoint> expectedPoints = {
+            {100, 50, 3},
+            {100, 51, 3},
+            {100, 52, 3},
+            {100, 53, 3},
+            {101, 50, 4},
+            {101, 51, 4},
+            {101, 52, 4},
+            {101, 53, 4},
+            {102, 50, 5},
+            {102, 51, 5},
+            {102, 52, 5},
+            {102, 53, 5},
+
+        };
+
+        checkPoints(graphics, expectedPoints);
+    }