N2.Trans storage optimization

This commit is contained in:
milo 2024-02-05 16:44:36 -05:00
parent a3c2d0dcca
commit 97d708c04e
3 changed files with 19 additions and 28 deletions

View File

@ -26,8 +26,7 @@ module Sprite = struct
Entity.Map.set map en cv;
Stack.push ord
(fun ren ->
let tf = Trans.world en in
Renderer.draw_sprites ren ~tf ~tint:cv.tint
Renderer.draw_sprites ren ~tf:en.tf ~tint:cv.tint
cv.sprite_map cv.sprites)
let clear en =
@ -57,8 +56,7 @@ module Label = struct
Entity.Map.set map en cv;
Stack.push ord
(fun ren ->
let tf = Trans.world en in
Renderer.draw_text ren ~tf ~fg:cv.fg
Renderer.draw_text ren ~tf:en.tf ~fg:cv.fg
cv.font cv.glyphs)
let set_text en text =

View File

@ -3,40 +3,29 @@ open Types
(* include (val Ohlog.sublogs Import.logger "Trans") *)
type cv = {
type rel = {
model : mat2a;
world : mat2a;
parent : mat2a option;
parent : mat2a;
}
let map = Entity.Map.make ()
let ord = Stack.make ()
let model en = (Entity.Map.get map en).model
let world en = (Entity.Map.get map en).world
let model en = Entity.Map.get map en
let[@inline] world en = en.tf
let init ?parent child =
let model = Mat2A.make () in
let cv = match parent with
| Some parent -> {
model;
world = Mat2A.make ();
parent = Some (world parent);
}
| None -> {
model;
world = model;
parent = None;
}
in
Entity.Map.set map child cv;
Stack.push ord child
match parent with
| Some parent ->
let rel = { model = mat2a (); world = child.tf; parent = parent.tf } in
Stack.push ord rel;
Entity.Map.set map child rel.model
| None ->
Entity.Map.set map child child.tf
let update en =
let cv = Entity.Map.get map en in
match cv.parent with
| None -> ((* world == parent *))
| Some parent -> Mat2A.multiply cv.world parent cv.model
let update rel =
Mat2A.multiply rel.world rel.parent rel.model
let update_transforms () =
Stack.iter update ord

View File

@ -5,6 +5,8 @@ open! Import
type en = {
name : string option;
id : int;
(* OPTIMIZATION: since all entities have a transform, store it directly *)
tf : mat2a;
}
module Entity = struct
@ -13,9 +15,11 @@ module Entity = struct
let make ?name () =
let id = !_next_id in
let tf = mat2a () in
incr _next_id; {
name;
id;
tf;
}
let pp ppf en =