"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
[c.x; c.y; c.w; c.h; c.ox; c.oy])
let sexp_of_sprite_map clips =
Sexp.List (
Atom "map" ::
(List.sort (fun a b -> compare b.name a.name) clips |>
List.rev_map sexp_of_clip))
type sprite_map = {
dpi : string;
clips : clip list;
}
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 ink v = ("http://www.inkscape.org/namespaces/inkscape", v)
@ -107,10 +117,12 @@ let extract_clips xml =
s.clips
let gen_sprite_map ic oc =
let sprite_map =
Xmlm.make_input (`Channel ic) |>
extract_clips
in
let sprite_map = {
dpi = Sys.argv.(1);
clips =
Xmlm.make_input (`Channel ic) |>
extract_clips
} in
Format.kasprintf (output_string oc) "%a\n"
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)
[[ "$src" -nt "$dst_map" ]] &&
($gen_sprite_map < $src > $dst_map || exit 1)
($gen_sprite_map $dpi < $src > $dst_map || exit 1)
}
gen blocks 192

View File

@ -12,7 +12,7 @@ let main () =
info (fun m -> m "renderer initialized");
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_font ctx (Asset.load_font "roman-md");
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_file : string -> (bigstring -> '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
end

View File

@ -66,23 +66,25 @@ let frame_of_sexp ~pdf = function
| sexp ->
Sexp_conv.of_sexp_error "invalid sprite" sexp
let of_sexp ~name ~texture ?dpi = function
| Sexp.List (Atom "map" :: args) ->
let pdf = match dpi with
| Some dpi -> Float.of_int dpi /. 96.0
| None -> 1.0
in
let frames = List.to_seq args |> Seq.map (frame_of_sexp ~pdf) in
let of_sexp ~name ~texture = function
| Sexp.List [
Atom "map";
List [Atom "dpi"; dpi];
List (Atom "frames" :: frames)
] ->
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
| sexp ->
Sexp_conv.of_sexp_error "invalid sprite map" sexp
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 map_path = Format.sprintf "sprites/%s.map" name 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);
spritemap
end