foxygit / fake-08-xbox360 Log in
commit 9f100baae5e88752bb3145967ddce84c70700c1a
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Fri Apr 24 15:41:35 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Fri Apr 24 15:41:35 2020 -0600

    implement palette shifting (pal())
---
 source/console.cpp    |  2 ++
 source/graphics.cpp   | 30 +++++++++++++++++++++++++++---
 source/graphics.h     |  6 ++++++
 source/picoluaapi.cpp | 21 +++++++++++++++++++++
 source/picoluaapi.h   |  1 +
 5 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/source/console.cpp b/source/console.cpp
index 68e0428..fd5ef63 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -79,6 +79,8 @@ void Console::LoadCart(std::string filename){
     lua_register(_luaState, "camera", camera);
     lua_register(_luaState, "clip", clip);

+    lua_register(_luaState, "pal", pal);
+
     lua_register(_luaState, "mget", mget);
     lua_register(_luaState, "mset", mset);
     lua_register(_luaState, "map", map);
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 235c1cc..a24bf8c 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -40,6 +40,9 @@ Graphics::Graphics(std::string fontdata) {
 	this->_gfxState_clip_h = 127;

 	copy_string_to_sprite_memory(fontSpriteData, fontdata);
+
+	//make all colors default
+	this->pal();
 }

 void Graphics::setSpriteSheet(std::string spritesheetstring){
@@ -77,6 +80,8 @@ void Graphics::copySpriteToScreen(

 	short dy = 1;

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

@@ -210,7 +215,7 @@ void Graphics::_private_pset(short x, short y, uint8_t col) {
 	y = y - _gfxState_camera_y;

 	if (canDrawAtPoint(x, y)){
-		_pico8_fb[(x * 128) + y] = col;
+		_pico8_fb[(x * 128) + y] = _gfxState_drawPaletteMap[col];
 	}
 }
 //end helper methods
@@ -558,7 +563,7 @@ uint8_t Graphics::mget(short celx, short cely){
 	else if (cely < 64){
 		return this->spriteSheetData[cely* 128 + celx];
 	}
-
+
 	return 0;
 }

@@ -586,13 +591,32 @@ void Graphics::map(int celx, int cely, int sx, int sy, int celw, int celh, uint8
 	}
 }

+void Graphics::pal() {
+	for (uint8_t c = 0; c < 16; c++) {
+		_gfxState_drawPaletteMap[c] = c;
+		_gfxState_screenPaletteMap[c] = c;
+		//todo: also clear transparency palette here once implemented
+	}
+
+}
+
+void Graphics::pal(uint8_t c0, uint8_t c1, uint8_t p){
+	if (c0 < 16 && c1 < 16) {
+		if (p == 0) {
+			_gfxState_drawPaletteMap[c0] = c1;
+		} else if (p == 1) {
+			_gfxState_screenPaletteMap[c0] = c1;
+		}
+	}
+}
+
 void Graphics::flipBuffer(uint8_t* fb) {
 	short x, y;
     for(x = 0; x < 400; x++) {
     	for(y = 0; y < 240; y++) {
 			if (x < 128 && y < 128) {
 				uint8_t c = _pico8_fb[x*128 + y];
-				Color col = PaletteColors[c];
+				Color col = PaletteColors[_gfxState_screenPaletteMap[c]];

 				fb[((x*240)+ (239 - y))*3+0] = col.Blue;
 				fb[((x*240)+ (239 - y))*3+1] = col.Green;
diff --git a/source/graphics.h b/source/graphics.h
index dfda31a..4bbdea2 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -51,6 +51,9 @@ class Graphics {
 	short _gfxState_clip_w;
 	short _gfxState_clip_h;

+	uint8_t _gfxState_drawPaletteMap[16];
+	uint8_t _gfxState_screenPaletteMap[16];
+
 	//not actually part of graphics state memory?
 	short _gfxState_line_x;
 	short _gfxState_line_y;
@@ -173,6 +176,9 @@ class Graphics {
 	void map(int celx, int cely, int sx, int sy, int celw, int celh);
 	void map(int celx, int cely, int sx, int sy, int celw, int celh, uint8_t layer);

+	void pal();
+	void pal(uint8_t c0, uint8_t c1, uint8_t p);
+

 	void flipBuffer(uint8_t* fb);

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 3fb7e2b..c5161e2 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -435,6 +435,27 @@ int map(lua_State *L) {
     return 0;
 }

+int pal(lua_State *L) {
+    if (lua_gettop(L) == 0) {
+        _graphicsForLuaApi->pal();
+
+        return 0;
+    }
+
+
+    uint8_t c0 = lua_tonumber(L,1);
+    uint8_t c1 = lua_tonumber(L,2);
+    uint8_t p = 0;
+
+    if (lua_gettop(L) > 2){
+        p = lua_tonumber(L,3);
+    }
+
+    _graphicsForLuaApi->pal(c0, c1, p);
+
+    return 0;
+}
+
 //Input

 //input api
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index d56e4fe..0443a38 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -36,6 +36,7 @@ int clip(lua_State *L);
 int mget(lua_State *L);
 int mset(lua_State *L);
 int map(lua_State *L);
+int pal(lua_State *L);

 //input api
 int btn(lua_State *L);