foxygit / fake-08-xbox360 Log in
commit bce684d5ee82fd90e0608b21f47c2d9131a0b4a6
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Wed Apr 8 19:09:57 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Wed Apr 8 19:09:57 2020 -0600

    implement line drawing
---
 source/fakecart.c | 10 ++++++++++
 source/graphics.c | 55 ++++++++++++++++++++++++++++++++++++++++---------------
 source/graphics.h |  2 ++
 3 files changed, 52 insertions(+), 15 deletions(-)

diff --git a/source/fakecart.c b/source/fakecart.c
index 7276b8c..03af485 100644
--- a/source/fakecart.c
+++ b/source/fakecart.c
@@ -34,6 +34,16 @@ void _draw() {
     rect((short)r2x1, (short)r2y1, (short)r2x0, (short)r2y0, r2c);

     pset(40, 10, 13);
+
+    line(120, 10, 120, 120, 14);
+
+    line(0, 0, 20, 20, 15);
+
+    line(0, 20, 20, 0, 15);
+
+    line(20, 30, 0, 20, 12);
+
+    line(0, 30, 20, 20, 12);
 }

 void _update() {
diff --git a/source/graphics.c b/source/graphics.c
index 0ecb350..f0622a1 100644
--- a/source/graphics.c
+++ b/source/graphics.c
@@ -61,12 +61,6 @@ void swap(short *x, short *y) {
    return;
 }

-void sortcoords(short *expectedLower, short *expectedHigher){
-	if (*expectedHigher < *expectedLower) {
-		swap(expectedLower, expectedHigher);
-	}
-}
-
 bool isOnScreen(short *x, short* y) {
 	return *x >= 0 && *x < 127 && *y >= 0 && *y < 127;
 }
@@ -94,14 +88,45 @@ void color(uint16_t col){
 	_graphicsState.color = col;
 }

+void line (short x1, short y1, short x2, short y2, uint16_t col) {
+	if (x1 > x2) {
+		swap(&x1, &x2);
+		swap(&y1, &y2);
+	}
+
+	float run = x2 - x1;
+	float rise = y2 - y1;
+
+	//vertical line
+	if (run == 0) {
+		if (y1 > y2) {
+			swap(&y1, &y2);
+		}
+
+		for (short y = y1; y <= y2; y++){
+			pset(x1, y, col);
+		}
+	}
+	else {
+		float slope = rise / run;
+
+		for (short x = x1; x <= x2; x++){
+			short y = y1 + (short)ceil((float)x * slope);
+			pset(x, y, col);
+		}
+	}
+}
+
 void rect(short x1, short y1, short x2, short y2, uint16_t col) {
-	sortcoords(&x1, &x2);
-	sortcoords(&y1, &y2);
+	if (x1 > x2) {
+		swap(&x1, &x2);
+		swap(&y1, &y2);
+	}

 	for (short i = x1; i <= x2; i++) {
 		for (short j = y1; j <= y2; j++) {
-			if (isOnScreen(&i, &j) && (i == x1 || i == x2 || j == y1 || j == y2) ) {
-				_pico8_fb[(i * 128) + j] = col;
+			if ((i == x1 || i == x2 || j == y1 || j == y2) ) {
+				pset(i, j, col);
 			}
 		}
 	}
@@ -109,14 +134,14 @@ void rect(short x1, short y1, short x2, short y2, uint16_t col) {
 }

 void rectfill(short x1, short y1, short x2, short y2, uint16_t col) {
-	sortcoords(&x1, &x2);
-	sortcoords(&y1, &y2);
+	if (x1 > x2) {
+		swap(&x1, &x2);
+		swap(&y1, &y2);
+	}

 	for (short i = x1; i <= x2; i++) {
 		for (short j = y1; j <= y2; j++) {
-			if (isOnScreen(&i, &j)) {
-				_pico8_fb[(i * 128) + j] = col;
-			}
+			pset(i, j, col);
 		}
 	}
 }
diff --git a/source/graphics.h b/source/graphics.h
index 2048e3a..5123cc6 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -41,6 +41,8 @@ uint16_t pget(short x, short y);

 void color(uint16_t c);

+void line (short x1, short y1, short x2, short y2, uint16_t col);
+
 void rect(short x1, short y1, short x2, short y2, uint16_t col);
 void rectfill(short x1, short y1, short x2, short y2, uint16_t col);