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

View File

@ -2,7 +2,9 @@ open Adam
include (val Ohlog.logs "N2")
module Transform = struct
module Transform_graph = struct
include (val Ohlog.logs "TG")
type t = {
mutable buffer : node array;
mutable size : int;
@ -69,6 +71,45 @@ module Transform = struct
| Node n -> n.model_tf
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
include (val Ohlog.sublogs logger "Entity")