const std = @import("std"); const assert = std.debug.assert; const UUID = @import("../common/uuid.zig").UUID; const mat = @import("../math/mat.zig"); const Mat4f = mat.Mat4f; const Mat3f = mat.Mat3f; const vec = @import("../math/vec.zig"); const Vec4f = vec.Vec4f; const Quat = @import("../math/quat.zig").Quat; const Box3d = @import("../math/box.zig").Box3d; const Mesh = @import("meshes.zig").Mesh; const Material = @import("materials.zig").Material; // TODO memory var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); const Entities = @This(); var s_entities: Entities = undefined; names: std.ArrayList(UUID), meshes: std.ArrayList(Mesh.Id), bounds: std.ArrayList(Box3d), materials: std.ArrayList(Material), matrices: std.ArrayList(Mat4f), inv_matrices: std.ArrayList(Mat3f), translations: std.ArrayList(Vec4f), rotations: std.ArrayList(Quat), scales: std.ArrayList(Vec4f), mod_time: std.time.Instant, pub fn init() void { s_entities.names = std.ArrayList(UUID).init(allocator); s_entities.meshes = std.ArrayList(Mesh.Id).init(allocator); s_entities.bounds = std.ArrayList(Box3d).init(allocator); s_entities.materials = std.ArrayList(Material).init(allocator); s_entities.matrices = std.ArrayList(Mat4f).init(allocator); s_entities.inv_matrices = std.ArrayList(Mat3f).init(allocator); s_entities.translations = std.ArrayList(Vec4f).init(allocator); s_entities.rotations = std.ArrayList(Quat).init(allocator); s_entities.scales = std.ArrayList(Vec4f).init(allocator); s_entities.mod_time = std.time.Instant.now(); } pub fn update() void {} pub fn get() *const Entities { return &s_entities; } pub fn deinit() void { delete(get()); s_entities.names.deinit(); s_entities.meshes.deinit(); s_entities.bounds.deinit(); s_entities.materials.deinit(); s_entities.matrices.deinit(); s_entities.inv_matrices.deinit(); s_entities.translations.deinit(); s_entities.rotations.deinit(); s_entities.scales.deinit(); } pub fn add(self: *const Entities, name: UUID) !i32 { self.names.append(name); self.translations.append(Vec4f.zero); self.scales.append(Vec4f.one); self.rotations.append(Quat.id); self.matrices.append(Mat4f.id); self.inv_matrices.append(Mat3f.id); self.mod_time = std.time.Instant.now(); } pub fn destroyAtIndex(ents: *const Entities, i: i32) void { assert(i >= 0); assert(i < ents.names.len); ents.meshes[i].release(); ents.materials[i].albedo.release(); ents.materials[i].rome.release(); ents.materials[i].normal.release(); } pub fn removeAtIndex(ents: *const Entities, i: i32) void { ents.destroyAtIndex(i); ents.names.swapRemove(i); ents.meshes.swapRemove(i); ents.bounds.swapRemove(i); ents.materials.swapRemove(i); ents.matrices.swapRemove(i); ents.inv_matrices.swapRemove(i); ents.translations.swapRemove(i); ents.rotations.swapRemove(i); ents.scales.swapRemove(i); } pub fn rm(ents: *const Entities, name: UUID) bool { if (ents.find(name)) |i| { ents.removeAtIndex(i); ents.mod_time = std.time.Instant.now(); return true; } return false; } pub fn find(ents: *const Entities, name: UUID) ?i32 { return ents.names.find(name); } pub fn clear(ents: *const Entities) void { for (ents.names) |_, i| { ents.destroyAtIndex(i); } ents.mod_time = std.time.Instant.now(); } pub fn delete(ents: *const Entities) void { ents.clear(); ents.names.clearAndFree(); ents.meshes.clearAndFree(); ents.bounds.clearAndFree(); ents.materials.clearAndFree(); ents.matrices.clearAndFree(); ents.inv_matrices.clearAndFree(); ents.translations.clearAndFree(); ents.rotations.clearAndFree(); ents.scales.clearAndFree(); }