commit c0a8ee6

Devine Lu Linvega  ·  2025-01-18 03:46:07 +0000 UTC
parent 9416d4a
(screen.c) Cleanup
4 files changed,  +32, -38
+6, -4
 1@@ -71,8 +71,6 @@
 2 @<draw-portraits> ( -- )
 3 	.position/x LDZ2 .Screen/x DEO2
 4 	.position/y LDZ2 .Screen/y DEO2
 5-	[ LIT2 00 -Screen/auto ] DEO
 6-	#43 .Screen/pixel DEO
 7 	[ LIT2 36 -Screen/auto ] DEO
 8 	.Mouse/state DEI .Controller/button DEI #0f AND ORA ?<draw-box>
 9 	( | top-left )
10@@ -87,8 +85,12 @@
11 	#b140 ORA <draw-portrait>
12 	.Screen/x DEI2k #0008 ADD2 ROT DEO2
13 	.Screen/y DEI2k #0020 ADD2 ROT DEO2
14-	#a140 ORA
15-	( >> )
16+	#a140 ORA <draw-portrait>
17+	.position/x LDZ2 .Screen/x DEO2
18+	.position/y LDZ2 .Screen/y DEO2
19+	[ LIT2 00 -Screen/auto ] DEO
20+	#43 .Screen/pixel DEO
21+	JMP2r
22 
23 @<draw-portrait> ( color -- )
24 	;portrait-chr .Screen/addr DEO2
+14, -18
 1@@ -17,7 +17,7 @@ WITH REGARD TO THIS SOFTWARE.
 2 
 3 UxnScreen uxn_screen;
 4 
 5-#define MAR(x) (x + 0x08)
 6+#define MAR(x) (x + 0x8)
 7 #define MAR2(x) (x + 0x10)
 8 
 9 /* c = !ch ? (color % 5 ? color >> 2 : 0) : color % 4 + ch == 1 ? 0 : (ch - 2 + (color & 3)) % 3 + 1; */
