commit bffe407ac60a37912e07b3b0a7c7ab5ae47cd68a
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Wed Apr 22 18:29:50 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Wed Apr 22 18:29:50 2020 -0600
Implement sget and sset
---
source/console.cpp | 2 ++
source/graphics.cpp | 29 +++++++++++++++++++++++++++++
source/graphics.h | 3 +++
source/picoluaapi.cpp | 18 ++++++++++++++++++
source/picoluaapi.h | 10 ++--------
5 files changed, 54 insertions(+), 8 deletions(-)
diff --git a/source/console.cpp b/source/console.cpp
index 3fea1d5..56155b7 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -73,6 +73,8 @@ void Console::LoadCart(std::string filename){
lua_register(_luaState, "sspr", sspr);
lua_register(_luaState, "fget", fget);
lua_register(_luaState, "fset", fset);
+ lua_register(_luaState, "sget", sget);
+ lua_register(_luaState, "sset", sset);
//input
lua_register(_luaState, "btn", btn);
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 2bd7342..c253644 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -378,6 +378,35 @@ void Graphics::fset(uint8_t n, uint8_t v){
spriteFlags[n] = v;
}
+uint8_t Graphics::sget(uint8_t x, uint8_t y){
+ short combinedIdx = y * 64 + (x / 2);
+
+ uint8_t combinedPix = this->spriteSheetData[combinedIdx];
+
+ uint8_t c = x % 2 == 0
+ ? combinedPix & 0x0f //just first 4 bits
+ : combinedPix >> 4; //just last 4 bits
+
+ return c;
+}
+
+void Graphics::sset(uint8_t x, uint8_t y, uint8_t c){
+ short combinedIdx = y * 64 + (x / 2);
+
+ uint8_t currentByte = this->spriteSheetData[combinedIdx];
+ uint8_t mask;
+ // set just 4 bits: https://stackoverflow.com/a/4439221
+ if (x % 2 == 0) {
+ mask = 0x0f;
+ }
+ else {
+ c = c << 4;
+ mask = 0xf0;
+ }
+
+ this->spriteSheetData[combinedIdx] = (currentByte & ~mask) | (c & mask);
+}
+
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 3a99dac..4c25ec2 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -116,6 +116,9 @@ class Graphics {
void fset(uint8_t n, uint8_t f, bool v);
void fset(uint8_t n, uint8_t v);
+ uint8_t sget(uint8_t x, uint8_t y);
+ void sset(uint8_t x, uint8_t y, uint8_t c);
+
void flipBuffer(uint8_t* fb);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index d12b518..420066f 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -204,6 +204,24 @@ int fset(lua_State *L) {
return 0;
}
+int sget(lua_State *L) {
+ int x = lua_tointeger(L,1);
+ int y = lua_tointeger(L,2);
+ uint8_t result = _graphicsForLuaApi->sget((uint8_t)x, (uint8_t)y);
+ lua_pushinteger(L, result);
+
+ return 1;
+}
+
+int sset(lua_State *L) {
+ int x = lua_tointeger(L,1);
+ int y = lua_tointeger(L,2);
+ int c = lua_tointeger(L,3);
+ _graphicsForLuaApi->sset((uint8_t)x, (uint8_t)y, (uint8_t)c);
+
+ return 0;
+}
+
//Input
//input api
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 16ad3b9..27c1aa2 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -29,6 +29,8 @@ int spr(lua_State *L);
int sspr(lua_State *L);
int fget(lua_State *L);
int fset(lua_State *L);
+int sget(lua_State *L);
+int sset(lua_State *L);
//input api
int btn(lua_State *L);
@@ -38,14 +40,6 @@ int btnp(lua_State *L);
//TODO:
//graphics
-//api.fget(n, f)
-
-//api.fset(n, f, v)
-
-//api.sget(x, y)
-
-//api.sset(x, y, c)
-
//api.flip()
//int flip(lua_State *L);
//api.camera(x, y)