"dpi" is part of the spritemap data, not passed as an argument

This commit is contained in:
milo 2024-02-05 16:33:57 -05:00
parent bbeb1f79c7
commit a3c2d0dcca
5 changed files with 35 additions and 21 deletions

View File

@ -16,11 +16,21 @@ let sexp_of_clip c =
List.map Sexplib.Conv.sexp_of_int List.map Sexplib.Conv.sexp_of_int
[c.x; c.y; c.w; c.h; c.ox; c.oy]) [c.x; c.y; c.w; c.h; c.ox; c.oy])
let sexp_of_sprite_map clips = type sprite_map = {
Sexp.List ( dpi : string;
Atom "map" :: clips : clip list;
(List.sort (fun a b -> compare b.name a.name) clips |> }
List.rev_map sexp_of_clip))
let sexp_of_sprite_map s =
Sexp.List [
Atom "map";
List [Atom "dpi"; Atom s.dpi];
List (
Atom "frames" ::
(List.sort (fun a b -> compare b.name a.name) s.clips |>
List.rev_map sexp_of_clip)
)
]
let svg v = ("http://www.w3.org/2000/svg", v) let svg v = ("http://www.w3.org/2000/svg", v)
let ink v = ("http://www.inkscape.org/namespaces/inkscape", v) let ink v = ("http://www.inkscape.org/namespaces/inkscape", v)
@ -107,10 +117,12 @@ let extract_clips xml =
s.clips s.clips
let gen_sprite_map ic oc = let gen_sprite_map ic oc =
let sprite_map = let sprite_map = {
Xmlm.make_input (`Channel ic) |> dpi = Sys.argv.(1);
extract_clips clips =
in Xmlm.make_input (`Channel ic) |>
extract_clips
} in
Format.kasprintf (output_string oc) "%a\n" Format.kasprintf (output_string oc) "%a\n"
Sexp.pp_hum (sexp_of_sprite_map sprite_map) Sexp.pp_hum (sexp_of_sprite_map sprite_map)

View File

@ -28,7 +28,7 @@ function gen() {
($svg_to_png $src -o $dst_png -d $dpi || exit 1) ($svg_to_png $src -o $dst_png -d $dpi || exit 1)
[[ "$src" -nt "$dst_map" ]] && [[ "$src" -nt "$dst_map" ]] &&
($gen_sprite_map < $src > $dst_map || exit 1) ($gen_sprite_map $dpi < $src > $dst_map || exit 1)
} }
gen blocks 192 gen blocks 192

View File

@ -12,7 +12,7 @@ let main () =
info (fun m -> m "renderer initialized"); info (fun m -> m "renderer initialized");
let ctx = Scene.make_context () in let ctx = Scene.make_context () in
Scene.register_sprite_map ctx (Asset.load_sprite_map "blocks" ~dpi:192); Scene.register_sprite_map ctx (Asset.load_sprite_map "blocks");
Scene.register_sprite_map ctx (Asset.load_sprite_map "hud"); Scene.register_sprite_map ctx (Asset.load_sprite_map "hud");
Scene.register_font ctx (Asset.load_font "roman-md"); Scene.register_font ctx (Asset.load_font "roman-md");
Scene.register_font ctx (Asset.load_font "mono-sm"); Scene.register_font ctx (Asset.load_font "mono-sm");

View File

@ -55,6 +55,6 @@ module Asset : sig
val load_string : string -> string val load_string : string -> string
val load_file : string -> (bigstring -> 'a) -> 'a val load_file : string -> (bigstring -> 'a) -> 'a
val load_sexp_conv : string -> (Sexp.t -> 'a) -> 'a val load_sexp_conv : string -> (Sexp.t -> 'a) -> 'a
val load_sprite_map : ?dpi:int -> string -> Sprite_map.t val load_sprite_map : string -> Sprite_map.t
val load_font : string -> Font.t val load_font : string -> Font.t
end end

View File

@ -66,23 +66,25 @@ let frame_of_sexp ~pdf = function
| sexp -> | sexp ->
Sexp_conv.of_sexp_error "invalid sprite" sexp Sexp_conv.of_sexp_error "invalid sprite" sexp
let of_sexp ~name ~texture ?dpi = function let of_sexp ~name ~texture = function
| Sexp.List (Atom "map" :: args) -> | Sexp.List [
let pdf = match dpi with Atom "map";
| Some dpi -> Float.of_int dpi /. 96.0 List [Atom "dpi"; dpi];
| None -> 1.0 List (Atom "frames" :: frames)
in ] ->
let frames = List.to_seq args |> Seq.map (frame_of_sexp ~pdf) in let dpi = Sexp_conv.int_of_sexp dpi in
let pdf = Float.of_int dpi /. 96.0 in
let frames = List.to_seq frames |> Seq.map (frame_of_sexp ~pdf) in
make ~name ~texture ~frames make ~name ~texture ~frames
| sexp -> | sexp ->
Sexp_conv.of_sexp_error "invalid sprite map" sexp Sexp_conv.of_sexp_error "invalid sprite map" sexp
module Asset = struct module Asset = struct
let load_sprite_map ?dpi name = let load_sprite_map name =
let tex_path = Format.sprintf "sprites/%s.png" name in let tex_path = Format.sprintf "sprites/%s.png" name in
let map_path = Format.sprintf "sprites/%s.map" name in let map_path = Format.sprintf "sprites/%s.map" name in
let texture = Texture.Asset.load_texture tex_path in let texture = Texture.Asset.load_texture tex_path in
let spritemap = Asset.load_sexp_conv map_path (of_sexp ~name ~texture ?dpi) in let spritemap = Asset.load_sexp_conv map_path (of_sexp ~name ~texture) in
debug (fun m -> m "loaded sprite map %S" name); debug (fun m -> m "loaded sprite map %S" name);
spritemap spritemap
end end