commit 84429bf
Devine Lu Linvega
·
2025-04-04 16:06:02 +0000 UTC
parent 94e041e
Optimized mouse example
1 files changed,
+124,
-167
+124,
-167
1@@ -1,11 +1,10 @@
2-( Mouse:
3- Paint with 3 colors with each mouse button. )
4+( Mouse: Paint with 3 colors with each mouse button. )
5
6 |00 @System &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 &debug $1 &halt $1
7 |20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
8 |90 @Mouse &vector $2 &x $2 &y $2 &state $1 &pad $3 &modx $2 &mody $2
9
10-|0000
11+|000
12
13 @line &x2 $2 &y2 $2
14 @length $2
15@@ -13,227 +12,185 @@
16 @pen &x $2 &y $2 &x2 $2 &y2 $2
17 @pointer &x $2 &y $2 &lastx $2 &lasty $2 &state $1
18
19-|0100 ( -> )
20+|100
21+
22+@on-reset ( -> )
23 ;meta #06 DEO2
24 #0200 .Screen/height DEO2
25- ( theme )
26- #0fa8 .System/r DEO2
27+ ( theme ) #0fa8 .System/r DEO2
28 #0f4f .System/g DEO2
29 #0fc2 .System/b DEO2
30- #abcd
31-
32- ( vectors )
33- ;on-mouse .Mouse/vector DEO2
34+ ( vectors ) ;on-mouse .Mouse/vector DEO2
35 ;on-frame .Screen/vector DEO2
36+ <draw-mouse>
37+ BRK
38
39- draw-mouse
40-
41-BRK
42-
43-@meta 00 &body
44- ( name ) "Noodle 0a
45+@meta 00
46+ &body ( name )
47+ "Noodle 0a
48 ( details ) "A 20 "Drawing 20 "Program 0a
49 ( author ) "By 20 "Hundred 20 "Rabbits 0a
50- ( date ) "Aug 20 "19, 20 "2024 00
51- 02
52- ( icon ) 83 =appicon
53- ( mask ) 41 1705
54+ ( date ) "4 20 "Apr 20 "2025 00 02
55+ ( icon ) 83 =appicon
56+ ( mask ) 41 1705
57
58 (
59 @|vectors )
60
61 @on-frame ( -> )
62-
63- .Mouse/state DEI ?&skip
64- ;run DUP2 JSR2 JSR2
65- &skip
66-
67-BRK
68+ .Mouse/state DEI ?{ ;run DUP2 JSR2 JSR2 }
69+ BRK
70
71 @on-mouse ( -> )
72-
73- ( clear last cursor )
74+ ( | clear last cursor )
75 .pointer/x LDZ2 .Screen/x DEO2
76 .pointer/y LDZ2 .Screen/y DEO2
77 ;fill-icn .Screen/addr DEO2
78 [ LIT2 40 -Screen/sprite ] DEO
79- draw-mouse
80- ( draw new cursor )
81+ <draw-mouse>
82+ ( | draw new cursor )
83 ;pointer-icn .Screen/addr DEO2
84 #00 .Screen/auto DEO
85- .Mouse/x DEI2 DUP2 .pointer/x STZ2 .Screen/x DEO2
86- .Mouse/y DEI2 DUP2 .pointer/y STZ2 .Screen/y DEO2
87+ .Mouse/x DEI2 DUP2 .pointer/x STZ2
88+ .Screen/x DEO2
89+ .Mouse/y DEI2 DUP2 .pointer/y STZ2
90+ .Screen/y DEO2
91 #45 .Mouse/state DEI #00 NEQ #05 MUL ADD .Screen/sprite DEO
92- ( on down )
93- .Mouse/state DEI #00 NEQ .pointer/state LDZ #00 EQU AND ?on-mouse-down
94- ( on drag )
95- .Mouse/state DEI ?on-mouse-drag
96+ ( on down ) .Mouse/state DEI #00 NEQ .pointer/state LDZ #00 EQU AND ?on-mouse-down
97+ ( on drag ) .Mouse/state DEI ?on-mouse-drag
98 .Mouse/state DEI .pointer/state STZ
99-
100-BRK
101+ BRK
102
103 @on-mouse-down ( -> )
104-
105- #0000 DUP2 .length STZ2 .frame STZ2
106- clear-screen
107- ( record start position )
108- .Mouse/x DEI2 DUP2 .pointer/x STZ2 .pointer/lastx STZ2
109- .Mouse/y DEI2 DUP2 .pointer/y STZ2 .pointer/lasty STZ2
110+ #0000 DUP2 .length STZ2
111+ .frame STZ2
112+ <clear-screen>
113+ ( record start position ) .Mouse/x DEI2 DUP2 .pointer/x STZ2
114+ .pointer/lastx STZ2
115+ .Mouse/y DEI2 DUP2 .pointer/y STZ2
116+ .pointer/lasty STZ2
117 .Mouse/state DEI .pointer/state STZ
118-
119-BRK
120+ BRK
121
122 @on-mouse-drag ( -> )
123-
124- ( record )
125- ;stroke .length LDZ2 #20 SFT2 ADD2 STH2
126- .pointer/x LDZ2 .pointer/lastx LDZ2 SUB2 STH2kr STA2
127- .pointer/y LDZ2 .pointer/lasty LDZ2 SUB2 STH2r INC2 INC2 STA2
128- ( move ptr )
129- .length LDZ2 INC2 .length STZ2
130- ( draw line )
131- .pointer/lastx LDZ2
132- .pointer/lasty LDZ2
133- .pointer/x LDZ2
134- .pointer/y LDZ2
135- #01 draw-line
136- ( record last position )
137- .Mouse/x DEI2
138- DUP2 .pointer/lastx STZ2
139- DUP2 .pen/x STZ2
140- .pen/x2 STZ2
141- .Mouse/y DEI2
142- DUP2 .pointer/lasty STZ2
143- DUP2 .pen/y STZ2
144- .pen/y2 STZ2
145- .Mouse/state DEI
146- DUP #01 NEQ INC ;run/color STA
147- .pointer/state STZ
148-
149-BRK
150+ ( record ) ;stroke .length LDZ2 #20 SFT2 ADD2 STH2
151+ .pointer/x LDZ2 .pointer/lastx LDZ2 SUB2 STH2kr STA2
152+ .pointer/y LDZ2 .pointer/lasty LDZ2 SUB2 STH2r INC2 INC2 STA2
153+ ( move ptr ) .length LDZ2 INC2 .length STZ2
154+ ( draw line ) .pointer/lastx LDZ2 .pointer/lasty LDZ2 .pointer/x LDZ2 .pointer/y LDZ2 #0a <draw-line>
155+ ( record last position ) .Mouse/x DEI2 DUP2 .pointer/lastx STZ2
156+ DUP2 .pen/x STZ2
157+ .pen/x2 STZ2
158+ .Mouse/y DEI2 DUP2 .pointer/lasty STZ2
159+ DUP2 .pen/y STZ2
160+ .pen/y2 STZ2
161+ .Mouse/state DEI DUP #01 NEQ INC ;run/color STA
162+ .pointer/state STZ
163+ BRK
164
165 (
166 @|main )
167
168 @run ( -- )
169-
170- ( read )
171- ;stroke .frame LDZ2 #20 SFT2 ADD2 STH2
172- .pen/x LDZ2 STH2kr LDA2 ADD2 .pen/x STZ2
173- .pen/y LDZ2 STH2r INC2 INC2 LDA2 ADD2 .pen/y STZ2
174- ( line )
175- .pen/x LDZ2 .pen/y LDZ2
176- .pen/x2 LDZ2 .pen/y2 LDZ2
177- .frame LDZ2 #01 SFT2 NIP #01 AND [ LIT &color $1 ] ADD INC draw-line
178- ( history )
179- .pen/x LDZ2 .pen/x2 STZ2
180+ ( read ) ;stroke .frame LDZ2 #20 SFT2 ADD2 STH2
181+ .pen/x LDZ2 STH2kr LDA2 ADD2 .pen/x STZ2
182+ .pen/y LDZ2 STH2r INC2 INC2 LDA2 ADD2 .pen/y STZ2
183+ ( line ) .pen/x LDZ2 .pen/y LDZ2 .pen/x2 LDZ2 .pen/y2 LDZ2 .frame LDZ2 #01 SFT2 NIP #01 AND [ LIT &color $1 ] ADD INC #05 MUL <draw-line>
184+ ( history ) .pen/x LDZ2 .pen/x2 STZ2
185 .pen/y LDZ2 .pen/y2 STZ2
186- ( incr frame )
187- .frame LDZ2 INC2 .length LDZ2 INC2 ( mod2 ) DIV2k MUL2 SUB2 .frame STZ2
188-
189-JMP2r
190-
191-@draw-mouse ( -- )
192-
193- ( clear )
194- #0010
195- DUP2 .Screen/x DEO2
196- .Screen/y DEO2
197- #16 .Screen/auto DEO
198+ ( incr frame ) .frame LDZ2 INC2 .length LDZ2 INC2
199+ ( mod2 ) DIV2k MUL2 SUB2 .frame STZ2
200+ JMP2r
201+
202+@<draw-mouse> ( -- )
203+ ( clear ) #0010 DUP2 .Screen/x DEO2
204+ .Screen/y DEO2
205+ [ LIT2 16 -Screen/auto ] DEO
206 ;fill-icn .Screen/addr DEO2
207- #40 .Screen/sprite DEOk DEO
208- ( buttons )
209- #0300
210- &l
211- #01 OVR #40 SFT SFT .Mouse/state DEI AND #00 EQU ?&no-draw
212+ [ LIT2 40 -Screen/sprite ] DEOk DEO
213+ ( buttons ) #0300
214+ &>l ( -- )
215+ #01 OVR #40 SFT SFT .Mouse/state DEI AND #00 EQU ?{
216 #0010 .Screen/y DEO2
217 #00 OVR #40 SFT2 ;button-icn ADD2 .Screen/addr DEO2
218- #45 .Screen/sprite DEO
219- &no-draw
220- INC GTHk ?&l
221+ [ LIT2 45 -Screen/sprite ] DEO }
222+ INC GTHk ?&>l
223 POP2
224- ( outline )
225- #0010 .Screen/y DEO2
226+ ( outline ) #0010 .Screen/y DEO2
227 ;mouse-icn .Screen/addr DEO2
228- #16 .Screen/auto DEO
229- #4a .Screen/sprite DEOk DEO
230+ [ LIT2 16 -Screen/auto ] DEO
231+ [ LIT2 4a -Screen/sprite ] DEOk DEO
232+ JMP2r
233+
234+%abs2 {
235+ DUP2 #0f SFT2 EQU #05 JCN
236+ #0000 SWP2 SUB2 }
237
238-JMP2r
239+%lts2 {
240+ #8000 STH2k ADD2 SWP2 STH2r ADD2 GTH2 }
241
242-@draw-line ( x1* y1* x2* y2* color -- )
243+%gts2 {
244+ #8000 STH2k ADD2 SWP2 STH2r ADD2 LTH2 }
245
246- ( load )
247- ,&color STR
248- ,&y STR2
249- ,&x STR2
250+@<draw-line> ( x1* y1* x2* y2* color -- )
251+ ( load ) STH
252+ [ LITr -Screen/sprite ] ;&y STA2
253+ ;&x STA2
254 .line/y2 STZ2
255 .line/x2 STZ2
256-
257+ ;circle-icn .Screen/addr DEO2
258 ,&x LDR2 .line/x2 LDZ2 SUB2 abs2 ,&dx STR2
259 #0000 ,&y LDR2 .line/y2 LDZ2 SUB2 abs2 SUB2 ,&dy STR2
260-
261 #ffff #00 .line/x2 LDZ2 ,&x LDR2 lts2 DUP2 ADD2 ADD2 ,&sx STR2
262 #ffff #00 .line/y2 LDZ2 ,&y LDR2 lts2 DUP2 ADD2 ADD2 ,&sy STR2
263-
264 [ LIT2 &dx $2 ] [ LIT2 &dy $2 ] ADD2 ,&e1 STR2
265-
266- &loop
267- .line/x2 LDZ2 DUP2 .Screen/x DEO2 [ LIT2 &x $2 ] EQU2
268- .line/y2 LDZ2 DUP2 .Screen/y DEO2 [ LIT2 &y $2 ] EQU2
269- [ LIT2 &color $1 -Screen/pixel ] DEO
270- AND ?&end
271- [ LIT2 &e1 $2 ] DUP2 ADD2 DUP2
272- ,&dy LDR2 lts2 ?&skipy
273- ,&e1 LDR2 ,&dy LDR2 ADD2 ,&e1 STR2
274- .line/x2 LDZ2 [ LIT2 &sx $2 ] ADD2 .line/x2 STZ2
275- &skipy
276+ &>l ( -- )
277+ .line/x2 LDZ2 DUP2 .Screen/x DEO2
278+ [ LIT2 &x $2 ] EQU2 .line/y2 LDZ2 DUP2 .Screen/y DEO2
279+ [ LIT2 &y $2 ] EQU2 DEOkr
280+ AND ?&end
281+ [ LIT2 &e1 $2 ] DUP2 ADD2 DUP2 ,&dy LDR2 lts2 ?&skipy
282+ ,&e1 LDR2 ,&dy LDR2 ADD2 ,&e1 STR2
283+ .line/x2 LDZ2 [ LIT2 &sx $2 ] ADD2 .line/x2 STZ2
284+ &skipy ( -- )
285 ,&dx LDR2 gts2 ?&skipx
286- ,&e1 LDR2 ,&dx LDR2 ADD2 ,&e1 STR2
287- .line/y2 LDZ2 [ LIT2 &sy $2 ] ADD2 .line/y2 STZ2
288- &skipx
289- !&loop
290- &end
291+ ,&e1 LDR2 ,&dx LDR2 ADD2 ,&e1 STR2
292+ .line/y2 LDZ2 [ LIT2 &sy $2 ] ADD2 .line/y2 STZ2
293+ &skipx ( -- )
294+ !&>l
295+ &end POP2r JMP2r
296
297-JMP2r
298+@<clear-screen> ( -- )
299+ #0000 DUP2 .Screen/x DEO2
300+ .Screen/y DEO2
301+ [ LIT2 80 -Screen/pixel ] DEO
302+ JMP2r
303
304-@abs2 DUP2 #0f SFT2 EQU #05 JCN #0000 SWP2 SUB2 JMP2r
305-@lts2 #8000 STH2k ADD2 SWP2 STH2r ADD2 GTH2 JMP2r
306-@gts2 #8000 STH2k ADD2 SWP2 STH2r ADD2 LTH2 JMP2r
307+@fill-icn [ ffff ffff ffff ffff ]
308
309-@clear-screen ( -- )
310+@pointer-icn [ 80c0 e0f0 f8e0 1000 ]
311
312- #0000 DUP2 .Screen/x DEO2 .Screen/y DEO2
313- #80 .Screen/pixel DEO
314+@circle-icn [ 187e 7eff ff7e 7e18 ]
315
316-JMP2r
317-
318-@fill-icn [
319- ffff ffff ffff ffff ]
320-@pointer-icn [
321- 80c0 e0f0 f8e0 1000 ]
322 @mouse-icn [
323- 000d 1212 1212 121d
324- 00b0 4848 4848 48b8
325- 1010 1010 1008 0700
326- 0808 0808 0810 e000 ]
327+ 000d 1212 1212 121d 00b0 4848 4848 48b8
328+ 1010 1010 1008 0700 0808 0808 0810 e000 ]
329+
330 @button-icn [
331- 000c 1e1e 1e1e 1e0c
332- 0000 0000 0000 0000
333- 0001 0303 0303 0301
334- 0080 c0c0 c0c0 c080
335- 0000 0000 0000 0000
336- 0030 7878 7878 7830 ]
337-
338-@appicon
339-0000 0000 0000 0000 0000 0000 0000 0001
340-0000 0000 0000 78fc 0c1e 1e07 037b 8503
341-0000 0000 0000 0078 0000 0000 0000 7884
342-0101 0303 0303 0303 0202 0404 0404 0707
343-feff fff9 f6e9 c9e6 0100 0006 0f1f 3fff
344-fcfc fefe fe7e 3e7c 0202 0101 0181 c3fe
345-0301 0100 0000 0000 0703 0301 0000 0000
346-f9ff ffff 7f37 0500 ffff ffff ff7f 3f07
347-fcf8 f8f0 e0c0 0000 fefc fcf8 f0e0 c000
348+ 000c 1e1e 1e1e 1e0c 0000 0000 0000 0000
349+ 0001 0303 0303 0301 0080 c0c0 c0c0 c080
350+ 0000 0000 0000 0000 0030 7878 7878 7830 ]
351+
352+@appicon [
353+ 0000 0000 0000 0000 0000 0000 0000 0001
354+ 0000 0000 0000 78fc 0c1e 1e07 037b 8503
355+ 0000 0000 0000 0078 0000 0000 0000 7884
356+ 0101 0303 0303 0303 0202 0404 0404 0707
357+ feff fff9 f6e9 c9e6 0100 0006 0f1f 3fff
358+ fcfc fefe fe7e 3e7c 0202 0101 0181 c3fe
359+ 0301 0100 0000 0000 0703 0301 0000 0000
360+ f9ff ffff 7f37 0500 ffff ffff ff7f 3f07
361+ fcf8 f8f0 e0c0 0000 fefc fcf8 f0e0 c000 ]
362
363 (
364 @|memory )