Compare commits
3 Commits
5ce8d30f89
...
104d941ab7
Author | SHA1 | Date |
---|---|---|
tali | 104d941ab7 | |
tali | 95758ed7c6 | |
tali | d6882feb15 |
15
src/main.ml
15
src/main.ml
|
@ -19,20 +19,18 @@ let main () =
|
|||
let scene = Scene.load "main" ~tg ~sg in
|
||||
let root = Scene.get scene "root" in
|
||||
|
||||
let render () =
|
||||
let render time =
|
||||
begin
|
||||
(* Update *)
|
||||
|
||||
let time = Sdl.get_ticks () |> Int32.to_int in
|
||||
let tf = TG.model (Scene.transform root) in
|
||||
begin
|
||||
let tx =
|
||||
Float_infix.(
|
||||
Float.sin (flt time / 1000.0 * 2.0) * 300.0
|
||||
Float.sin (time * 2.0) * 300.0
|
||||
+ 512.0
|
||||
)
|
||||
in
|
||||
let ty = 400.0 in
|
||||
let tf = TG.model (Scene.transform root) in
|
||||
Mat2A.set tf ~tx ~ty ~sx:1.0 ~sy:1.0
|
||||
end;
|
||||
|
||||
|
@ -48,7 +46,12 @@ let main () =
|
|||
end
|
||||
in
|
||||
Window.event_loop window ~render;
|
||||
info (fun m -> m "quitting")
|
||||
|
||||
begin
|
||||
info (fun m -> m "quitting");
|
||||
Window.destroy window;
|
||||
Renderer.destroy ren;
|
||||
end
|
||||
|
||||
let () =
|
||||
Ohlog.init () ~min_level:DEBUG;
|
||||
|
|
|
@ -5,4 +5,4 @@
|
|||
adam
|
||||
s2
|
||||
ohlog
|
||||
sexplib))
|
||||
sexplib0))
|
||||
|
|
67
src/n2/n2.ml
67
src/n2/n2.ml
|
@ -1,5 +1,4 @@
|
|||
open Adam
|
||||
module Sexp = Sexplib0.Sexp
|
||||
|
||||
include (val Ohlog.logs "N2")
|
||||
|
||||
|
@ -144,6 +143,9 @@ end
|
|||
*)
|
||||
|
||||
module Scene = struct
|
||||
module Sexp = Sexplib0.Sexp
|
||||
open Sexplib0.Sexp_conv
|
||||
|
||||
include (val Ohlog.sublogs logger "Scene")
|
||||
|
||||
type t = {
|
||||
|
@ -156,36 +158,42 @@ module Scene = struct
|
|||
sprites : Sprite_graph.node list;
|
||||
}
|
||||
|
||||
let get t name = try Hashtbl.find t.objs name
|
||||
with Not_found ->
|
||||
Format.ksprintf failwith "no node %S in scene" name
|
||||
|
||||
let transform obj = obj.transform
|
||||
|
||||
let parse_transform_arg ~(tf : mat2a) = function
|
||||
| Sexp.List [Atom "tx"; arg] ->
|
||||
tf.a2 <- Sexplib.Conv.float_of_sexp arg
|
||||
tf.a2 <- float_of_sexp arg
|
||||
| Sexp.List [Atom "ty"; arg] ->
|
||||
tf.a5 <- Sexplib.Conv.float_of_sexp arg
|
||||
tf.a5 <- float_of_sexp arg
|
||||
| Sexp.List [Atom "sx"; arg] ->
|
||||
tf.a0 <- Sexplib.Conv.float_of_sexp arg
|
||||
tf.a0 <- float_of_sexp arg
|
||||
| Sexp.List [Atom "sy"; arg] ->
|
||||
tf.a4 <- Sexplib.Conv.float_of_sexp arg
|
||||
tf.a4 <- float_of_sexp arg
|
||||
| sexp ->
|
||||
Sexplib.Conv.of_sexp_error "bad argument to transform" sexp
|
||||
of_sexp_error "bad argument to transform" sexp
|
||||
|
||||
let parse_sprite_arg ~(rect : aabb) ~(fill : color) = function
|
||||
| Sexp.List [Atom "rect"; x0; x1; y0; y1] ->
|
||||
AABB.set rect
|
||||
~x0:(Sexplib.Conv.float_of_sexp x0)
|
||||
~y0:(Sexplib.Conv.float_of_sexp x1)
|
||||
~x1:(Sexplib.Conv.float_of_sexp y0)
|
||||
~y1:(Sexplib.Conv.float_of_sexp y1)
|
||||
~x0:(float_of_sexp x0)
|
||||
~y0:(float_of_sexp x1)
|
||||
~x1:(float_of_sexp y0)
|
||||
~y1:(float_of_sexp y1)
|
||||
| Sexp.List [Atom "fill"; rgb] ->
|
||||
Color.set_rgb24 fill
|
||||
(Sexplib.Conv.int_of_sexp rgb)
|
||||
| Sexp.List [Atom "fill"; r; g; b] ->
|
||||
begin
|
||||
fill.r <- Sexplib.Conv.float_of_sexp r;
|
||||
fill.g <- Sexplib.Conv.float_of_sexp g;
|
||||
fill.b <- Sexplib.Conv.float_of_sexp b;
|
||||
fill.r <- float_of_sexp r;
|
||||
fill.g <- float_of_sexp g;
|
||||
fill.b <- float_of_sexp b;
|
||||
end
|
||||
| sexp ->
|
||||
Sexplib.Conv.of_sexp_error "bad argument to sprite" sexp
|
||||
of_sexp_error "bad argument to sprite" sexp
|
||||
|
||||
let parse_sprite ~sg ~tf = function
|
||||
| Sexp.List (Atom "sprite" :: args) ->
|
||||
|
@ -194,7 +202,7 @@ module Scene = struct
|
|||
List.iter (parse_sprite_arg ~rect ~fill) args;
|
||||
Sprite_graph.add_rect sg ~tf ~bb:rect ~fill
|
||||
| sexp ->
|
||||
Sexplib.Conv.of_sexp_error "invalid sprite" sexp
|
||||
of_sexp_error "invalid sprite" sexp
|
||||
|
||||
let rec parse_obj t ~tg ~sg ~parent = function
|
||||
| Sexp.List (Atom "object" :: args) ->
|
||||
|
@ -208,7 +216,7 @@ module Scene = struct
|
|||
obj.name;
|
||||
obj
|
||||
| sexp ->
|
||||
Sexplib.Conv.of_sexp_error "invalid object" sexp
|
||||
of_sexp_error "invalid object" sexp
|
||||
|
||||
and parse_obj_args t obj ~tg ~sg = function
|
||||
| [] -> { obj with sprites = List.rev obj.sprites }
|
||||
|
@ -237,7 +245,7 @@ module Scene = struct
|
|||
trace (fun m -> m "parse_obj_args: end object");
|
||||
parse_obj_args t obj args ~tg ~sg
|
||||
| sexp ->
|
||||
Sexplib.Conv.of_sexp_error "bad argument to object" sexp
|
||||
of_sexp_error "bad argument to object" sexp
|
||||
|
||||
let of_sexp ~tg ~sg = function
|
||||
| Sexp.List [Atom "scene"; root] ->
|
||||
|
@ -248,27 +256,10 @@ module Scene = struct
|
|||
t
|
||||
|
||||
| sexp ->
|
||||
Sexplib.Conv.of_sexp_error "invalid scene" sexp
|
||||
of_sexp_error "invalid scene" sexp
|
||||
|
||||
let load name ~tg ~sg =
|
||||
let path = Format.sprintf "%s.scene" name in
|
||||
match Sexplib.Sexp.of_string_conv
|
||||
(S2.Asset.load_file path)
|
||||
(of_sexp ~tg ~sg)
|
||||
with
|
||||
| `Result v ->
|
||||
debug (fun m -> m "loaded scene %S" name);
|
||||
v
|
||||
| `Error (exn, _) ->
|
||||
let msg = match exn with
|
||||
| Failure msg -> msg
|
||||
| exn -> Printexc.to_string exn
|
||||
in
|
||||
raise (S2.Asset.Error (path, "parse error: " ^ msg))
|
||||
|
||||
let get t name =
|
||||
try Hashtbl.find t.objs name
|
||||
with Not_found -> Format.ksprintf failwith "no node %S in scene" name
|
||||
|
||||
let transform obj = obj.transform
|
||||
S2.Asset.load_sexp_conv
|
||||
(name ^ ".scene")
|
||||
(of_sexp ~tg ~sg)
|
||||
end
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
open Adam
|
||||
open S2
|
||||
module Sexp = Sexplib0.Sexp
|
||||
|
||||
module Transform_graph : sig
|
||||
type t
|
||||
|
|
|
@ -23,3 +23,11 @@ let load_file path =
|
|||
| n -> read (i + n)
|
||||
in
|
||||
Bytes.sub_string buf 0 (read 0)
|
||||
|
||||
let load_sexp_conv path of_sexp =
|
||||
try
|
||||
load_file path
|
||||
|> Sexplib.Sexp.of_string
|
||||
|> of_sexp
|
||||
with Sexplib.Conv.Of_sexp_error (Failure msg, _) ->
|
||||
raise (Error (path, "parse error: " ^ msg))
|
||||
|
|
|
@ -6,4 +6,5 @@
|
|||
|
||||
ohlog
|
||||
tsdl
|
||||
tgls.tgl4))
|
||||
tgls.tgl4
|
||||
sexplib))
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
module Gl : sig
|
||||
exception Error of string * string
|
||||
end
|
||||
|
||||
module Sdl : sig
|
||||
exception Error of string
|
||||
end
|
||||
|
||||
module Window : sig
|
||||
type t
|
||||
val make : title:string -> t
|
||||
val destroy : t -> unit
|
||||
|
||||
val event_loop : t -> render:(float -> unit) -> unit
|
||||
end
|
||||
|
||||
module Renderer : sig
|
||||
open Adam
|
||||
|
||||
type t
|
||||
val make : window:Window.t -> t
|
||||
val destroy : t -> unit
|
||||
|
||||
val pre_draw : t -> unit
|
||||
val post_draw : t -> unit
|
||||
val clear : t -> color -> unit
|
||||
val draw_rect : t -> tf:mat2a -> bb:aabb -> fill:color -> unit
|
||||
end
|
||||
|
||||
module Asset : sig
|
||||
open Sexplib
|
||||
|
||||
exception Error of string * string
|
||||
|
||||
val load_file : string -> string
|
||||
val load_sexp_conv : string -> (Sexp.t -> 'a) -> 'a
|
||||
end
|
|
@ -19,6 +19,8 @@ let make ~title =
|
|||
let event_loop window ~render =
|
||||
let event = Sdl.Event.create () in
|
||||
let some_event = Some event in
|
||||
let time = ref 0.0 in
|
||||
|
||||
try
|
||||
Sdl.show_window window;
|
||||
while true do
|
||||
|
@ -28,7 +30,11 @@ let event_loop window ~render =
|
|||
| _ ->
|
||||
trace (fun m -> m "uncaught event (%d)" Sdl.Event.(get event typ))
|
||||
done;
|
||||
render ()
|
||||
|
||||
time := Int32.to_float (Sdl.get_ticks ()) /. 1000.0;
|
||||
render !time;
|
||||
done
|
||||
with Quit ->
|
||||
()
|
||||
|
||||
let destroy = Sdl.destroy_window
|
||||
|
|
Loading…
Reference in New Issue