commit 9416d4a

Devine Lu Linvega  ·  2025-01-18 00:09:43 +0000 UTC
parent a3af37b
(Screen.c) Removed redundant variables
4 files changed,  +68, -76
+43, -43
  1@@ -5,6 +5,10 @@
  2 |80 @Controller &vector $2 &button $1 &key $1
  3 |90 @Mouse &vector $2 &x $2 &y $2 &state $1 &pad $3 &scrollx $2 &scrolly $2
  4 
  5+|000
  6+
  7+	@position &x $2 &y $2
  8+
  9 |100
 10 
 11 @on-reset ( -> )
 12@@ -13,46 +17,27 @@
 13 	#2358 .System/b DEO2
 14 	;on-mouse .Mouse/vector DEO2
 15 	;on-button .Controller/vector DEO2
 16-	<redraw>
 17+	<draw-guide>
 18+	#0028 #0028 <move>
 19 	BRK
 20 
 21 @on-button ( -> )
 22-	( | clear foreground )
 23-	#0000 DUP2 .Screen/x DEO2
 24-	.Screen/y DEO2
 25-	#c0 .Screen/pixel DEO
 26-	<draw-guide>
 27 	.Controller/button DEI
 28 	( | handlers )
 29 	DUP #10 AND #00 EQU ?{
 30-		,&y LDR2 #0004 SUB2 ,&y STR2 }
 31+		.position/y LDZ2 #0004 SUB2 .position/y STZ2 }
 32 	DUP #20 AND #00 EQU ?{
 33-		,&y LDR2 #0004 ADD2 ,&y STR2 }
 34+		.position/y LDZ2 #0004 ADD2 .position/y STZ2 }
 35 	DUP #40 AND #00 EQU ?{
 36-		,&x LDR2 #0004 SUB2 ,&x STR2 }
 37+		.position/x LDZ2 #0004 SUB2 .position/x STZ2 }
 38 	DUP #80 AND #00 EQU ?{
 39-		,&x LDR2 #0004 ADD2 ,&x STR2 }
 40+		.position/x LDZ2 #0004 ADD2 .position/x STZ2 }
 41 	DUP #01 NEQ ?{ <toggle-size> }
 42-	POP
 43-	( | draw )
 44-	[ LIT2 &x 0040 ] .Screen/x DEO2
 45-	[ LIT2 &y 0040 ] .Screen/y DEO2
 46-	<draw-portraits>
 47+	POP <draw-portraits>
 48 	BRK
 49 
 50 @on-mouse ( -> )
 51-	( | clear foreground )
 52-	#0000 DUP2 .Screen/x DEO2
 53-	.Screen/y DEO2
 54-	#c0 .Screen/pixel DEO
 55-	<draw-guide>
 56-	( | cursor )
 57-	#41 ;cursor-icn <update-cursor>
 58-	<draw-portraits>
 59-	( | dot )
 60-	.Mouse/x DEI2 .Screen/x DEO2
 61-	.Mouse/y DEI2 .Screen/y DEO2
 62-	#43 .Screen/pixel DEO
 63+	.Mouse/x DEI2 .Mouse/y DEI2 <move>
 64 	BRK
 65 
 66 (
 67@@ -61,23 +46,33 @@
 68 @<toggle-size> ( -- )
 69 	.Screen/width DEI2 #0200 EQU2 ?{
 70 		#0200 .Screen/width DEO2
 71-		#0200 .Screen/height DEO2 !<redraw> }
 72+		#0200 .Screen/height DEO2
 73+		<draw-guide> !<draw-portraits> }
 74 	#0180 .Screen/width DEO2
 75-	#0180 .Screen/height DEO2 !<redraw>
 76-
 77-@<redraw> ( -- )
 78-	( | draw fills )
 79-	#0080 .Screen/x DEO2
 80-	#0080 .Screen/y DEO2
 81-	#82 .Screen/pixel DEO
 82-	#93 .Screen/pixel DEO
 83-	#a1 .Screen/pixel DEO
 84-	( | guide )
 85-	<draw-guide>
 86-	#0028 .Screen/x DEO2
 87-	#0028 .Screen/y DEO2 !<draw-portraits>
 88+	#0180 .Screen/height DEO2
 89+	<draw-guide> !<draw-portraits>
 90+
 91+@<move> ( x* y* -- )
 92+	( | clear )
 93+	.position/x LDZ2 #0020 SUB2 .Screen/x DEO2
 94+	.position/y LDZ2 #0020 SUB2 .Screen/y DEO2
 95+	[ LIT2 71 -Screen/auto ] DEO
 96+	;fill-icn .Screen/addr DEO2
 97+	#f8
 98+	&>l ( -- )
 99+		#40 .Screen/sprite DEO
