Browse code

(mouse.tal) Improved implementation

neauoire authored on 09/11/2023 22:01:11
Showing 4 changed files
... ...
@@ -1,5 +1,4 @@
1
-( Mouse:
2
-	Paint with 3 colors with each mouse button. )
1
+( Mouse: Paint with 3 colors with each mouse button. )
3 2
 
4 3
 |00 @System &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 &debug $1 &halt $1
5 4
 |20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
... ...
@@ -7,214 +6,181 @@
7 6
 
8 7
 |0000
9 8
 
10
-	@line &x2 $2 &y2 $2
11 9
 	@length $2
12 10
 	@frame $2
13 11
 	@pen &x $2 &y $2 &x2 $2 &y2 $2
14 12
 	@pointer &x $2 &y $2 &lastx $2 &lasty $2 &state $1
15 13
 
16
-|0100 ( -> )
14
+|0100
17 15
 
18
-	( theme )
19
-	#4cfd .System/r DEO2
20
-	#4cf3 .System/g DEO2
21
-	#dcf2 .System/b DEO2
22
-
23
-	( vectors )
16
+@on-reset ( -> )
17
+	( | theme )
18
+	#68af .System/r DEO2
19
+	#79bf .System/g DEO2
20
+	#8ace .System/b DEO2
21
+	( | vectors )
24 22
 	;on-mouse .Mouse/vector DEO2
25 23
 	;on-frame .Screen/vector DEO2
26
-
27
-	draw-mouse
28
-
29
-BRK
24
+	<draw-mouse>
25
+	BRK
30 26
 
31 27
 (
32 28
 @|vectors )
33 29
 
34 30
 @on-frame ( -> )
35
-
36
-	.Mouse/state DEI ?&skip
37
-		;run DUP2 JSR2 JSR2
38
-		&skip
39
-
40
-BRK
31
+	.Mouse/state DEI ?{
32
+		;run DUP2 JSR2 JSR2 }
33
+	BRK
41 34
 
42 35
 @on-mouse ( -> )
43
-
44
-	( clear last cursor )
36
+	( | clear last cursor )
45 37
 	.pointer/x LDZ2 .Screen/x DEO2
46 38
 	.pointer/y LDZ2 .Screen/y DEO2
47 39
 	;fill-icn .Screen/addr DEO2
48 40
 	[ LIT2 40 -Screen/sprite ] DEO
49
-	draw-mouse
50
-	( draw new cursor )
41
+	<draw-mouse>
42
+	( | draw new cursor )
51 43
 	;pointer-icn .Screen/addr DEO2
52 44
 	#00 .Screen/auto DEO
53
-	.Mouse/x DEI2 DUP2 .pointer/x STZ2 .Screen/x DEO2
54
-	.Mouse/y DEI2 DUP2 .pointer/y STZ2 .Screen/y DEO2
45
+	.Mouse/x DEI2 DUP2 .pointer/x STZ2
46
+	.Screen/x DEO2
47
+	.Mouse/y DEI2 DUP2 .pointer/y STZ2
48
+	.Screen/y DEO2
55 49
 	#45 .Mouse/state DEI #00 NEQ #05 MUL ADD .Screen/sprite DEO
56
-	( on down )
57
-	.Mouse/state DEI #00 NEQ .pointer/state LDZ #00 EQU AND ?on-mouse-down
58
-	( on drag )
59
-	.Mouse/state DEI ?on-mouse-drag
50
+	( | on down )
51
+	.Mouse/state DEI #00 NEQ .pointer/state LDZ #00 EQU AND ?&down
52
+	( | on drag )
53
+	.Mouse/state DEI ?&drag
60 54
 	.Mouse/state DEI .pointer/state STZ
61
-
62
-BRK
63
-
64
-@on-mouse-down ( -> )
65
-
66
-	#0000 DUP2 .length STZ2 .frame STZ2
67
-	clear-screen
68
-	( record start position )
69
-	.Mouse/x DEI2 DUP2 .pointer/x STZ2 .pointer/lastx STZ2
70
-	.Mouse/y DEI2 DUP2 .pointer/y STZ2 .pointer/lasty STZ2
71
-	.Mouse/state DEI .pointer/state STZ
72
-
73
-BRK
74
-
75
-@on-mouse-drag ( -> )
76
-
77
-	( record )
78
-	;stroke .length LDZ2 #20 SFT2 ADD2 STH2
79
-		.pointer/x LDZ2 .pointer/lastx LDZ2 SUB2 STH2kr STA2
55
+	BRK
56
+	&down ( -> )
57
+		#0000 DUP2 .length STZ2
58
+		.frame STZ2
59
+		<clear-screen>
60
+		( | record start position )
61
+		.Mouse/x DEI2 DUP2 .pointer/x STZ2
62
+		.pointer/lastx STZ2
63
+		.Mouse/y DEI2 DUP2 .pointer/y STZ2
64
+		.pointer/lasty STZ2
65
+		.Mouse/state DEI .pointer/state STZ
66
+		BRK
67
+	&drag ( -> )
68
+		( | record )
69
+		;stroke .length LDZ2 #20 SFT2 ADD2 STH2 .pointer/x LDZ2 .pointer/lastx LDZ2 SUB2 STH2kr STA2
80 70
 		.pointer/y LDZ2 .pointer/lasty LDZ2 SUB2 STH2r INC2 INC2 STA2
