foxygit / fake-08-xbox360 Log in
commit 67a271e900810357a571b64f5a83b345316698b4
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Mon Aug 15 17:13:26 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Mon Aug 15 17:13:26 2022 -0600

    Fix mget bounds check off by one

    fixes tline visual bugs as well
---
 source/graphics.cpp    | 4 +++-
 test/graphicstests.cpp | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 9efec49..a194331 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1515,10 +1515,12 @@ uint8_t Graphics::mget(int celx, int cely){

 	const int mapW = _memory->hwState.widthOfTheMap == 0 ? 256 : _memory->hwState.widthOfTheMap;
 	const int mapH = mapSize / mapW;
+	const int maxXIdx = mapW - 1;
+	const int maxYIdx = mapH - 1;

 	const int idx = cely * mapW + celx;

-	if (celx < 0 || celx > mapW || cely < 0 || cely > mapH) {
+	if (celx < 0 || celx > maxXIdx || cely < 0 || cely > maxYIdx) {
         return 0;
 	}

diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 57341e0..8f5de3b 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1574,9 +1574,9 @@ TEST_CASE("graphics class behaves as expected") {
             }
         }
         auto result1 = graphics->mget(-1, 16);
-        auto result2 = graphics->mget(129, 16);
+        auto result2 = graphics->mget(128, 16);
         auto result3 = graphics->mget(10, -1);
-        auto result4 = graphics->mget(10, 67);
+        auto result4 = graphics->mget(10, 64);

         CHECK_EQ(0, result1);
         CHECK_EQ(0, result2);