foxygit / fake-08-xbox360 Log in
commit b71a2ce125f5e8bace33820f95bdf417d621b0af
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Mon Aug 24 15:22:45 2020 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Sep 15 17:09:08 2020 -0600

    add initial draw palette state tests
---
 source/graphics.cpp    | 23 +++++++++++++++++------
 source/graphics.h      |  3 +++
 test/graphicstests.cpp | 21 +++++++++++++++++++++
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 8da35ab..c5c188d 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -43,10 +43,11 @@ Graphics::Graphics(std::string fontdata, PicoRam* memory) {

 	//set default clip
 	clip();
+	pal();
+	color(7);
 }


-
 uint8_t* Graphics::GetP8FrameBuffer(){
 	return this->_pico8_fb;
 }
@@ -135,7 +136,7 @@ void Graphics::copySpriteToScreen(
 					? bothPix & 0x0f //just first 4 bits
 					: bothPix >> 4;  //just last 4 bits

-				if (_memory->_gfxState_transparencyPalette[c] == false) { //if not transparent. Come back later to add palt() support by checking tranparency palette
+				if (isColorTransparent(c) == false) { //if not transparent. Come back later to add palt() support by checking tranparency palette
 					_private_pset(scr_x + x, scr_y + y, c); //set color on framebuffer. Come back later and add pal() by translating color
 				}
 			}
@@ -149,7 +150,7 @@ void Graphics::copySpriteToScreen(
 					? bothPix >> 4 //just first 4 bits
 					: bothPix & 0x0f;  //just last 4 bits

-				if (_memory->_gfxState_transparencyPalette[c] == false) { //if not transparent. Come back later to add palt() support by checking tranparency palette
+				if (isColorTransparent(c) == false) { //if not transparent. Come back later to add palt() support by checking tranparency palette
 					_private_pset(scr_x + x, scr_y + y, c); //set color on framebuffer. Come back later and add pal() by translating color
 				}
 			}
@@ -242,7 +243,7 @@ void Graphics::copyStretchSpriteToScreen(
 				uint8_t c = (pixIndex >> 16) % 2 == 0
 					? bothPix & 0x0f //just first 4 bits
 					: bothPix >> 4;  //just last 4 bits
-				if (_memory->_gfxState_transparencyPalette[c] == false) {
+				if (isColorTransparent(c) == false) {
 					_private_pset(scr_x + x, scr_y + y, c);
 				}
 			}
@@ -255,7 +256,7 @@ void Graphics::copyStretchSpriteToScreen(
 				uint8_t c = (pixIndex >> 16) % 2 == 0
 					? bothPix >> 4 //just first 4 bits
 					: bothPix & 0x0f;  //just last 4 bits
-				if (_memory->_gfxState_transparencyPalette[c] == false) {
+				if (isColorTransparent(c) == false) {
 					_private_pset(scr_x + x, scr_y + y, c);
 				}
 			}
@@ -300,6 +301,16 @@ bool Graphics::isOnScreen(int x, int y) {
 		y <= 127;
 }

+bool Graphics::isColorTransparent(uint8_t color) {
+	color = color & 15;
+	return _memory->_gfxState_transparencyPalette[color];
+}
+
+uint8_t Graphics::getPalMappedColor(uint8_t color) {
+	color = color & 15;
+	return _memory->_gfxState_drawPaletteMap[color];
+}
+
 bool Graphics::isWithinClip(int x, int y) {
 	return
 		x >= _memory->_gfxState_clip_xb &&
@@ -609,7 +620,7 @@ int Graphics::print(std::string str, int x, int y, uint16_t c) {
 	//font sprite sheet has text as color 7, with 0 as transparent. We need to override
 	//these values and restore them after
 	uint8_t prevCol7Map = _memory->_gfxState_drawPaletteMap[7];
-	bool prevCol0Transp = _memory->_gfxState_transparencyPalette[0];
+	bool prevCol0Transp = isColorTransparent(0);

 	_memory->_gfxState_drawPaletteMap[7] = c;
 	_memory->_gfxState_transparencyPalette[0] = true;
diff --git a/source/graphics.h b/source/graphics.h
index afc03d4..cb3de4b 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -80,6 +80,9 @@ class Graphics {
 	uint8_t* GetScreenPaletteMap();
 	Color* GetPaletteColors();

+	bool isColorTransparent(uint8_t color);
+	uint8_t getPalMappedColor(uint8_t color);
+
 	void cls();
 	void cls(uint8_t color);

diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index 735d2e2..a187ef2 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -60,6 +60,27 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK(picoRam._gfxState_clip_yb == 0);
         CHECK(picoRam._gfxState_clip_xe == 127);
         CHECK(picoRam._gfxState_clip_ye == 127);
+    }
+    SUBCASE("Constructor sets default color") {
+        CHECK(picoRam._gfxState_color == 7);
+    }
+    SUBCASE("Constructor sets default draw color palette") {
+        //colors mapped to themselves
+        for (uint8_t c = 0; c < 16; c++) {
+            CHECK(graphics->getPalMappedColor(c) == c);
+        }
+    }
+    SUBCASE("Constructor sets default draw transparency palette") {
+        //color 0 defaults to transparent
+        CHECK(graphics->isColorTransparent(0) == true);
+
+        //all other colors not transparent
+        for (uint8_t c = 1; c < 16; c++) {
+            CHECK(graphics->isColorTransparent(c) == false);
+        }
+    }
+    SUBCASE("Constructor sets default screen palette") {
+
     }
     //check palette maps are set to default values