https://github.com/akkartik/mu/blob/main/apps/ex11.mu
1
2
3
4
5
6
7
8
9
10
11
12
13
14 fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
15 var env-storage: environment
16 var env/esi: (addr environment) <- address env-storage
17 initialize-environment env, 0x200 0x20, 0x180 0x90, 0x180 0x160
18 {
19 render screen, env
20 edit keyboard, env
21 loop
22 }
23 }
24
25 type environment {
26 p0: (handle point)
27 p1: (handle point)
28 p2: (handle point)
29 cursor: (handle point)
30 }
31
32 type point {
33 x: int
34 y: int
35 }
36
37 fn render screen: (addr screen), _self: (addr environment) {
38 clear-screen screen
39 var self/esi: (addr environment) <- copy _self
40 var tmp-ah/ecx: (addr handle point) <- get self, p0
41 var tmp/eax: (addr point) <- lookup *tmp-ah
42 var p0/ebx: (addr point) <- copy tmp
43 tmp-ah <- get self, p1
44 tmp <- lookup *tmp-ah
45 var p1/edx: (addr point) <- copy tmp
46 tmp-ah <- get self, p2
47 tmp <- lookup *tmp-ah
48 var p2/ecx: (addr point) <- copy tmp
49
50 line screen, p0, p1, 7/color
51 line screen, p1, p2, 7/color
52
53 bezier screen, p0, p1, p2, 0xc/color
54
55 disc screen, p0, 3/radius, 7/color 0xf/border
56 disc screen, p1, 3/radius, 7/color 0xf/border
57 disc screen, p2, 3/radius, 7/color 0xf/border
58
59 var cursor-ah/eax: (addr handle point) <- get self, cursor
60 var cursor/eax: (addr point) <- lookup *cursor-ah
61 cursor screen, cursor, 0xa/side, 3/color
62 }
63
64 fn bezier screen: (addr screen), _p0: (addr point), _p1: (addr point), _p2: (addr point), color: int {
65 var p0/esi: (addr point) <- copy _p0
66 var x0/ecx: (addr int) <- get p0, x
67 var y0/edx: (addr int) <- get p0, y
68 var p1/esi: (addr point) <- copy _p1
69 var x1/ebx: (addr int) <- get p1, x
70 var y1/eax: (addr int) <- get p1, y
71 var p2/esi: (addr point) <- copy _p2
72 var x2/edi: (addr int) <- get p2, x
73 var y2/esi: (addr int) <- get p2, y
74 draw-monotonic-bezier screen, *x0 *y0, *x1 *y1, *x2 *y2, color
75 }
76
77 fn cursor screen: (addr screen), _p: (addr point), side: int, color: int {
78 var half-side/eax: int <- copy side
79 half-side <- shift-right 1
80 var p/esi: (addr point) <- copy _p
81 var x-a/ecx: (addr int) <- get p, x
82 var left-x/ecx: int <- copy *x-a
83 left-x <- subtract half-side
84 var y-a/edx: (addr int) <- get p, y
85 var top-y/edx: int <- copy *y-a
86 top-y <- subtract half-side
87 var max/eax: int <- copy left-x
88 max <- add side
89 draw-horizontal-line screen, top-y, left-x, max, color
90 max <- copy top-y
91 max <- add side
92 draw-vertical-line screen, left-x, top-y, max, color
93 var right-x/ebx: int <- copy left-x
94 right-x <- add side
95 draw-vertical-line screen, right-x, top-y, max, color
96 var bottom-y/edx: int <- copy top-y
97 bottom-y <- add side
98 draw-horizontal-line screen, bottom-y, left-x, right-x, color
99 }
100
101 fn edit keyboard: (addr keyboard), _self: (addr environment) {
102 var self/esi: (addr environment) <- copy _self
103 var key/eax: byte <- read-key keyboard
104 compare key, 0
105 loop-if-=
106 {
107 compare key, 9/tab
108 break-if-!=
109 toggle-cursor self
110 return
111 }
112 {
113 compare key, 0x80/left-arrow
114 break-if-!=
115 cursor-left self
116 return
117 }
118 {
119 compare key, 0x83/right-arrow
120 break-if-!=
121 cursor-right self
122 return
123 }
124 {
125 compare key, 0x81/down-arrow
126 break-if-!=
127 cursor-down self
128 return
129 }
130 {
131 compare key, 0x82/up-arrow
132 break-if-!=
133 cursor-up self
134 return
135 }
136 }
137
138 fn toggle-cursor _self: (addr environment) {
139 var self/esi: (addr environment) <- copy _self
140 var cursor-ah/edi: (addr handle point) <- get self, cursor
141 var p0-ah/ecx: (addr handle point) <- get self, p0
142 var p1-ah/edx: (addr handle point) <- get self, p1
143 var p2-ah/ebx: (addr handle point) <- get self, p2
144 {
145 var p0?/eax: boolean <- handle-equal? *p0-ah, *cursor-ah
146 compare p0?, 0/false
147 break-if-=
148 copy-object p1-ah, cursor-ah
149 return
150 }
151 {
152 var p1?/eax: boolean <- handle-equal? *p1-ah, *cursor-ah
153 compare p1?, 0/false
154 break-if-=
155 copy-object p2-ah, cursor-ah
156 return
157 }
158 {
159 var p2?/eax: boolean <- handle-equal? *p2-ah, *cursor-ah
160 compare p2?, 0/false
161 break-if-=
162 copy-object p0-ah, cursor-ah
163 return
164 }
165 abort "lost cursor"
166 }
167
168 fn cursor-left _self: (addr environment) {
169 var self/esi: (addr environment) <- copy _self
170 var cursor-ah/esi: (addr handle point) <- get self, cursor
171 var cursor/eax: (addr point) <- lookup *cursor-ah
172 var cursor-x/eax: (addr int) <- get cursor, x
173 compare *cursor-x, 0x20
174 {
175 break-if-<
176 subtract-from *cursor-x, 0x20
177 }
178 }
179
180 fn cursor-right _self: (addr environment) {
181 var self/esi: (addr environment) <- copy _self
182 var cursor-ah/esi: (addr handle point) <- get self, cursor
183 var cursor/eax: (addr point) <- lookup *cursor-ah
184 var cursor-x/eax: (addr int) <- get cursor, x
185 compare *cursor-x, 0x3f0
186 {
187 break-if->
188 add-to *cursor-x, 0x20
189 }
190 }
191
192 fn cursor-up _self: (addr environment) {
193 var self/esi: (addr environment) <- copy _self
194 var cursor-ah/esi: (addr handle point) <- get self, cursor
195 var cursor/eax: (addr point) <- lookup *cursor-ah
196 var cursor-y/eax: (addr int) <- get cursor, y
197 compare *cursor-y, 0x20
198 {
199 break-if-<
200 subtract-from *cursor-y, 0x20
201 }
202 }
203
204 fn cursor-down _self: (addr environment) {
205 var self/esi: (addr environment) <- copy _self
206 var cursor-ah/esi: (addr handle point) <- get self, cursor
207 var cursor/eax: (addr point) <- lookup *cursor-ah
208 var cursor-y/eax: (addr int) <- get cursor, y
209 compare *cursor-y, 0x2f0
210 {
211 break-if->
212 add-to *cursor-y, 0x20
213 }
214 }
215
216 fn line screen: (addr screen), _p0: (addr point), _p1: (addr point), color: int {
217 var p0/esi: (addr point) <- copy _p0
218 var x0/ecx: (addr int) <- get p0, x
219 var y0/edx: (addr int) <- get p0, y
220 var p1/esi: (addr point) <- copy _p1
221 var x1/ebx: (addr int) <- get p1, x
222 var y1/eax: (addr int) <- get p1, y
223 draw-line screen, *x0 *y0, *x1 *y1, color
224 }
225
226 fn disc screen: (addr screen), _p: (addr point), radius: int, color: int, border-color: int {
227 var p/esi: (addr point) <- copy _p
228 var x/ecx: (addr int) <- get p, x
229 var y/edx: (addr int) <- get p, y
230 draw-disc screen, *x *y, radius, color, border-color
231 }
232
233 fn initialize-environment _self: (addr environment), x0: int, y0: int, x1: int, y1: int, x2: int, y2: int {
234 var self/esi: (addr environment) <- copy _self
235 var p0-ah/eax: (addr handle point) <- get self, p0
236 allocate p0-ah
237 var p0/eax: (addr point) <- lookup *p0-ah
238 initialize-point p0, x0 y0
239 var p1-ah/eax: (addr handle point) <- get self, p1
240 allocate p1-ah
241 var p1/eax: (addr point) <- lookup *p1-ah
242 initialize-point p1, x1 y1
243 var p2-ah/eax: (addr handle point) <- get self, p2
244 allocate p2-ah
245 var p2/eax: (addr point) <- lookup *p2-ah
246 initialize-point p2, x2 y2
247
248 var cursor-ah/edi: (addr handle point) <- get self, cursor
249 var src-ah/esi: (addr handle point) <- get self, p0
250 copy-object src-ah, cursor-ah
251 }
252
253 fn initialize-point _p: (addr point), x: int, y: int {
254 var p/esi: (addr point) <- copy _p
255 var dest/eax: (addr int) <- get p, x
256 var src/ecx: int <- copy x
257 copy-to *dest, src
258 dest <- get p, y
259 src <- copy y
260 copy-to *dest, src
261 }