commit 4f96944b9807a18017b3fd2cf74572edf8612065
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Nov 28 14:23:42 2020 -0700
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Nov 28 14:23:42 2020 -0700
Add tline implementation ported from zepto8
---
source/graphics.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++
source/graphics.h | 3 ++
source/picoluaapi.cpp | 22 ++++++++++++
source/picoluaapi.h | 1 +
source/vm.cpp | 1 +
test/endtoendtests.cpp | 9 -----
test/graphicstests.cpp | 27 +++++++++++++++
7 files changed, 147 insertions(+), 9 deletions(-)
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 632a2a4..9028116 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -522,6 +522,99 @@ void Graphics::line(int x0, int y0, int x1, int y1, uint8_t col) {
}
}
+
+void Graphics::tline(int x0, int y0, int x1, int y1, fix32 mx, fix32 my){
+ tline(
+ x0,
+ y0,
+ x1,
+ y1,
+ mx,
+ my,
+ fix32::frombits(0x2000), // 1/8
+ fix32(0)
+ );
+}
+
+//proted from zepto 8 impl
+void Graphics::tline(int x0, int y0, int x1, int y1, fix32 mx, fix32 my, fix32 mdx, fix32 mdy){
+ applyCameraToPoint(&x0, &y0);
+ applyCameraToPoint(&x1, &y1);
+
+ //determine whether x or y coordinates need to get incremented
+ bool xDifGreater = std::abs(x1 - x0) >= std::abs(y1 - y0);
+ int dx = xDifGreater ? x0 <= x1 ? 1 : -1 : 0;
+ int dy = xDifGreater ? 0 : y0 <= y1 ? 1 : -1;
+
+ bool vertical = x0 == x1;
+
+ int x = clampCoordToScreenDims(x0);
+ int xend = clampCoordToScreenDims(x1);
+ int y = clampCoordToScreenDims(y0);
+ int yend = clampCoordToScreenDims(y1);
+
+ auto &ds = _memory->drawState;
+
+ // Retrieve masks for wrap-around and subtract 0x0.0001
+ fix32 xmask = fix32(ds.tlineMapWidth) - fix32::frombits(1);
+ fix32 ymask = fix32(ds.tlineMapHeight) - fix32::frombits(1);
+
+ // Advance texture coordinates; do it in steps to avoid overflows
+ int delta = abs(xDifGreater ? x - x0 : y - y0);
+ while (delta) {
+ int step = std::min(8192, delta);
+ mx = (mx & ~xmask) | ((mx + mdx * fix32(step)) & xmask);
+ my = (my & ~ymask) | ((my + mdy * fix32(step)) & ymask);
+ delta -= step;
+ }
+
+ for (;;) {
+ // Find sprite in map memory
+ int sx = (ds.tlineMapXOffset + int(mx)) & 0x7f;
+ int sy = (ds.tlineMapYOffset + int(my)) & 0x3f;
+ uint8_t sprite = mget(sx, sy);
+ //uint8_t bits = fget(sprite);
+
+ int spr_x = (sprite % 16) * 8;
+ int spr_y = (sprite / 16) * 8;
+
+ // If found, draw pixel //todo layer param
+ //if (cell && ((layer == 0) || (fget(cell) & layer))) {
+ if (sprite) {
+ //uint8_t col = _memory->spriteSheetData.gfx.get(spr_x + (int(mx << 3) & 0x7),
+ // spr_y + (int(my << 3) & 0x7));
+ uint8_t col = getPixelNibble(
+ spr_x + (int(mx << 3) & 0x7),
+ spr_y + (int(my << 3) & 0x7),
+ _memory->spriteSheetData);
+
+ if (!isColorTransparent(col)) {
+ _private_pset(x, y, getDrawPalMappedColor(col));
+ }
+ }
+
+ // Advance source coordinates
+ mx = (mx & ~xmask) | ((mx + mdx) & xmask);
+ my = (my & ~ymask) | ((my + mdy) & ymask);
+
+ // Advance destination coordinates
+ if (xDifGreater) {
+ if (x == xend)
+ break;
+ x += dx;
+ y = y0 + ((float)(x - x0) / (x1 - x0)) * (y1 - y0);
+ }
+ else {
+ if (y == yend)
+ break;
+ y += dy;
+ if (! vertical) {
+ x = x0 + ((float)(y - y0) / (y1 - y0)) * (x1 - x0);
+ }
+ }
+ }
+}
+
void Graphics::circ(int ox, int oy){
this->circ(ox, oy, 4);
}
diff --git a/source/graphics.h b/source/graphics.h
index bc82129..632fba4 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -104,6 +104,9 @@ class Graphics {
void line (int x1, int y1, int x2, int y2);
void line (int x1, int y1, int x2, int y2, uint8_t col);
+ void tline(int x0, int y0, int x1, int y1, fix32 mx, fix32 my);
+ void tline(int x0, int y0, int x1, int y1, fix32 mx, fix32 my, fix32 mdx, fix32 mdy);
+
void circ(int ox, int oy);
void circ(int ox, int oy, int r);
void circ(int ox, int oy, int r, uint8_t col);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 39e5986..fc913d8 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -131,6 +131,28 @@ int line (lua_State *L){
return 0;
}
+int tline (lua_State *L){
+ int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
+ fix32 mx = 0, my = 0, mdx = fix32::frombits(0x2000), mdy = 0;
+
+ if (lua_gettop(L) >= 6) {
+ x0 = lua_tonumber(L,1);
+ y0 = lua_tonumber(L,2);
+ x1 = lua_tonumber(L,3);
+ y1 = lua_tonumber(L,4);
+ mx = lua_tonumber(L,5);
+ my = lua_tonumber(L,6);
+ }
+ if (lua_gettop(L) > 7){
+ mdx = lua_tonumber(L,7);
+ mdy = lua_tonumber(L,8);
+ }
+
+ _graphicsForLuaApi->tline(x0, y0, x1, y1, mx, my, mdx, mdy);
+
+ return 0;
+}
+
int circ(lua_State *L){
int ox = lua_tonumber(L,1);
int oy = lua_tonumber(L,2);
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index f946062..a6702d7 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -21,6 +21,7 @@ int pset(lua_State *L);
int pget(lua_State *L);
int color(lua_State *L);
int line (lua_State *L);
+int tline (lua_State *L);
int circ(lua_State *L);
int circfill(lua_State *L);
int oval(lua_State *L);
diff --git a/source/vm.cpp b/source/vm.cpp
index aef7e93..739143b 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -165,6 +165,7 @@ bool Vm::loadCart(Cart* cart) {
lua_register(_luaState, "pget", pget);
lua_register(_luaState, "color", color);
lua_register(_luaState, "line", line);
+ lua_register(_luaState, "tline", tline);
lua_register(_luaState, "circ", circ);
lua_register(_luaState, "circfill", circfill);
lua_register(_luaState, "oval", oval);
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 98c26ed..7398a6d 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -392,18 +392,9 @@ TEST_CASE("Loading and running carts") {
"bor", "bxor", "bnot", "shl", "shr", "lshr", "rotl", "rotr"
};
- vector<string> expectedMissing {
- "tline"
- };
-
string missing = "";
for (auto apiFunction : apiFunctions) {
- if (std::find(expectedMissing.begin(), expectedMissing.end(), apiFunction) != expectedMissing.end())
- {
- continue;
- }
-
std::stringstream ss;
ss << "function globalfunctest()\n return " << apiFunction << " ~= nil\n end\n";
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index b1df456..01f3ec8 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1648,6 +1648,33 @@ TEST_CASE("graphics class behaves as expected") {
CHECK_EQ(picoRam.drawState.text_y, 0);
CHECK_EQ(picoRam.drawState.color, 3);
}
+ SUBCASE("tline draws sprites from map"){
+ for(int i = 0; i < 128; i++) {
+ for (int j = 0; j < 32; j++)
+ graphics->sset(i, j, i%16);
+ }
+ for(int x = 0; x < 128; x++) {
+ for (int y = 0; y < 32; y++) {
+ graphics->mset(x, y, (x + y) % 16);
+ }
+ }
+
+ graphics->tline(27, 34, 34, 41, 3, 4);
+
+ //diagonal across screen- 2x2 cells, 16x16 pixels
+ std::vector<coloredPoint> expectedPoints = {
+ {27,34,8},
+ {28,35,9},
+ {29,36,10},
+ {30,37,11},
+ {31,38,12},
+ {32,39,13},
+ {33,40,14},
+ {34,41,15}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
//general teardown