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

View File

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

View File

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