81
-	( move ptr )
82
-	.length LDZ2 INC2 .length STZ2
83
-	( draw line )
84
-	.pointer/lastx LDZ2
85
-	.pointer/lasty LDZ2
86
-	.pointer/x LDZ2
87
-	.pointer/y LDZ2
88
-		#01 draw-line
89
-	( record last position )
90
-	.Mouse/x DEI2
91
-		DUP2 .pointer/lastx STZ2
71
+		( | move ptr )
72
+		.length LDZ2 INC2 .length STZ2
73
+		( | draw line )
74
+		.pointer/lastx LDZ2 .pointer/lasty LDZ2 .pointer/x LDZ2 .pointer/y LDZ2 #01 <draw-line>
75
+		( | record last position )
76
+		.Mouse/x DEI2 DUP2 .pointer/lastx STZ2
92 77
 		DUP2 .pen/x STZ2
93 78
 		.pen/x2 STZ2
94
-	.Mouse/y DEI2
95
-		DUP2 .pointer/lasty STZ2
79
+		.Mouse/y DEI2 DUP2 .pointer/lasty STZ2
96 80
 		DUP2 .pen/y STZ2
97 81
 		.pen/y2 STZ2
98
-	.Mouse/state DEI
99
-		DUP #01 NEQ INC ;run/color STA
82
+		.Mouse/state DEI DUP #01 NEQ INC ;run/color STA
100 83
 		.pointer/state STZ
101
-
102
-BRK
84
+		BRK
103 85
 
104 86
 (
105 87
 @|main )
106 88
 
107 89
 @run ( -- )
108
-
109
-	( read )
110
-	;stroke .frame LDZ2 #20 SFT2 ADD2 STH2
111
-		.pen/x LDZ2 STH2kr LDA2 ADD2 .pen/x STZ2
112
-		.pen/y LDZ2 STH2r INC2 INC2 LDA2 ADD2 .pen/y STZ2
113
-	( line )
114
-	.pen/x LDZ2 .pen/y LDZ2
115
-	.pen/x2 LDZ2 .pen/y2 LDZ2
116
-		.frame LDZ2 #01 SFT2 NIP #01 AND [ LIT &color $1 ] ADD INC draw-line
117
-	( history )
90
+	( | read )
91
+	;stroke .frame LDZ2 #20 SFT2 ADD2 STH2 .pen/x LDZ2 STH2kr LDA2 ADD2 .pen/x STZ2
92
+	.pen/y LDZ2 STH2r INC2 INC2 LDA2 ADD2 .pen/y STZ2
93
+	( | line )
94
+	.pen/x LDZ2 .pen/y LDZ2 .pen/x2 LDZ2 .pen/y2 LDZ2 .frame LDZ2 #01 SFT2 NIP #01 AND [ LIT &color $1 ] ADD INC <draw-line>
95
+	( | history )
118 96
 	.pen/x LDZ2 .pen/x2 STZ2
119 97
 	.pen/y LDZ2 .pen/y2 STZ2
120
-	( incr frame )
121
-	.frame LDZ2 INC2 .length LDZ2 INC2 ( mod2 ) DIV2k MUL2 SUB2 .frame STZ2
122
-
123
-JMP2r
124
-
125
-@draw-mouse ( -- )
126
-
127
-	( clear )
128
-	#0010
129
-		DUP2 .Screen/x DEO2
130
-		.Screen/y DEO2
98
+	( | incr frame )
99
+	.frame LDZ2 INC2 .length LDZ2 INC2
100
+	( mod2 ) DIV2k MUL2 SUB2 .frame STZ2
101
+	JMP2r
102
+
103
+@<draw-mouse> ( -- )
104
+	( | clear )
105
+	#0010 DUP2 .Screen/x DEO2
106
+	.Screen/y DEO2
131 107
 	#16 .Screen/auto DEO
132 108
 	;fill-icn .Screen/addr DEO2
133
-	#40 .Screen/sprite DEOk DEO
134
-	( buttons )
109
+	#40 .Screen/sprite DEOk
110
+	DEO
111
+	( | buttons )
135 112
 	#0300
