foxygit / fake-08-xbox360 Log in
commit a896b94399b318a779fd03003052f23d43135382
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Fri Apr 24 09:26:47 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Fri Apr 24 09:26:47 2020 -0600

    Implement map (and mget/mset) for non-shared portion
---
 source/console.cpp             |  5 +++++
 source/graphics.cpp            | 47 +++++++++++++++++++++++++++++++++++++++---
 source/graphics.h              |  7 +++++++
 source/picoluaapi.cpp          | 38 ++++++++++++++++++++++++++++++++++
 source/picoluaapi.h            |  3 +++
 source/stringToDataHelpers.cpp |  4 ++--
 source/stringToDataHelpers.h   |  4 ++--
 testcart.p8                    |  8 +++++--
 8 files changed, 107 insertions(+), 9 deletions(-)

diff --git a/source/console.cpp b/source/console.cpp
index d36b987..68e0428 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -48,6 +48,7 @@ void Console::LoadCart(std::string filename){

     _graphics->setSpriteSheet(cart->SpriteSheetString);
     _graphics->setSpriteFlags(cart->SpriteFlagsString);
+    _graphics->setMapData(cart->MapString);

     _loadedCart = cart;

@@ -78,6 +79,10 @@ void Console::LoadCart(std::string filename){
     lua_register(_luaState, "camera", camera);
     lua_register(_luaState, "clip", clip);

+    lua_register(_luaState, "mget", mget);
+    lua_register(_luaState, "mset", mset);
+    lua_register(_luaState, "map", map);
+
     //input
     lua_register(_luaState, "btn", btn);
     lua_register(_luaState, "btnp", btnp);
diff --git a/source/graphics.cpp b/source/graphics.cpp
index ea9082c..2466418 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -39,17 +39,22 @@ Graphics::Graphics(std::string fontdata) {
 	this->_gfxState_clip_w = 127;
 	this->_gfxState_clip_h = 127;

-	copy_data_to_sprites(fontSpriteData, fontdata);
+	copy_string_to_sprite_memory(fontSpriteData, fontdata);
 }

 void Graphics::setSpriteSheet(std::string spritesheetstring){
 	Logger::Write("Copying data to spritesheet\n");
-	copy_data_to_sprites(spriteSheetData, spritesheetstring);
+	copy_string_to_sprite_memory(spriteSheetData, spritesheetstring);
 }

 void Graphics::setSpriteFlags(std::string spriteFlagsstring){
 	Logger::Write("Copying data to sprite flags\n");
-	copy_data_to_sprite_flags(spriteFlags, spriteFlagsstring);
+	copy_string_to_memory(spriteFlags, spriteFlagsstring);
+}
+
+void Graphics::setMapData(std::string mapDataString){
+	Logger::Write("Copying data to sprite flags\n");
+	copy_string_to_memory(mapData, mapDataString);
 }

 //start helper methods
@@ -544,6 +549,42 @@ void Graphics::clip(short x, short y, short w, short h) {
 	_gfxState_clip_h = h;
 }

+
+//map methods heavily based on tac08 implementation
+uint8_t Graphics::mget(short celx, short cely){
+	if (cely < 32) {
+
+		return this->mapData[cely * 128 + celx];
+	}
+
+	//todo: get this from the sprite sheet?
+	return 0;
+}
+
+void Graphics::mset(short celx, short cely, uint8_t snum){
+	if (cely < 32) {
+
+		this->mapData[cely * 128 + celx] = snum;
+	}
+
+	//todo: set on second half of sprite sheet
+}
+
+void Graphics::map(int celx, int cely, int sx, int sy, int celw, int celh) {
+	map(celx, cely, sx, sy, celw, celh, 0);
+}
+
+void Graphics::map(int celx, int cely, int sx, int sy, int celw, int celh, uint8_t layer) {
+	for (int y = 0; y < celh; y++) {
+		for (int x = 0; x < celw; x++) {
+			uint8_t cell = mget(celx + x, cely + y);
+			if (cell && ((layer == 0) || (fget(cell) & layer))) {
+				spr(cell, sx + x * 8, sy + y * 8);
+			}
+		}
+	}
+}
+
 void Graphics::flipBuffer(uint8_t* fb) {
 	short x, y;
     for(x = 0; x < 400; x++) {
diff --git a/source/graphics.h b/source/graphics.h
index d75ae69..dfda31a 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -98,6 +98,7 @@ class Graphics {

 	void setSpriteSheet(std::string spriteSheetString);
 	void setSpriteFlags(std::string spriteFlagsString);
+	void setMapData(std::string mapString);

 	void cls();
 	void cls(uint8_t color);
@@ -166,6 +167,12 @@ class Graphics {
 	void clip();
 	void clip(short x, short y, short w, short h);

+	uint8_t mget(short celx, short cely);
+	void mset(short celx, short cely, uint8_t snum);
+
+	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 flipBuffer(uint8_t* fb);

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index edb11f3..3fb7e2b 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -397,6 +397,44 @@ int clip(lua_State *L) {
     return 0;
 }

+int mget(lua_State *L) {
+    short celx = lua_tonumber(L,1);
+    short cely = lua_tonumber(L,2);
+
+    uint8_t result = _graphicsForLuaApi->mget(celx, cely);
+    lua_pushnumber(L, result);
+
+    return 1;
+}
+
+int mset(lua_State *L) {
+    short celx = lua_tonumber(L,1);
+    short cely = lua_tonumber(L,2);
+    uint8_t snum = lua_tonumber(L, 3);
+
+    _graphicsForLuaApi->mset(celx, cely, snum);
+
+    return 0;
+}
+
+int map(lua_State *L) {
+    short celx = lua_tonumber(L,1);
+    short cely = lua_tonumber(L,2);
+    short sx = lua_tonumber(L,3);
+    short sy = lua_tonumber(L,4);
+    short celw = lua_tonumber(L,5);
+    short celh = lua_tonumber(L,6);
+    uint8_t layer = 0;
+
+    if (lua_gettop(L) > 6){
+        layer = lua_tonumber(L,7);
+    }
+
+    _graphicsForLuaApi->map(celx, cely, sx, sy, celw, celh, layer);
+
+    return 0;
+}
+
 //Input

 //input api
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 6123e02..d56e4fe 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -33,6 +33,9 @@ int sget(lua_State *L);
 int sset(lua_State *L);
 int camera(lua_State *L);
 int clip(lua_State *L);
+int mget(lua_State *L);
+int mset(lua_State *L);
+int map(lua_State *L);

 //input api
 int btn(lua_State *L);
diff --git a/source/stringToDataHelpers.cpp b/source/stringToDataHelpers.cpp
index 5f315fa..bddfcdf 100644
--- a/source/stringToDataHelpers.cpp
+++ b/source/stringToDataHelpers.cpp
@@ -2,7 +2,7 @@

 #include "stringToDataHelpers.h"

-void copy_data_to_sprites(uint8_t sprite_data[128 * 64], std::string data) {
+void copy_string_to_sprite_memory(uint8_t sprite_data[128 * 64], std::string data) {
 	uint16_t i = 0;

 	for (size_t n = 0; n < data.length(); n++) {
@@ -27,7 +27,7 @@ void copy_data_to_sprites(uint8_t sprite_data[128 * 64], std::string data) {
 	}
 }

-void copy_data_to_sprite_flags(uint8_t sprite_flag_data[256], std::string data) {
+void copy_string_to_memory(uint8_t* sprite_flag_data, std::string data) {
 	uint16_t i = 0;

 	for (size_t n = 0; n < data.length(); n++) {
diff --git a/source/stringToDataHelpers.h b/source/stringToDataHelpers.h
index dbe498f..290a646 100644
--- a/source/stringToDataHelpers.h
+++ b/source/stringToDataHelpers.h
@@ -2,6 +2,6 @@

 #include <string>

-void copy_data_to_sprites(uint8_t sprite_data[128 * 64], std::string data);
+void copy_string_to_sprite_memory(uint8_t sprite_data[128 * 64], std::string data);

-void copy_data_to_sprite_flags(uint8_t sprite_flag_data[256], std::string data);
\ No newline at end of file
+void copy_string_to_memory(uint8_t* sprite_flag_data, std::string data);
\ No newline at end of file
diff --git a/testcart.p8 b/testcart.p8
index c113426..18aa485 100644
--- a/testcart.p8
+++ b/testcart.p8
@@ -28,6 +28,7 @@ function _draw()
  cls()

  rectfill(0, 0, 127, 127, 0)
+ map(0, 0, 0, 0, 3, 5)
  pset(1, 80, 10)
  pset(1.99, 80, 10)

@@ -247,5 +248,8 @@ __gff__
 0001020304050607080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000088ff
 __map__
-0102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000506
-1112000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001516
+0102040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000506
+1112040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001516
+0506040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+1516040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+0404040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000