change scene sexp syntax for sprites

This commit is contained in:
tali 2024-01-20 15:36:43 -05:00
parent d7209eebec
commit 65dd5eb0b8
2 changed files with 38 additions and 42 deletions

View File

@ -3,44 +3,45 @@
(object "playfield"
(transform (tx -160) (ty 320))
(sprite hud grid)
(sprite hud field)
(sprites hud grid field)
(object blocks
(sprite blocks L (pos 0 0))
(sprite blocks L (pos 32 0))
(sprite blocks L (pos 64 0))
(sprite blocks L (pos 64 -32))))
(sprites blocks
(L 0 0)
(L 32 0)
(L 64 0)
(L 64 -32))))
(object "hold"
(transform (tx -272) (ty -272))
(sprite hud hold))
(sprites hud hold))
(object "next"
(transform (tx 272) (ty -272))
(sprite hud next))
(sprites hud next))
(object "left_labels"
(transform (tx -172) (ty 320))
(sprite hud pps (pos -12 -148))
(sprite hud kpp (pos -12 -116))
(sprites hud
(pps -12 -148)
(kpp -12 -116)
(num 0 -73)
(time 0 -25))
(sprite hud num (pos 0 -73))
(object "pcnt"
(label mono-sm "0")
(transform (ty -50) (sx 24) (sy 24)
; TODO: text align right
(tx -14)))
(tx -14))
(label mono-sm "0"))
(sprite hud time (pos 0 -25))
(object "time"
(label mono-sm "0:00.00")
(transform (ty -2) (sx 24) (sy 24)
; TODO: text align right
(tx -100))))
(tx -100))
(label mono-sm "0:00.00")))
(object "right_labels"
(transform (tx 272) (ty 320))
(sprite hud lines_rem (pos 0 -20))
(sprites hud (lines_rem 0 -20))
(object "goal"
(label roman-md "40")
(transform (ty -56) (sx 80) (sy 80)

View File

@ -191,7 +191,6 @@ module Scene = struct
and obj = {
name : string option;
transform : Transform_graph.node;
sprites : Sprite_graph.node list;
}
let get t name = try Hashtbl.find t.objs name
@ -212,25 +211,23 @@ module Scene = struct
| sexp ->
of_sexp_error "bad argument to transform" sexp
let parse_sprite_arg ~(pos : vec2) = function
| Sexp.List [Atom "pos"; x; y] ->
Vec2.set pos
~x:(float_of_sexp x)
~y:(float_of_sexp y)
(* | Sexp.List [Atom "tint"; rgb] -> *)
(* Color.set_rgb24 tint *)
(* (Sexplib.Conv.int_of_sexp rgb) *)
| sexp ->
of_sexp_error "bad argument to sprite" sexp
let parse_sprite ~sg ~tf = function
| Sexp.List (Atom "sprite" :: Atom map :: Atom sprite :: args) ->
let parse_sprite_arg ~sg ~tf ~map = function
| Sexp.List [Atom sprite; x; y] ->
let sprite = Sprite_graph.get_sprite sg map sprite in
let pos = vec2 (float_of_sexp x) (float_of_sexp y) in
Sprite_graph.add_sprite sg ~tf ~pos ~sprite |> ignore
| Sexp.List [Atom sprite] | Atom sprite ->
let sprite = Sprite_graph.get_sprite sg map sprite in
let pos = vec2 0.0 0.0 in
List.iter (parse_sprite_arg ~pos) args;
Sprite_graph.add_sprite sg ~tf ~pos ~sprite
Sprite_graph.add_sprite sg ~tf ~pos ~sprite |> ignore
| sexp ->
of_sexp_error "invalid sprite" sexp
of_sexp_error "bad sprite argument" sexp
let parse_sprites ~sg ~tf = function
| Sexp.List (Atom "sprites" :: Atom map :: args) ->
List.iter (parse_sprite_arg ~sg ~tf ~map) args;
| sexp ->
of_sexp_error "invalid sprites" sexp
let parse_label_arg ~(fg : color) = function
| Sexp.List [Atom "fg"; rgb] ->
@ -243,7 +240,7 @@ module Scene = struct
let font = Sprite_graph.get_font sg font in
let fg = rgb24 0xffffff in
List.iter (parse_label_arg ~fg) args;
Sprite_graph.add_label sg ~tf ~font ~text ~fg
Sprite_graph.add_label sg ~tf ~font ~text ~fg |> ignore
| sexp ->
of_sexp_error "invalid label" sexp
@ -255,7 +252,7 @@ module Scene = struct
| Some parent -> parent.transform
in
let transform = Transform_graph.add tg ptgn in
let obj = { name = None; transform; sprites = [] } in
let obj = { name = None; transform } in
let obj = parse_obj_args t obj args ~tg ~sg in
Option.iter
(fun name -> Hashtbl.replace t.objs name obj)
@ -265,7 +262,7 @@ module Scene = struct
of_sexp_error "invalid object" sexp
and parse_obj_args t obj ~tg ~sg = function
| [] -> { obj with sprites = List.rev obj.sprites }
| [] -> obj
| arg :: args ->
match arg with
| Atom name ->
@ -279,16 +276,14 @@ module Scene = struct
trace (fun m -> m "parse_obj_args: tf=%a" Mat2A.pp tf);
parse_obj_args t obj args ~tg ~sg
| Sexp.List (Atom "sprite" :: _) ->
| Sexp.List (Atom "sprites" :: _) ->
let tf = Transform_graph.world obj.transform in
let spr = parse_sprite arg ~sg ~tf in
let obj = { obj with sprites = spr :: obj.sprites } in
parse_sprites arg ~sg ~tf;
parse_obj_args t obj args ~tg ~sg
| Sexp.List (Atom "label" :: _) ->
let tf = Transform_graph.world obj.transform in
let lab = parse_label arg ~sg ~tf in
let obj = { obj with sprites = lab :: obj.sprites } in
parse_label arg ~sg ~tf;
parse_obj_args t obj args ~tg ~sg
| Sexp.List (Atom "object" :: _) ->