replace 'circle' with Mu implementation

This commit is contained in:
Kartik K. Agaram 2021-07-05 18:21:02 -07:00
parent c7bedaf49b
commit 493aabf79a
2 changed files with 161 additions and 19 deletions

View File

@ -140,24 +140,6 @@
(fill_rect . [def (fill_rect screen x1 y1 x2 y2 color)
for y y1 (y < y2) ++y
(hline screen y x1 x2 color)])
(circle . [def (circle scr cx cy r clr)
with (x (0 - r)
y 0
err (2 - 2*r)
continue? 1)
while continue?
(pixel scr cx-x cy+y clr)
(pixel scr cx-y cy-x clr)
(pixel scr cx+x cy-y clr)
(pixel scr cx+y cy+x clr)
(set r err)
when (r <= y)
++y
err += (2*y + 1)
when (or (r > x) (err > y))
++x
err += (2*x + 1)
set continue? (x < 0)])
(ring . [def (ring screen cx cy r0 w clr)
for r r0 (r < r0+w) ++r
(circle screen cx cy r clr)])

View File

@ -41,6 +41,7 @@ fn initialize-primitives _self: (addr global-table) {
append-primitive self, "line"
append-primitive self, "hline"
append-primitive self, "vline"
append-primitive self, "circle"
append-primitive self, "width"
append-primitive self, "height"
# for keyboards
@ -128,7 +129,7 @@ fn render-primitives screen: (addr screen), xmin: int, xmax: int, ymax: int {
tmpx <- draw-text-rightward screen, "pixel graphics", tmpx, left-max, y, 7/fg=grey, 0xdc/bg=green-bg
y <- increment
var tmpx/eax: int <- copy xmin
tmpx <- draw-text-rightward screen, " line hline vline pixel", tmpx, left-max, y, 0x2a/fg=orange, 0xdc/bg=green-bg
tmpx <- draw-text-rightward screen, " circle line hline vline pixel", tmpx, left-max, y, 0x2a/fg=orange, 0xdc/bg=green-bg
y <- increment
var tmpx/eax: int <- copy xmin
tmpx <- draw-text-rightward screen, " width height", tmpx, left-max, y, 0x2a/fg=orange, 0xdc/bg=green-bg
@ -456,6 +457,13 @@ fn apply-primitive _f: (addr cell), args-ah: (addr handle cell), out: (addr hand
apply-vline args-ah, out, trace
return
}
{
var circle?/eax: boolean <- string-equal? f-name, "circle"
compare circle?, 0/false
break-if-=
apply-circle args-ah, out, trace
return
}
{
var width?/eax: boolean <- string-equal? f-name, "width"
compare width?, 0/false
@ -2439,6 +2447,158 @@ fn apply-vline _args-ah: (addr handle cell), out: (addr handle cell), trace: (ad
# return nothing
}
fn apply-circle _args-ah: (addr handle cell), out: (addr handle cell), trace: (addr trace) {
trace-text trace, "eval", "apply 'circle'"
var args-ah/eax: (addr handle cell) <- copy _args-ah
var _args/eax: (addr cell) <- lookup *args-ah
var args/esi: (addr cell) <- copy _args
{
var args-type/eax: (addr int) <- get args, type
compare *args-type, 0/pair
break-if-=
error trace, "args to 'circle' are not a list"
return
}
var empty-args?/eax: boolean <- nil? args
compare empty-args?, 0/false
{
break-if-=
error trace, "'circle' needs 5 args but got 0"
return
}
# screen = args->left
var first-ah/eax: (addr handle cell) <- get args, left
var first/eax: (addr cell) <- lookup *first-ah
{
var first-type/eax: (addr int) <- get first, type
compare *first-type, 5/screen
break-if-=
error trace, "first arg for 'circle' is not a screen"
return
}
var screen-ah/eax: (addr handle screen) <- get first, screen-data
var _screen/eax: (addr screen) <- lookup *screen-ah
var screen/edi: (addr screen) <- copy _screen
# cx = args->right->left->value
var rest-ah/eax: (addr handle cell) <- get args, right
var _rest/eax: (addr cell) <- lookup *rest-ah
var rest/esi: (addr cell) <- copy _rest
{
var rest-type/eax: (addr int) <- get rest, type
compare *rest-type, 0/pair
break-if-=
error trace, "'circle' encountered non-pair"
return
}
{
var rest-nil?/eax: boolean <- nil? rest
compare rest-nil?, 0/false
break-if-=
error trace, "'circle' needs 5 args but got 1"
return
}
var second-ah/eax: (addr handle cell) <- get rest, left
var second/eax: (addr cell) <- lookup *second-ah
{
var second-type/eax: (addr int) <- get second, type
compare *second-type, 1/number
break-if-=
error trace, "second arg for 'circle' is not a number (screen x coordinate of center)"
return
}
var second-value/eax: (addr float) <- get second, number-data
var cx/edx: int <- convert *second-value
# cy = rest->right->left->value
var rest-ah/eax: (addr handle cell) <- get rest, right
var _rest/eax: (addr cell) <- lookup *rest-ah
rest <- copy _rest
{
var rest-type/eax: (addr int) <- get rest, type
compare *rest-type, 0/pair
break-if-=
error trace, "'circle' encountered non-pair"
return
}
{
var rest-nil?/eax: boolean <- nil? rest
compare rest-nil?, 0/false
break-if-=
error trace, "'circle' needs 5 args but got 2"
return
}
var third-ah/eax: (addr handle cell) <- get rest, left
var third/eax: (addr cell) <- lookup *third-ah
{
var third-type/eax: (addr int) <- get third, type
compare *third-type, 1/number
break-if-=
error trace, "third arg for 'circle' is not a number (screen y coordinate of center)"
return
}
var third-value/eax: (addr float) <- get third, number-data
var cy/ebx: int <- convert *third-value
# r = rest->right->left->value
var rest-ah/eax: (addr handle cell) <- get rest, right
var _rest/eax: (addr cell) <- lookup *rest-ah
var rest/esi: (addr cell) <- copy _rest
{
var rest-type/eax: (addr int) <- get rest, type
compare *rest-type, 0/pair
break-if-=
error trace, "'circle' encountered non-pair"
return
}
{
var rest-nil?/eax: boolean <- nil? rest
compare rest-nil?, 0/false
break-if-=
error trace, "'circle' needs 5 args but got 3"
return
}
var fourth-ah/eax: (addr handle cell) <- get rest, left
var fourth/eax: (addr cell) <- lookup *fourth-ah
{
var fourth-type/eax: (addr int) <- get fourth, type
compare *fourth-type, 1/number
break-if-=
error trace, "fourth arg for 'circle' is not a number (screen radius)"
return
}
var fourth-value/eax: (addr float) <- get fourth, number-data
var r/ecx: int <- convert *fourth-value
# color = rest->right->left->value
var rest-ah/eax: (addr handle cell) <- get rest, right
var _rest/eax: (addr cell) <- lookup *rest-ah
rest <- copy _rest
{
var rest-type/eax: (addr int) <- get rest, type
compare *rest-type, 0/pair
break-if-=
error trace, "'circle' encountered non-pair"
return
}
{
var rest-nil?/eax: boolean <- nil? rest
compare rest-nil?, 0/false
break-if-=
error trace, "'circle' needs 5 args but got 5"
return
}
var fifth-ah/eax: (addr handle cell) <- get rest, left
var fifth/eax: (addr cell) <- lookup *fifth-ah
{
var fifth-type/eax: (addr int) <- get fifth, type
compare *fifth-type, 1/number
break-if-=
error trace, "fifth arg for 'circle' is not an int (color; 0..0xff)"
return
}
var fifth-value/eax: (addr float) <- get fifth, number-data
var color/eax: int <- convert *fifth-value
draw-circle screen, cx, cy, r, color
# return nothing
}
fn apply-wait-for-key _args-ah: (addr handle cell), out: (addr handle cell), trace: (addr trace) {
trace-text trace, "eval", "apply 'key'"
var args-ah/eax: (addr handle cell) <- copy _args-ah