commit c332d9ec1247b134d8e292c0f1e3bd79dfe1087b
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Fri Nov 27 12:45:58 2020 -0700
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Fri Nov 27 12:45:58 2020 -0700
Implement oval and ovalfill
---
LICENSE.MD | 3 +
source/graphics.cpp | 193 ++++++++++++++++++++++++++++++++++++++++++++++++-
source/graphics.h | 6 ++
source/picoluaapi.cpp | 43 +++++++++++
source/picoluaapi.h | 2 +
source/vm.cpp | 2 +
test/endtoendtests.cpp | 2 +-
test/graphicstests.cpp | 37 ++++++++++
test/vmtests.cpp | 1 -
9 files changed, 286 insertions(+), 3 deletions(-)
diff --git a/LICENSE.MD b/LICENSE.MD
index d6ff173..3fb85c8 100644
--- a/LICENSE.MD
+++ b/LICENSE.MD
@@ -41,6 +41,9 @@
- Website: https://github.com/switchbrew/libnx
- License: ISC
+# oval drawing implementation:
+- Website: //https://stackoverflow.com/a/8448181
+- License: (CC BY-SA 3.0) https://creativecommons.org/licenses/by-sa/3.0/
# **License Text**
## MIT license
diff --git a/source/graphics.cpp b/source/graphics.cpp
index b902027..632a2a4 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -595,7 +595,198 @@ void Graphics::circfill(int ox, int oy, int r, uint8_t col){
err += ++y * 2 + 1;
} while (x < 0);
}
-
+}
+
+void Graphics::oval(int x0, int y0, int x1, int y1) {
+ this->oval(x0, y0, x1, y1, _memory->drawState.color);
+}
+
+//https://stackoverflow.com/a/8448181
+void Graphics::oval(int x0, int y0, int x1, int y1, uint8_t col) {
+ color(col);
+
+ applyCameraToPoint(&x0, &y0);
+ applyCameraToPoint(&x1, &y1);
+
+ sortCoordsForRect(&x0, &y0, &x1, &y1);
+
+ //x radius and y radius
+ int xr = (x1 - x0) / 2;
+ int yr = (y1 - y0) / 2;
+
+ //center location
+ int xc = x0 + xr;
+ int yc = y0 + yr;
+
+ int wx, wy;
+ int thresh;
+ int asq = xr * xr;
+ int bsq = yr * yr;
+ int xa, ya;
+
+ _private_safe_pset(xc, yc+yr, col);
+ _private_safe_pset(xc, yc-yr, col);
+
+ wx = 0;
+ wy = yr;
+ xa = 0;
+ ya = asq * 2 * yr;
+ thresh = asq / 4 - asq * yr;
+
+ for (;;) {
+ thresh += xa + bsq;
+
+ if (thresh >= 0) {
+ ya -= asq * 2;
+ thresh -= ya;
+ wy--;
+ }
+
+ xa += bsq * 2;
+ wx++;
+
+ if (xa >= ya)
+ break;
+
+
+ _private_safe_pset(xc+wx, yc-wy, col);
+ _private_safe_pset(xc-wx, yc-wy, col);
+ _private_safe_pset(xc+wx, yc+wy, col);
+ _private_safe_pset(xc-wx, yc+wy, col);
+ }
+
+ _private_safe_pset(xc+xr, yc, col);
+ _private_safe_pset(xc-xr, yc, col);
+
+ wx = xr;
+ wy = 0;
+ xa = bsq * 2 * xr;
+
+ ya = 0;
+ thresh = bsq / 4 - bsq * xr;
+
+ for (;;) {
+ thresh += ya + asq;
+
+ if (thresh >= 0) {
+ xa -= bsq * 2;
+ thresh = thresh - xa;
+ wx--;
+ }
+
+ ya += asq * 2;
+ wy++;
+
+ if (ya > xa)
+ break;
+
+ _private_safe_pset(xc+wx, yc-wy, col);
+ _private_safe_pset(xc-wx, yc-wy, col);
+ _private_safe_pset(xc+wx, yc+wy, col);
+ _private_safe_pset(xc-wx, yc+wy, col);
+ }
+}
+
+void Graphics::ovalfill(int x0, int y0, int x1, int y1) {
+ this->ovalfill(x0, y0, x1, y1, _memory->drawState.color);
+}
+
+//https://stackoverflow.com/a/8448181
+void Graphics::ovalfill(int x0, int y0, int x1, int y1, uint8_t col){
+ color(col);
+
+ applyCameraToPoint(&x0, &y0);
+ applyCameraToPoint(&x1, &y1);
+
+ sortCoordsForRect(&x0, &y0, &x1, &y1);
+
+ //x radius and y radius
+ int xr = (x1 - x0) / 2;
+ int yr = (y1 - y0) / 2;
+
+ //center location
+ int xc = x0 + xr;
+ int yc = y0 + yr;
+
+ int wx, wy;
+ int thresh;
+ int asq = xr * xr;
+ int bsq = yr * yr;
+ int xa, ya;
+
+ _private_v_line(yc+yr, yc-yr, xc, col);
+
+ wx = 0;
+ wy = yr;
+ xa = 0;
+ ya = asq * 2 * yr;
+ thresh = asq / 4 - asq * yr;
+
+ for (;;) {
+ thresh += xa + bsq;
+
+ if (thresh >= 0) {
+ ya -= asq * 2;
+ thresh -= ya;
+ wy--;
+ }
+
+ xa += bsq * 2;
+ wx++;
+
+ if (xa >= ya)
+ break;
+
+ _private_h_line(xc+wx, xc-wx, yc-wy, col);
+ _private_h_line(xc+wx, xc-wx, yc+wy, col);
+ }
+
+ _private_h_line(xc+xr, xc-xr, yc, col);
+
+ wx = xr;
+ wy = 0;
+ xa = bsq * 2 * xr;
+
+ ya = 0;
+ thresh = bsq / 4 - bsq * xr;
+
+ for (;;) {
+ thresh += ya + asq;
+
+ if (thresh >= 0) {
+ xa -= bsq * 2;
+ thresh = thresh - xa;
+ wx--;
+ }
+
+ ya += asq * 2;
+ wy++;
+
+ if (ya > xa)
+ break;
+
+ _private_h_line(xc+wx, xc-wx, yc-wy, col);
+ _private_h_line(xc+wx, xc-wx, yc+wy, col);
+ }
+ /*
+ //this algo doesn't like up correctly
+ //center line
+ _private_h_line(xc - xr, xc + xr, yc, col);
+
+ for (int y = 1; y <= yr; y++) {
+ int x = currWidth - (dx - 1); // try slopes of dx - 1 or more
+ for ( ; x > 0; x--) {
+ if (x*x*hh + y*y*ww <= hhww) {
+ break;
+ }
+ }
+ dx = currWidth - x; // current approximation of the slope
+ currWidth = x;
+
+ _private_h_line(xc - currWidth, xc + currWidth, yc + y, col);
+ _private_h_line(xc - currWidth, xc + currWidth, yc - y, col);
+ }
+ */
}
void Graphics::rect(int x1, int y1, int x2, int y2) {
diff --git a/source/graphics.h b/source/graphics.h
index 6542663..bc82129 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -111,6 +111,12 @@ class Graphics {
void circfill(int ox, int oy, int r);
void circfill(int ox, int oy, int r, uint8_t col);
+ void oval(int x0, int y0, int x1, int y1);
+ void oval(int x0, int y0, int x1, int y1, uint8_t col);
+
+ void ovalfill(int x0, int y0, int x1, int y1);
+ void ovalfill(int x0, int y0, int x1, int y1, uint8_t col);
+
void rect(int x1, int y1, int x2, int y2);
void rect(int x1, int y1, int x2, int y2, uint8_t col);
void rectfill(int x1, int y1, int x2, int y2);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index cc7679d..39e5986 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -173,6 +173,49 @@ int circfill(lua_State *L){
return 0;
}
+int oval(lua_State *L){
+
+ if (lua_gettop(L) >= 4) {
+ int x1 = lua_tonumber(L,1);
+ int y1 = lua_tonumber(L,2);
+ int x2 = lua_tonumber(L,3);
+ int y2 = lua_tonumber(L,4);
+
+ if (lua_gettop(L) == 4){
+ _graphicsForLuaApi->oval(x1, y1, x2, y2);
+
+ }
+ else {
+ uint8_t c = lua_tonumber(L,5);
+
+ _graphicsForLuaApi->oval(x1, y1, x2, y2, c);
+ }
+ }
+
+ return 0;
+}
+
+int ovalfill(lua_State *L){
+ if (lua_gettop(L) >= 4) {
+ int x1 = lua_tonumber(L,1);
+ int y1 = lua_tonumber(L,2);
+ int x2 = lua_tonumber(L,3);
+ int y2 = lua_tonumber(L,4);
+
+ if (lua_gettop(L) == 4){
+ _graphicsForLuaApi->ovalfill(x1, y1, x2, y2);
+
+ }
+ else {
+ fix32 c = lua_tonumber(L,5);
+
+ _graphicsForLuaApi->ovalfill(x1, y1, x2, y2, c);
+ }
+ }
+
+ return 0;
+}
+
int rect(lua_State *L){
if (lua_gettop(L) >= 4) {
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 4c2c9e8..f946062 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -23,6 +23,8 @@ int color(lua_State *L);
int line (lua_State *L);
int circ(lua_State *L);
int circfill(lua_State *L);
+int oval(lua_State *L);
+int ovalfill(lua_State *L);
int rect(lua_State *L);
int rectfill(lua_State *L);
int print(lua_State *L);
diff --git a/source/vm.cpp b/source/vm.cpp
index 311af48..aef7e93 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -167,6 +167,8 @@ bool Vm::loadCart(Cart* cart) {
lua_register(_luaState, "line", line);
lua_register(_luaState, "circ", circ);
lua_register(_luaState, "circfill", circfill);
+ lua_register(_luaState, "oval", oval);
+ lua_register(_luaState, "ovalfill", ovalfill);
lua_register(_luaState, "rect", rect);
lua_register(_luaState, "rectfill", rectfill);
lua_register(_luaState, "print", print);
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 99d2e5f..98c26ed 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -393,7 +393,7 @@ TEST_CASE("Loading and running carts") {
};
vector<string> expectedMissing {
- "oval", "ovalfill", "tline"
+ "tline"
};
string missing = "";
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index a1ca840..b1df456 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -449,6 +449,43 @@ TEST_CASE("graphics class behaves as expected") {
checkPoints(graphics, expectedPoints);
}
+ SUBCASE("oval({x0}, {x1}, {y0}, {y1}, {c}) draws an ellipse") {
+ graphics->cls();
+ graphics->oval(40, 40, 45, 42, 13);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {40,41,13},
+ {41,40,13},
+ {41,42,13},
+ {42,40,13},
+ {42,42,13},
+ {43,40,13},
+ {43,42,13},
+ {44,41,13}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
+ SUBCASE("ovalfill({x0}, {x1}, {y0}, {y1}, {c}) fills an ellipse") {
+ graphics->cls();
+ graphics->ovalfill(40, 40, 45, 42, 13);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {40,41,13},
+ {41,40,13},
+ {41,41,13},
+ {41,42,13},
+ {42,40,13},
+ {42,41,13},
+ {42,42,13},
+ {43,40,13},
+ {43,41,13},
+ {43,42,13},
+ {44,41,13}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
SUBCASE("rect({x1}, {y1}, {x2}, {y2}) uses pen color") {
graphics->cls();
graphics->color(15);
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index ce95075..1b3ca10 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -407,7 +407,6 @@ TEST_CASE("Vm memory functions") {
vm->UpdateAndDraw();
bool btnbits = vm->ExecuteLua(
"function btnbitstest1()\n"
- " printh(\"hello btnbits: \" .. btnbits .. \"\\\n\")"
" return btnbits == 1\n"
"end\n",
"btnbitstest1");