100+		INC DUP ?&>l
101+	POP
102+	( | update )
103+	.position/y STZ2
104+	.position/x STZ2
105+	( >> )
106 
107 @<draw-portraits> ( -- )
108+	.position/x LDZ2 .Screen/x DEO2
109+	.position/y LDZ2 .Screen/y DEO2
110+	[ LIT2 00 -Screen/auto ] DEO
111+	#43 .Screen/pixel DEO
112 	[ LIT2 36 -Screen/auto ] DEO
113 	.Mouse/state DEI .Controller/button DEI #0f AND ORA ?<draw-box>
114 	( | top-left )
115@@ -92,8 +87,8 @@
116 	#b140 ORA <draw-portrait>
117 	.Screen/x DEI2k #0008 ADD2 ROT DEO2
118 	.Screen/y DEI2k #0020 ADD2 ROT DEO2
119-	#a140 ORA <draw-portrait>
120-	JMP2r
121+	#a140 ORA
122+	( >> )
123 
124 @<draw-portrait> ( color -- )
125 	;portrait-chr .Screen/addr DEO2
126@@ -135,6 +130,11 @@
127 	JMP2r
128 
129 @<draw-guide> ( -- )
130+	#0080 .Screen/x DEO2
131+	#0080 .Screen/y DEO2
132+	#82 .Screen/pixel DEO
133+	#93 .Screen/pixel DEO
134+	#a1 .Screen/pixel DEO
135 	#0008 DUP2 .Screen/x DEO2
136 	.Screen/y DEO2
137 	#e2 .Screen/auto DEO
+13, -27
 1@@ -1,10 +1,11 @@
 2 #include <stdlib.h>
 3+#include <stdio.h>
 4 
 5 #include "../uxn.h"
 6 #include "screen.h"
 7 
 8 /*
 9-Copyright (c) 2021-2024 Devine Lu Linvega, Andrew Alderwick
10+Copyright (c) 2021-2025 Devine Lu Linvega, Andrew Alderwick
11 
12 Permission to use, copy, modify, and distribute this software for any
13 purpose with or without fee is hereby granted, provided that the above
14@@ -28,24 +29,12 @@ static Uint8 blending[4][16] = {
15 	{2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}};
16 
17 int
18-screen_changed(void)
19+screen_clamp(void)
20 {
21-	if(uxn_screen.x1 < 0)
22-		uxn_screen.x1 = 0;
23-	else if(uxn_screen.x1 >= uxn_screen.width)
24-		uxn_screen.x1 = uxn_screen.width;
25-	if(uxn_screen.y1 < 0)
26-		uxn_screen.y1 = 0;
27-	else if(uxn_screen.y1 >= uxn_screen.height)
28-		uxn_screen.y1 = uxn_screen.height;
29-	if(uxn_screen.x2 < 0)
30-		uxn_screen.x2 = 0;
31-	else if(uxn_screen.x2 >= uxn_screen.width)
32-		uxn_screen.x2 = uxn_screen.width;
33-	if(uxn_screen.y2 < 0)
34-		uxn_screen.y2 = 0;
35-	else if(uxn_screen.y2 >= uxn_screen.height)
36-		uxn_screen.y2 = uxn_screen.height;
37+	clampclamp(uxn_screen.x1, 0, uxn_screen.width)
38+	clampclamp(uxn_screen.y1, 0, uxn_screen.height)
39+	clampclamp(uxn_screen.x2, 0, uxn_screen.width)
40+	clampclamp(uxn_screen.y2, 0, uxn_screen.height)
41 	return uxn_screen.x2 > uxn_screen.x1 && uxn_screen.y2 > uxn_screen.y1;
42 }
43 
44@@ -120,25 +109,22 @@ void
45 screen_redraw(void)
46 {
47 	int i, x, y, k, l;
48-	int x1 = uxn_screen.x1 < 0 ? 0 : uxn_screen.x1, y1 = uxn_screen.y1 < 0 ? 0 : uxn_screen.y1;
49-	int x2 = uxn_screen.x2 > uxn_screen.width ? uxn_screen.width : uxn_screen.x2;
50-	int y2 = uxn_screen.y2 > uxn_screen.height ? uxn_screen.height : uxn_screen.y2;
51-	Uint32 palette[16], *pixels = uxn_screen.pixels;
52-	uxn_screen.x1 = uxn_screen.y1 = 9000;
53-	uxn_screen.x2 = uxn_screen.y2 = 0;
54+	Uint32 palette[16];
55 	for(i = 0; i < 16; i++)
56 		palette[i] = uxn_screen.palette[(i >> 2) ? (i >> 2) : (i & 3)];
57-	for(y = y1; y < y2; y++) {
58+	for(y = uxn_screen.y1; y < uxn_screen.y2; y++) {
59 		int ys = y * uxn_screen.scale;
60-		for(x = x1, i = MAR(x) + MAR(y) * MAR2(uxn_screen.width); x < x2; x++, i++) {
61+		for(x = uxn_screen.x1, i = MAR(x) + MAR(y) * MAR2(uxn_screen.width); x < uxn_screen.x2; x++, i++) {
62 			int c = palette[uxn_screen.fg[i] << 2 | uxn_screen.bg[i]];
63 			for(k = 0; k < uxn_screen.scale; k++) {
64 				int oo = ((ys + k) * uxn_screen.width + x) * uxn_screen.scale;
65 				for(l = 0; l < uxn_screen.scale; l++)
66-					pixels[oo + l] = c;
67+					uxn_screen.pixels[oo + l] = c;
68 			}
69 		}
70 	}
71+	uxn_screen.x1 = uxn_screen.y1 = 9999;
72+	uxn_screen.x2 = uxn_screen.y2 = 0;
73 }
74 
75 /* screen registers */
+7, -2
 1@@ -1,5 +1,5 @@
 2 /*
 3-Copyright (c) 2021 Devine Lu Linvega, Andrew Alderwick
 4+Copyright (c) 2021-2025 Devine Lu Linvega, Andrew Alderwick
 5 
 6 Permission to use, copy, modify, and distribute this software for any
 7 purpose with or without fee is hereby granted, provided that the above
 8@@ -17,7 +17,7 @@ typedef struct UxnScreen {
 9 
10 extern UxnScreen uxn_screen;
11 extern int emu_resize(int width, int height);
12-int screen_changed(void);
13+int screen_clamp(void);
14 void screen_change(int x1, int y1, int x2, int y2);
15 void screen_fill(Uint8 *layer, int color);
16 void screen_palette(void);
17@@ -27,4 +27,9 @@ void screen_redraw();
18 Uint8 screen_dei(Uint8 addr);
19 void screen_deo(Uint8 addr);
20 
21+/* clang-format off */
22+
23+#define clampclamp(v,a,b) { if(v < a) v = a; else if(v >= b) v = b; }
24 #define twos(v) (v & 0x8000 ? (int)v - 0x10000 : (int)v)
25+
26+/* clang-format on */
+5, -4
 1@@ -236,12 +236,13 @@ emu_run(void)
 2 		while(XPending(display))
 3 			emu_event();
 4 		if(poll(&fds[1], 1, 0)) {
 5+			int x,y,w,h;
 6 			read(fds[1].fd, expirations, 8);
 7 			if(uxn_screen.vector)
 8 				uxn_eval(uxn_screen.vector);
 9-			if(screen_changed()) {
10-				int x = uxn_screen.x1 * uxn_screen.scale, y = uxn_screen.y1 * uxn_screen.scale;
11-				int w = uxn_screen.x2 * uxn_screen.scale - x, h = uxn_screen.y2 * uxn_screen.scale - y;
12+			if(uxn_screen.x2 && uxn_screen.y2 && screen_clamp()) {
13+				x = uxn_screen.x1 * uxn_screen.scale, y = uxn_screen.y1 * uxn_screen.scale;
14+				w = uxn_screen.x2 * uxn_screen.scale - x, h = uxn_screen.y2 * uxn_screen.scale - y;
15 				screen_redraw();
16 				XPutImage(display, window, DefaultGC(display, 0), ximage, x, y, x + PAD, y + PAD, w, h);
17 			}
18@@ -261,7 +262,7 @@ main(int argc, char **argv)
19 {
20 	int i = 1;
21 	if(argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v')
22-		return !fprintf(stdout, "Uxn11 - Varvara Emulator, 1 Jan 2025.\n");
23+		return !fprintf(stdout, "Uxn11 - Varvara Emulator, 17 Jan 2025.\n");
24 	else if(argc == 1)
25 		return !fprintf(stdout, "usage: %s [-v] file.rom [args..]\n", argv[0]);
26 	else if(!display_init())