136
-	&l
137
-		#01 OVR #40 SFT SFT .Mouse/state DEI AND #00 EQU ?&no-draw
113
+	&l ( -- )
114
+		#01 OVR #40 SFT SFT .Mouse/state DEI AND #00 EQU ?{
138 115
 			#0010 .Screen/y DEO2
139 116
 			#00 OVR #40 SFT2 ;button-icn ADD2 .Screen/addr DEO2
140
-			#45 .Screen/sprite DEO
141
-			&no-draw
117
+			#45 .Screen/sprite DEO }
142 118
 		INC GTHk ?&l
143 119
 	POP2
144
-	( outline )
120
+	( | outline )
145 121
 	#0010 .Screen/y DEO2
146 122
 	;mouse-icn .Screen/addr DEO2
147 123
 	#16 .Screen/auto DEO
148
-	#4a .Screen/sprite DEOk DEO
124
+	#4a .Screen/sprite DEOk
125
+	DEO
126
+	JMP2r
149 127
 
150
-JMP2r
151
-
152
-@draw-line ( x1* y1* x2* y2* color -- )
153
-
154
-	( load )
128
+@<draw-line> ( x1* y1* x2* y2* color -- )
155 129
 	,&color STR
156 130
 	,&y STR2
157 131
 	,&x STR2
158
-	.line/y2 STZ2
159
-	.line/x2 STZ2
160
-
161
-	,&x LDR2 .line/x2 LDZ2 SUB2 abs2 ,&dx STR2
162
-	#0000 ,&y LDR2 .line/y2 LDZ2 SUB2 abs2 SUB2 ,&dy STR2
163
-
164
-	#ffff #00 .line/x2 LDZ2 ,&x LDR2 lts2 DUP2 ADD2 ADD2 ,&sx STR2
165
-	#ffff #00 .line/y2 LDZ2 ,&y LDR2 lts2 DUP2 ADD2 ADD2 ,&sy STR2
166
-
167
-	[ LIT2 &dx $2 ] [ LIT2 &dy $2 ] ADD2 ,&e1 STR2
168
-
169
-	&loop
170
-		.line/x2 LDZ2 DUP2 .Screen/x DEO2 [ LIT2 &x $2 ] EQU2
171
-		.line/y2 LDZ2 DUP2 .Screen/y DEO2 [ LIT2 &y $2 ] EQU2
172
-			[ LIT2 &color $1 -Screen/pixel ] DEO
173
-			AND ?&end
174
-		[ LIT2 &e1 $2 ] DUP2 ADD2 DUP2
175
-		,&dy LDR2 lts2 ?&skipy
176
-			,&e1 LDR2 ,&dy LDR2 ADD2 ,&e1 STR2
177
-			.line/x2 LDZ2 [ LIT2 &sx $2 ] ADD2 .line/x2 STZ2
178
-		&skipy
179
-		,&dx LDR2 gts2 ?&skipx
180
-			,&e1 LDR2 ,&dx LDR2 ADD2 ,&e1 STR2
181
-			.line/y2 LDZ2 [ LIT2 &sy $2 ] ADD2 .line/y2 STZ2
182
-		&skipx
183
-		!&loop
184
-	&end
185
-
186
-JMP2r
187
-
188
-@abs2 DUP2 #0f SFT2 EQU #05 JCN #0000 SWP2 SUB2 JMP2r
189
-@lts2 #8000 STH2k ADD2 SWP2 STH2r ADD2 GTH2 JMP2r
190
-@gts2 #8000 STH2k ADD2 SWP2 STH2r ADD2 LTH2 JMP2r
191
-
192
-@clear-screen ( -- )
193
-
194
-	#0000 DUP2 .Screen/x DEO2 .Screen/y DEO2
132
+	,&y2 STR2
133
+	,&x2 STR2
134
+	,&x LDR2 ,&x2 LDR2 SUB2 abs2 ,&dx STR2
135
+	#0000 ,&y LDR2 ,&y2 LDR2 SUB2 abs2 SUB2 ,&dy STR2
136
+	#ffff [ LIT2 00 _&x2 ] LDR2 ,&x LDR2 lts2 DUP2 ADD2 ADD2 ,&sx STR2
137
+	#ffff [ LIT2 00 _&y2 ] LDR2 ,&y LDR2 lts2 DUP2 ADD2 ADD2 ,&sy STR2
138
+	[ LIT2 &dx $2 ] [ LIT2 &dy $2 ] ADD2 STH2
139
+	&while ( -- )
140
+		[ LIT2 &x2 $2 ] DUP2 .Screen/x DEO2
141
+		[ LIT2 &x $2 ] EQU2 [ LIT2 &y2 $2 ] DUP2 .Screen/y DEO2
142
+		[ LIT2 &y $2 ] EQU2 [ LIT2 &color $1 -Screen/pixel ] DEO
143
+		AND ?&end
144
+		STH2kr DUP2 ADD2 DUP2 ,&dy LDR2 lts2 ?&skipy
145
+		STH2r ,&dy LDR2 ADD2 STH2 ,&x2 LDR2 [ LIT2 &sx $2 ] ADD2 ,&x2 STR2
146
+	&skipy ( -- )
147
+		,&dx LDR2 gts2 ?&while
148
+	STH2r ,&dx LDR2 ADD2 STH2 ,&y2 LDR2 [ LIT2 &sy $2 ] ADD2 ,&y2 STR2
149
+	!&while
150
+	&end POP2r JMP2r
151
+
152
+@abs2 ( a* -- f )
153
+	DUP2 #0f SFT2 EQU ?{ #0000 SWP2 SUB2 }
154
+	JMP2r
155
+
156
+@lts2 ( a* b* -- f )
157
+	#8000 STH2k ADD2 SWP2 STH2r ADD2 GTH2 JMP2r
158
+
159
+@gts2 ( a* b* -- f )
160
+	#8000 STH2k ADD2 SWP2 STH2r ADD2 LTH2 JMP2r
161
+
162
+@<clear-screen> ( -- )
163
+	#0000 DUP2 .Screen/x DEO2
164
+	.Screen/y DEO2
195 165
 	#80 .Screen/pixel DEO
