ECS WIP: sprite graph

This commit is contained in:
tali 2024-01-19 13:00:43 -05:00
parent 91322da5b1
commit 455b04163a
2 changed files with 71 additions and 33 deletions

View File

@ -1,22 +1,30 @@
open Adam open Adam
open S2 open S2
open N2 module TG = N2.Transform_graph
module SG = N2.Sprite_graph
include (val Ohlog.logs "Main") include (val Ohlog.logs "Main")
let main () = let main () =
let tfs = Transform.make () in let tg = TG.make () in
let sg = SG.make () in
let a = Transform.add tfs in let sq = aabb (-50.0) (-50.0) 50.0 50.0 in
let b = Transform.add tfs ~parent:a in
let c = Transform.add tfs ~parent:a in
let square = aabb (-50.0) (-50.0) 50.0 50.0 in let t_a = TG.add tg in
Mat2A.set (Transform.model b) let t_b = TG.add tg ~parent:t_a in
SG.add sg ~tf:(TG.world t_b)
~bb:sq
~fill:(rgb24 0xff0000);
Mat2A.set (TG.model t_b)
~tx:(-5.0) ~ty:(-5.0) ~sx:1.0 ~sy:1.0; ~tx:(-5.0) ~ty:(-5.0) ~sx:1.0 ~sy:1.0;
Mat2A.set (Transform.model c) let t_c = TG.add tg ~parent:t_a in
SG.add sg ~tf:(TG.world t_c)
~bb:sq
~fill:(rgb24 0x0055ff);
Mat2A.set (TG.model t_c)
~tx:(+5.0) ~ty:(+5.0) ~sx:1.0 ~sy:1.0; ~tx:(+5.0) ~ty:(+5.0) ~sx:1.0 ~sy:1.0;
debug (fun m -> m "initializing"); debug (fun m -> m "initializing");
@ -29,37 +37,26 @@ let main () =
(* Update *) (* Update *)
let time = Sdl.get_ticks () |> Int32.to_int in let time = Sdl.get_ticks () |> Int32.to_int in
let tx = begin
Float_infix.( let tx =
Float.sin (flt time / 1000.0 * 2.0) * 300.0 Float_infix.(
+ 512.0 Float.sin (flt time / 1000.0 * 2.0) * 300.0
) + 512.0
in )
let ty = 400.0 in in
let ty = 400.0 in
Mat2A.set (Transform.model a) Mat2A.set (TG.model t_a) ~tx ~ty ~sx:1.0 ~sy:1.0
~tx ~ty ~sx:1.0 ~sy:1.0; end;
Transform.update tfs;
(* Render *)
Renderer.pre_draw ren; Renderer.pre_draw ren;
Renderer.clear ren (rgb24 0x000000); Renderer.clear ren (rgb24 0x000000);
Renderer.draw_rect ren TG.update tg;
~tf:(Transform.world b) SG.render sg ~ren;
~bb:square
~fill:(rgb24 0xff0000);
Renderer.draw_rect ren
~tf:(Transform.world c)
~bb:square
~fill:(rgb24 0x0055ff);
Renderer.post_draw ren; Renderer.post_draw ren;
Gc.major (); Gc.minor ();
end end
in in
Window.event_loop window ~render; Window.event_loop window ~render;

View File

@ -2,7 +2,9 @@ open Adam
include (val Ohlog.logs "N2") include (val Ohlog.logs "N2")
module Transform = struct module Transform_graph = struct
include (val Ohlog.logs "TG")
type t = { type t = {
mutable buffer : node array; mutable buffer : node array;
mutable size : int; mutable size : int;
@ -69,6 +71,45 @@ module Transform = struct
| Node n -> n.model_tf | Node n -> n.model_tf
end end
module Sprite_graph = struct
include (val Ohlog.logs "SG")
type t = {
mutable list_rev : sprite list;
mutable list : sprite list;
}
and sprite = {
tf : mat2a;
bb : aabb;
fill : color;
(* mutable remove : bool; *)
}
let make () = {
list_rev = [];
list = [];
}
let add t ~tf ~bb ~fill =
let sp = { tf; bb; fill } in
begin
t.list_rev <- sp :: t.list_rev;
t.list <- [];
end
let rec render_rec ren = function
| [] -> ()
| { tf; bb; fill } :: sps ->
S2.Renderer.draw_rect ren ~tf ~bb ~fill;
render_rec ren sps
let render t ~ren =
if t.list = [] then
t.list <- List.rev t.list_rev;
render_rec ren t.list
end
(* (*
module Entity = struct module Entity = struct
include (val Ohlog.sublogs logger "Entity") include (val Ohlog.sublogs logger "Entity")