foxygit / fake-08-xbox360 Log in
commit 81987d5bbe5facbc84a2671557afa5b29ab5e5b8
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Wed Apr 8 11:35:41 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Wed Apr 8 11:35:41 2020 -0600

    implement pset and pget
---
 source/fakecart.c |  2 ++
 source/graphics.c | 31 ++++++++++++++++++++++++-------
 source/graphics.h |  3 +++
 3 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/source/fakecart.c b/source/fakecart.c
index 64a1181..7276b8c 100644
--- a/source/fakecart.c
+++ b/source/fakecart.c
@@ -32,6 +32,8 @@ void _draw() {
     rectfill((short)r1x0, (short)r1y0, (short)r1x1, (short)r1y1, r1c);

     rect((short)r2x1, (short)r2y1, (short)r2x0, (short)r2y0, r2c);
+
+    pset(40, 10, 13);
 }

 void _update() {
diff --git a/source/graphics.c b/source/graphics.c
index 7a36303..30b4b97 100644
--- a/source/graphics.c
+++ b/source/graphics.c
@@ -58,18 +58,35 @@ void sortcoords(short *expectedLower, short *expectedHigher){
 	}
 }

+bool isOnScreen(short *x, short* y) {
+	return *x >= 0 && *x < 127 && *y >= 0 && *y < 127;
+}
+
 void cls() {
 	memset(_pico8_fb, 0, sizeof(_pico8_fb));
 }

+void pset(short x, short y, uint16_t col){
+	if (isOnScreen(&x, &y)){
+		_pico8_fb[(x * 128) + y] = col;
+	}
+}
+
+uint16_t pget(short x, short y){
+	if (isOnScreen(&x, &y)){
+		return _pico8_fb[(x * 128) + y];
+	}
+
+	return 0;
+}
+
 void rect(short x1, short y1, short x2, short y2, uint16_t col) {
 	sortcoords(&x1, &x2);
 	sortcoords(&y1, &y2);

-	for (int i = x1; i <= x2; i++) {
-		for (int j = y1; j <= y2; j++) {
-			if (i >= 0 && i < 127 && j >= 0 && j < 127 &&
-				(i == x1 || i == x2 || j == y1 || j == 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;
 			}
 		}
@@ -81,9 +98,9 @@ void rectfill(short x1, short y1, short x2, short y2, uint16_t col) {
 	sortcoords(&x1, &x2);
 	sortcoords(&y1, &y2);

-	for (int i = x1; i <= x2; i++) {
-		for (int j = y1; j <= y2; j++) {
-			if (i >= 0 && i < 127 && j >= 0 && j < 127) {
+	for (short i = x1; i <= x2; i++) {
+		for (short j = y1; j <= y2; j++) {
+			if (isOnScreen(&i, &j)) {
 				_pico8_fb[(i * 128) + j] = col;
 			}
 		}
diff --git a/source/graphics.h b/source/graphics.h
index a83632a..52195bf 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -30,6 +30,9 @@ typedef struct {

 void cls();

+void pset(short x, short y, uint16_t col);
+uint16_t pget(short x, short y);
+
 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);