From e52db282997db379347167becc495fc856542e63 Mon Sep 17 00:00:00 2001
From: neauoire <aliceffekt@gmail.com>
Date: Sat, 11 Nov 2023 20:59:08 -0800
Subject: [PATCH] (Screen) Fill function

---
 src/devices/screen.c | 50 +++++++++++++++++++++++---------------------
 src/devices/screen.h | 12 +++++++----
 src/uxn11.c          |  4 ++--
 3 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/src/devices/screen.c b/src/devices/screen.c
index 38a1c84..98afb02 100644
--- a/src/devices/screen.c
+++ b/src/devices/screen.c
@@ -39,7 +39,15 @@ screen_change(Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2)
 }
 
 void
-screen_fill(Uint8 *layer, int x1, int y1, int x2, int y2, int color)
+screen_fill(Uint8 *layer, int color)
+{
+	int i, length = uxn_screen.width * uxn_screen.height;
+	for(i = 0; i < length; i++)
+		layer[i] = color;
+}
+
+void
+screen_rect(Uint8 *layer, int x1, int y1, int x2, int y2, int color)
 {
 	int x, y, width = uxn_screen.width, height = uxn_screen.height;
 	for(y = y1; y < y2 && y < height; y++)
@@ -96,16 +104,16 @@ screen_debugger(Uxn *u)
 	int i;
 	for(i = 0; i < 0x08; i++) {
 		Uint8 pos = u->wst.ptr - 4 + i;
-		Uint8 color = i > 4 ? 0x01 : !pos ? 0xc :
-			i == 4                        ? 0x8 :
-                                            0x2;
+		Uint8 color = i > 4 ? 0x01 : !pos ? 0xc
+			: i == 4                      ? 0x8
+										  : 0x2;
 		draw_byte(u->wst.dat[pos], i * 0x18 + 0x8, uxn_screen.height - 0x18, color);
 	}
 	for(i = 0; i < 0x08; i++) {
 		Uint8 pos = u->rst.ptr - 4 + i;
-		Uint8 color = i > 4 ? 0x01 : !pos ? 0xc :
-			i == 4                        ? 0x8 :
-                                            0x2;
+		Uint8 color = i > 4 ? 0x01 : !pos ? 0xc
+			: i == 4                      ? 0x8
+										  : 0x2;
 		draw_byte(u->rst.dat[pos], i * 0x18 + 0x8, uxn_screen.height - 0x10, color);
 	}
 	screen_blit(uxn_screen.fg, arrow, 0, 0x68, uxn_screen.height - 0x20, 3, 0, 0, 0);
@@ -137,24 +145,18 @@ screen_resize(Uint16 width, Uint16 height)
 		return;
 	if(uxn_screen.width == width && uxn_screen.height == height)
 		return;
-	bg = malloc(width * height),
-	fg = malloc(width * height);
+	bg = malloc(width * height), fg = malloc(width * height);
 	if(bg && fg)
 		pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32));
 	if(!bg || !fg || !pixels) {
-		free(bg);
-		free(fg);
+		free(bg), free(fg);
 		return;
 	}
-	free(uxn_screen.bg);
-	free(uxn_screen.fg);
-	uxn_screen.bg = bg;
-	uxn_screen.fg = fg;
+	free(uxn_screen.bg), free(uxn_screen.fg);
+	uxn_screen.bg = bg, uxn_screen.fg = fg;
 	uxn_screen.pixels = pixels;
-	uxn_screen.width = width;
-	uxn_screen.height = height;
-	screen_fill(uxn_screen.bg, 0, 0, width, height, 0);
-	screen_fill(uxn_screen.fg, 0, 0, width, height, 0);
+	uxn_screen.width = width, uxn_screen.height = height;
+	screen_fill(uxn_screen.bg, 0), screen_fill(uxn_screen.fg, 0);
 	emu_resize(width, height);
 	screen_change(0, 0, width, height);
 }
@@ -194,13 +196,13 @@ screen_dei(Uxn *u, Uint8 addr)
 void
 screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
 {
-	Uint8 *port_width, *port_height, *port_x, *port_y, *port_addr;
+	Uint8 *port_height, *port_x, *port_y, *port_addr;
 	Uint16 x, y, dx, dy, dxy, dyx, addr, addr_incr;
 	switch(port) {
-	case 0x3:
-		port_width = d + 0x2;
+	case 0x3: {
+		Uint8 *port_width = d + 0x2;
 		screen_resize(PEEK2(port_width), uxn_screen.height);
-		break;
+	} break;
 	case 0x5:
 		port_height = d + 0x4;
 		screen_resize(uxn_screen.width, PEEK2(port_height));
@@ -219,7 +221,7 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
 			Uint16 y2 = uxn_screen.height;
 			if(ctrl & 0x10) x2 = x, x = 0;
 			if(ctrl & 0x20) y2 = y, y = 0;
-			screen_fill(layer, x, y, x2, y2, color);
+			screen_rect(layer, x, y, x2, y2, color);
 			screen_change(x, y, x2, y2);
 		}
 		/* pixel mode */
diff --git a/src/devices/screen.h b/src/devices/screen.h
index 167c634..4bf1981 100644
--- a/src/devices/screen.h
+++ b/src/devices/screen.h
@@ -10,6 +10,8 @@ WITH REGARD TO THIS SOFTWARE.
 */
 
 #define SCREEN_VERSION 1
+#define SCREEN_DEIMASK 0x003c
+#define SCREEN_DEOMASK 0xc028
 
 typedef struct UxnScreen {
 	int width, height, x1, y1, x2, y2;
@@ -17,13 +19,15 @@ typedef struct UxnScreen {
 	Uint8 *fg, *bg;
 } UxnScreen;
 
-void screen_fill(Uint8 *layer, int x1, int y1, int x2, int y2, int color);
+extern UxnScreen uxn_screen;
+extern int emu_resize(int width, int height);
+
+void screen_fill(Uint8 *layer, int color);
+void screen_rect(Uint8 *layer, int x1, int y1, int x2, int y2, int color);
 void screen_palette(Uint8 *addr);
 void screen_resize(Uint16 width, Uint16 height);
 void screen_change(Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2);
 void screen_redraw(Uxn *u);
+
 Uint8 screen_dei(Uxn *u, Uint8 addr);
 void screen_deo(Uint8 *ram, Uint8 *d, Uint8 port);
-
-extern UxnScreen uxn_screen;
-extern int emu_resize(int width, int height);
\ No newline at end of file
diff --git a/src/uxn11.c b/src/uxn11.c
index c00d722..0550ca1 100644
--- a/src/uxn11.c
+++ b/src/uxn11.c
@@ -86,8 +86,8 @@ static void
 emu_restart(Uxn *u, char *rom, int soft)
 {
 	screen_resize(WIDTH, HEIGHT);
-	screen_fill(uxn_screen.bg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
-	screen_fill(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
+	screen_rect(uxn_screen.bg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
+	screen_rect(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
 	system_reboot(u, rom, soft);
 	/* set window title */
 }