10@@ -29,16 +29,17 @@ static Uint8 blending[4][16] = {
11 	{2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}};
12 
13 int
14-screen_clamp(void)
15+screen_changed(void)
16 {
17-	clampclamp(uxn_screen.x1, 0, uxn_screen.width)
18-	clampclamp(uxn_screen.y1, 0, uxn_screen.height)
19-	clampclamp(uxn_screen.x2, 0, uxn_screen.width)
20-	clampclamp(uxn_screen.y2, 0, uxn_screen.height)
21-	return uxn_screen.x2 > uxn_screen.x1 && uxn_screen.y2 > uxn_screen.y1;
22+	clamp(uxn_screen.x1, 0, uxn_screen.width);
23+	clamp(uxn_screen.y1, 0, uxn_screen.height);
24+	clamp(uxn_screen.x2, 0, uxn_screen.width);
25+	clamp(uxn_screen.y2, 0, uxn_screen.height);
26+	return uxn_screen.x2 > uxn_screen.x1 &&
27+		uxn_screen.y2 > uxn_screen.y1;
28 }
29 
30-void
31+static void
32 screen_change(int x1, int y1, int x2, int y2)
33 {
34 	if(x1 < uxn_screen.x1) uxn_screen.x1 = x1;
35@@ -47,14 +48,6 @@ screen_change(int x1, int y1, int x2, int y2)
36 	if(y2 > uxn_screen.y2) uxn_screen.y2 = y2;
37 }
38 
39-void
40-screen_fill(Uint8 *layer, int color)
41-{
42-	int i, len = MAR2(uxn_screen.width) * uxn_screen.height;
43-	for(i = 0; i < len; i++)
44-		layer[i] = color;
45-}
46-
47 void
48 screen_palette(void)
49 {
50@@ -99,8 +92,11 @@ screen_resize(Uint16 width, Uint16 height, int scale)
51 	uxn_screen.bg = bg, uxn_screen.fg = fg;
52 	uxn_screen.pixels = pixels;
53 	uxn_screen.width = width, uxn_screen.height = height, uxn_screen.scale = scale;
54-	if(dim_change)
55-		screen_fill(uxn_screen.bg, 0), screen_fill(uxn_screen.fg, 0);
56+	if(dim_change) {
57+		int i, len = MAR2(uxn_screen.width) * uxn_screen.height;
58+		for(i = 0; i < len; i++)
59+			uxn_screen.bg[i] = uxn_screen.fg[i] = 0;
60+	}
61 	emu_resize(width, height);
62 	screen_change(0, 0, width + 8, height + 8);
63 }
+2, -4
 1@@ -17,9 +17,7 @@ typedef struct UxnScreen {
 2 
 3 extern UxnScreen uxn_screen;
 4 extern int emu_resize(int width, int height);
 5-int screen_clamp(void);
 6-void screen_change(int x1, int y1, int x2, int y2);
 7-void screen_fill(Uint8 *layer, int color);
 8+int screen_changed(void);
 9 void screen_palette(void);
10 void screen_resize(Uint16 width, Uint16 height, int scale);
11 void screen_redraw();
12@@ -29,7 +27,7 @@ void screen_deo(Uint8 addr);
13 
14 /* clang-format off */
15 
16-#define clampclamp(v,a,b) { if(v < a) v = a; else if(v >= b) v = b; }
17+#define clamp(v,a,b) { if(v < a) v = a; else if(v >= b) v = b; }
18 #define twos(v) (v & 0x8000 ? (int)v - 0x10000 : (int)v)
19 
20 /* clang-format on */
+10, -12
 1@@ -34,16 +34,15 @@ static XImage *ximage;
 2 static Display *display;
 3 static Window window;
 4 
 5+/* clang-format off */
 6+
 7 #define WIDTH (64 * 8)
 8 #define HEIGHT (40 * 8)
 9 #define PAD 2
10 #define CONINBUFSIZE 256
11+#define clamp(v,a,b) { if(v < a) v = a; else if(v >= b) v = b; }
12 
13-static int
14-clamp(int val, int min, int max)
15-{
16-	return (val >= min) ? (val <= max) ? val : max : min;
17-}
18+/* clang-format on */
19 
20 Uint8
21 emu_dei(Uint8 addr)
22@@ -93,8 +92,6 @@ emu_restart(int soft)
23 {
24 	close_console();
25 	screen_resize(WIDTH, HEIGHT, uxn_screen.scale);
26-	screen_fill(uxn_screen.bg, 0);
27-	screen_fill(uxn_screen.fg, 0);
28 	system_reboot(soft);
29 }
30 
31@@ -174,9 +171,10 @@ emu_event(void)
32 	} break;
33 	case MotionNotify: {
34 		XMotionEvent *e = (XMotionEvent *)&ev;
35-		int x = clamp((e->x - PAD) / uxn_screen.scale, 0, uxn_screen.width - 1);
36-		int y = clamp((e->y - PAD) / uxn_screen.scale, 0, uxn_screen.height - 1);
37-		mouse_pos(x, y);
38+		int x = (e->x - PAD) / uxn_screen.scale, y = (e->y - PAD) / uxn_screen.scale;
39+		clamp(x, 0, uxn_screen.width - 1)
40+			clamp(y, 0, uxn_screen.height - 1)
41+				mouse_pos(x, y);
42 	} break;
43 	}
44 }
45@@ -236,11 +234,11 @@ emu_run(void)
46 		while(XPending(display))
47 			emu_event();
48 		if(poll(&fds[1], 1, 0)) {
49-			int x,y,w,h;
50+			int x, y, w, h;
51 			read(fds[1].fd, expirations, 8);
52 			if(uxn_screen.vector)
53 				uxn_eval(uxn_screen.vector);
54-			if(uxn_screen.x2 && uxn_screen.y2 && screen_clamp()) {
55+			if(uxn_screen.x2 && uxn_screen.y2 && screen_changed()) {
56 				x = uxn_screen.x1 * uxn_screen.scale, y = uxn_screen.y1 * uxn_screen.scale;
57 				w = uxn_screen.x2 * uxn_screen.scale - x, h = uxn_screen.y2 * uxn_screen.scale - y;
58 				screen_redraw();