commit 13e4f53e7b94599e87727c88546c7276a5fffda3
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Mon Apr 20 22:27:48 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Mon Apr 20 22:27:48 2020 -0600
Add basic sspr implementation
---
source/console.cpp | 1 +
source/graphics.cpp | 78 ++++++++++++++++++++++++-
source/graphics.h | 27 ++++++++-
source/picoluaapi.cpp | 27 +++++++++
source/picoluaapi.h | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++
testcart.p8 | 2 +
6 files changed, 285 insertions(+), 4 deletions(-)
diff --git a/source/console.cpp b/source/console.cpp
index 49536b6..d7c3418 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -67,6 +67,7 @@ void Console::LoadCart(std::string filename){
lua_register(_luaState, "rectfill", rectfill);
lua_register(_luaState, "print", print);
lua_register(_luaState, "spr", spr);
+ lua_register(_luaState, "sspr", sspr);
lua_register(_luaState, "btn", btn);
lua_register(_luaState, "btnp", btnp);
diff --git a/source/graphics.cpp b/source/graphics.cpp
index bf7c86f..a37dd83 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -94,6 +94,62 @@ void Graphics::copySpriteToScreen(
}
}
+//based on tac08 implementation of stretch_blitter()
+//uses ints so we can shift bits and do integer division instead of floating point
+void Graphics::copyStretchSpriteToScreen(
+ uint8_t spritebuffer[],
+ int spr_x,
+ int spr_y,
+ int spr_w,
+ int spr_h,
+ int scr_x,
+ int scr_y,
+ int scr_w,
+ int scr_h,
+ bool flip_x = false,
+ bool flip_y = false)
+{
+ if (false || (spr_h == scr_h && spr_w == scr_w)) {
+ // use faster non stretch blitter if sprite is not stretched
+ copySpriteToScreen(spritebuffer, scr_x, scr_y, spr_x, spr_y, scr_w, scr_h, flip_x, flip_y);
+ return;
+ }
+
+ //shift bits to avoid floating point math
+ spr_x = spr_x << 16;
+ spr_y = spr_y << 16;
+ spr_w = spr_w << 16;
+ spr_h = spr_h << 16;
+
+ int dx = spr_w / scr_w;
+ int dy = spr_h / scr_h;
+
+ if (flip_y) {
+ spr_y += spr_h - 1 * dy;
+ dy = -dy;
+ }
+
+ for (int y = 0; y < scr_h; y++) {
+ uint8_t* spr = spritebuffer + (((spr_y + y * dy) >> 16) & 0x7f) * 128;
+
+ if (!flip_x) {
+ for (int x = 0; x < scr_w; x++) {
+ uint8_t c = spr[((spr_x + x * dx) >> 16) & 0x7f];
+ if (c != 0) {
+ pset(scr_x + x, scr_y + y, c);
+ }
+ }
+ } else {
+ for (int x = 0; x < scr_w; x++) {
+ uint8_t c = spr[((spr_x + spr_w - (x + 1) * dx) >> 16) & 0x7f];
+ if (c != 0) {
+ pset(scr_x + x, scr_y + y, c);
+ }
+ }
+ }
+ }
+}
+
void Graphics::swap(short *x, short *y) {
short temp;
temp = *x;
@@ -275,12 +331,28 @@ short Graphics::print(std::string str, short x, short y, uint16_t c) {
return x;
}
-void Graphics::spr(int n, int x, int y, int w, int h, bool flip_x, bool flip_y) {
- int spr_x = (n % 16) * 8;
- int spr_y = (n / 16) * 8;
+void Graphics::spr(short n, short x, short y, short w, short h, bool flip_x, bool flip_y) {
+ short spr_x = (n % 16) * 8;
+ short spr_y = (n / 16) * 8;
copySpriteToScreen(spriteSheetData, x, y, spr_x, spr_y, w * 8, h * 8, flip_x, flip_y);
}
+void Graphics::sspr(
+ short sx,
+ short sy,
+ short sw,
+ short sh,
+ short dx,
+ short dy,
+ short dw,
+ short dh,
+ bool flip_x,
+ bool flip_y)
+{
+ copyStretchSpriteToScreen(spriteSheetData, sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y);
+
+}
+
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 870f8ec..8ee0885 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -51,6 +51,19 @@ class Graphics {
bool flip_x,
bool flip_y);
+ void copyStretchSpriteToScreen(
+ uint8_t spritebuffer[],
+ int spr_x,
+ int spr_y,
+ int spr_w,
+ int spr_h,
+ int scr_x,
+ int scr_y,
+ int scr_w,
+ int scr_h,
+ bool flip_x,
+ bool flip_y);
+
void swap(short *x, short *y);
void sortPointsLtoR(short *x1, short *y1, short *x2, short *y2);
@@ -80,7 +93,19 @@ class Graphics {
void rectfill(short x1, short y1, short x2, short y2, uint8_t col);
short print(std::string str, short x, short y, uint16_t c);
- void spr(int n, int x, int y, int w, int h, bool flip_x, bool flip_y);
+ void spr(short n, short x, short y, short w, short h, bool flip_x, bool flip_y);
+
+ void sspr(
+ short sx,
+ short sy,
+ short sw,
+ short sh,
+ short dx,
+ short dy,
+ short dw,
+ short dh,
+ bool flip_x,
+ bool flip_y);
void flipBuffer(uint8_t* fb);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 749686a..52e6554 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -129,6 +129,33 @@ int spr(lua_State *L) {
return 0;
}
+int sspr(lua_State *L) {
+ double sx = lua_tonumber(L,1);
+ double sy = lua_tonumber(L,2);
+ double sw = lua_tonumber(L,3);
+ double sh = lua_tonumber(L,4);
+ double dx = lua_tonumber(L,5);
+ double dy = lua_tonumber(L,6);
+ double dw = lua_tonumber(L,7);
+ double dh = lua_tonumber(L,8);
+ bool flip_x = lua_toboolean(L,9);
+ bool flip_y = lua_toboolean(L,10);
+
+ _graphicsForLuaApi->sspr(
+ (short)sx,
+ (short)sy,
+ (short)sw,
+ (short)sh,
+ (short)dx,
+ (short)dy,
+ (short)dw,
+ (short)dh,
+ flip_x,
+ flip_y);
+
+ return 0;
+}
+
//Input
//input api
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 724a031..1e641bd 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -26,7 +26,161 @@ int rect(lua_State *L);
int rectfill(lua_State *L);
int print(lua_State *L);
int spr(lua_State *L);
+int sspr(lua_State *L);
//input api
int btn(lua_State *L);
int btnp(lua_State *L);
+
+
+//TODO:
+//graphics
+//api.sspr(sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y)
+
+//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)
+//int camera(lua_State *L);
+//api.clip(x, y, w, h)
+//int clip(lua_State *L);
+//api.cursor(x, y)
+//int cursor(lua_State *L);
+//api.pal(c0, c1, p)
+
+//api.palt(c, t)
+
+//api.fillp(p)
+
+//map
+//api.map(cel_x, cel_y, sx, sy, cel_w, cel_h, bitmask)
+
+//api.mget(x, y)
+
+//api.mset(x, y, v)
+
+
+//system functions
+//api.time() and api.t()
+
+//api.tonum(val)
+
+//api.tostr(val, hex)
+
+//api.rnd(x)
+
+//api.srand(seed)
+
+//api.flr=math.floor
+
+//api.ceil=math.ceil
+
+//api.sgn(x)
+
+//api.abs=math.abs
+
+//api.min(a, b)
+
+//api.max(a, b)
+
+//api.mid(x, y, z)
+
+//api.cos(x)
+
+//api.sin(x)
+
+//api.sqrt=math.sqrt
+
+//api.atan2(x, y)
+
+//api.band(x, y)
+
+//api.bor(x, y)
+
+//api.bxor(x, y)
+
+//api.bnot(x)
+
+//api.shl(x, y)
+
+//api.shr(x, y)
+
+//api.lshr(x, y)
+
+//api.rotl(x, y)
+
+//api.rotr(x, y)
+
+//sound
+//api.music(n, fade_len, channel_mask)
+
+//api.sfx(n, channel, offset)
+
+
+//not to do any time soon, but don't crash?
+//directory stuff
+//api.load(filename)
+
+//api.save()
+
+//api.run()
+
+//api.stop()
+
+//api.reboot()
+
+//api.shutdown()
+
+//api.exit()
+
+//api.info()
+
+//api.export()
+
+//api.import()
+
+//api.help()
+
+//api.folder()
+
+//api.ls()
+
+//api.dir=api.ls
+
+//api.cd()
+
+//api.mkdir()
+
+//api.install_demos()
+
+//api.install_games()
+
+//api.keyconfig()
+
+//api.splore()
+
+//memory
+//api.peek(addr)
+
+//api.poke(addr, val)
+
+//api.peek4(addr)
+
+//api.poke4(addr, val)
+
+//api.memcpy(dest_addr, source_addr, len)
+
+//api.memset(dest_addr, val, len)
+
+//api.reload(dest_addr, source_addr, len)
+
+//api.cstore(dest_addr, source_addr, len)
+
+//
\ No newline at end of file
diff --git a/testcart.p8 b/testcart.p8
index b96aab3..1f4f2d4 100644
--- a/testcart.p8
+++ b/testcart.p8
@@ -56,6 +56,8 @@ function _draw()
print("test str", 64, 64, 7)
spr(sprn, sprx, spry, 1, 1, false, false)
+
+ sspr(64, 0, 8, 8, 72, 48, 12, 20, false, false)
end
function _update()