166
+	JMP2r
167
+
168
+@fill-icn [ ffff ffff ffff ffff ]
196 169
 
197
-JMP2r
170
+@pointer-icn [ 80c0 e0f0 f8e0 1000 ]
198 171
 
199
-@fill-icn [
200
-	ffff ffff ffff ffff ]
201
-@pointer-icn [
202
-	80c0 e0f0 f8e0 1000 ]
203 172
 @mouse-icn [
204
-	000d 1212 1212 121d
205
-	00b0 4848 4848 48b8
206
-	1010 1010 1008 0700
207
-	0808 0808 0810 e000 ]
173
+	000d 1212 1212 121d 00b0 4848 4848 48b8
174
+	1010 1010 1008 0700 0808 0808 0810 e000 ]
175
+
208 176
 @button-icn [
209
-	000c 1e1e 1e1e 1e0c
210
-	0000 0000 0000 0000
211
-	0001 0303 0303 0301
212
-	0080 c0c0 c0c0 c080
213
-	0000 0000 0000 0000
214
-	0030 7878 7878 7830 ]
177
+	000c 1e1e 1e1e 1e0c 0000 0000 0000 0000
178
+	0001 0303 0303 0301 0080 c0c0 c0c0 c080
179
+	0000 0000 0000 0000 0030 7878 7878 7830 ]
215 180
 
216 181
 (
217 182
 @|memory )
218 183
 
219 184
 @stroke
220 185
 
186
+
... ...
@@ -53,8 +53,7 @@ uxn_eval(Uxn *u, Uint16 pc)
53 53
 			case 0x60: /* JSI  */             SHIFT( 2) rr = ram + pc; pc += 2; T2_(pc); pc += PEEK2(rr); break;
54 54
 			case 0x80: /* LIT  */ case 0xc0:  SHIFT( 1) T = ram[pc++]; break;
55 55
 			case 0xa0: /* LIT2 */ case 0xe0:  SHIFT( 2) N = ram[pc++]; T = ram[pc++]; break;
56
-			}
57
-			break;
56
+			} break;
58 57
 		/* ALU */
59 58
 		case 0x01: /* INC  */ t=T;            SET(1, 0) T = t + 1; break;
60 59
 		case 0x21: /* INC2 */ t=T2;           SET(2, 0) T2_(t + 1) break;
... ...
@@ -72,7 +72,7 @@ main(int argc, char **argv)
72 72
 		return system_error("usage", "uxncli [-v] file.rom [args..]");
73 73
 	/* Read flags */
74 74
 	if(argv[i][0] == '-' && argv[i][1] == 'v')
75
-		return system_version("Uxncli - Console Varvara Emulator", "1 Nov 2023");
75
+		return system_version("Uxncli - Console Varvara Emulator", "9 Nov 2023");
76 76
 	if(!system_init(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++]))
77 77
 		return system_error("Init", "Failed to initialize uxn.");
78 78
 	/* Game Loop */
... ...
@@ -540,7 +540,7 @@ main(int argc, char **argv)
540 540
 	/* Read flag. Right now, there can be only one. */
541 541
 	if(argv[i][0] == '-') {
542 542
 		if(argv[i][1] == 'v')
543
-			return system_version("Uxnemu - Graphical Varvara Emulator", "1 Nov 2023");
543
+			return system_version("Uxnemu - Graphical Varvara Emulator", "9 Nov 2023");
544 544
 		if(argv[i][1] == '-')
545 545
 			i++;
546 546
 		if(strcmp(argv[i], "-2x") == 0 || strcmp(argv[i], "-3x") == 0)