diff --git a/engine/core/src/ecs.zig b/engine/core/src/ecs.zig index fc31bbf..c8c7782 100644 --- a/engine/core/src/ecs.zig +++ b/engine/core/src/ecs.zig @@ -112,16 +112,21 @@ var gEcsRegistry: *EcsRegistry = undefined; // SparseMultiSet (AOS version of sparseSet Really specialized, only used for core engine systems) pub fn createEntity() !Entity { - return .{ .handle = try gEcsRegistry.baseSet.createObject(.{}) }; + const rv = Entity{ .handle = try gEcsRegistry.baseSet.createObject(.{}) }; + core.engine_log("entity created: {d}", .{rv.handle.index}); + return rv; } pub fn destroyEntity(e: Entity) void { + core.engine_log("entity destroyed: {d}", .{e.handle.index}); if (gEcsRegistry.baseSet.get(e.handle)) |entityEntry| { for (entityEntry.containers.items) |ref| { ref.vtable.destroyObject(ref.ptr, e.handle); } entityEntry.containers.deinit(gEcsRegistry.allocator); gEcsRegistry.baseSet.destroyObject(e.handle); + } else { + core.engine_log("UNABLE TO DESTROY ENTITY {d}", .{e.handle.index}); } } @@ -317,6 +322,7 @@ pub const Entity = struct { } pub fn addComponent(self: @This(), comptime Component: type) ?*Component { + core.engine_log("adding component: {d} {s}", .{ self.handle.index, @typeName(Component) }); const rv = Component.BaseContainer.createWithHandleECS(self.handle); const list = &gEcsRegistry.baseSet.get(self.handle).?.containers; diff --git a/engine/core/src/scene.zig b/engine/core/src/scene.zig index ef04fe9..451144f 100644 --- a/engine/core/src/scene.zig +++ b/engine/core/src/scene.zig @@ -128,6 +128,11 @@ pub const Scene = struct { _ = SceneObjectContainer.createWithHandleECS(handle); } + pub fn deinitECS(self: *@This(), handle: core.ObjectHandle) void { + _ = self; + _ = SceneObjectContainer.destroyObject(handle); + } + pub fn printHandleIndex(self: @This()) void { core.engine_log("handle.index = 0x{x} generation = {d} alive={any}", .{ self.handle.index, self.handle.generation, self.handle.alive }); } diff --git a/engine/rend/src/sgpu/mesh-pool.zig b/engine/rend/src/sgpu/mesh-pool.zig index 736c1b8..665abfb 100644 --- a/engine/rend/src/sgpu/mesh-pool.zig +++ b/engine/rend/src/sgpu/mesh-pool.zig @@ -55,9 +55,8 @@ pub const MeshPool = struct { var iter = self.invalidations.iterator(); while (iter.next()) |n| { const invalidate = n.value_ptr; - _ = invalidate; - // self.indexSpans.removeSpan(invalidate.index); - // self.vertexSpans.removeSpan(invalidate.vertex); + self.indexSpans.removeSpan(invalidate.index); + self.vertexSpans.removeSpan(invalidate.vertex); } self.invalidations.clearRetainingCapacity(); diff --git a/extras/bsp/src/maploader.zig b/extras/bsp/src/maploader.zig index 47f2682..8467bf1 100644 --- a/extras/bsp/src/maploader.zig +++ b/extras/bsp/src/maploader.zig @@ -18,6 +18,7 @@ pub const TBMap = struct { pub fn create(allocator: std.mem.Allocator) !*@This() { const self = try allocator.create(@This()); + core.engine_logs("creating map root"); self.* = .{ .allocator = allocator, .root = try core.createEntity(), @@ -131,8 +132,10 @@ pub const TBMap = struct { // physics.gPhysicsRuntime.shapes.get(name.handle()).?.shape.release(); } self.colliderSpecs.deinit(self.allocator); - self.allocator.destroy(self); + self.root.destroy(); + + self.allocator.destroy(self); } }; diff --git a/lib/p2/src/structures/sparse-set.zig b/lib/p2/src/structures/sparse-set.zig index 8309cb7..77ba79e 100644 --- a/lib/p2/src/structures/sparse-set.zig +++ b/lib/p2/src/structures/sparse-set.zig @@ -199,7 +199,7 @@ pub fn SparseMultiSetAdvanced(comptime T: type, comptime SparseSize: u32) type { return self.createObjectInternal(initValue, handle.index, handle.generation); } - pub fn destroyObject(self: *@This(), handle: SetHandle) false { + pub fn destroyObject(self: *@This(), handle: SetHandle) bool { // to destroy an object // get handle and get the dense position, swap and remove. // Then insert the tombstone value into the sparse handle @@ -225,6 +225,8 @@ pub fn SparseMultiSetAdvanced(comptime T: type, comptime SparseSize: u32) type { if (self.containerListener) |l| { l.onHandleRemoved(l.ptr, self.containerID, handle); } + + return true; } var prng = std.Random.DefaultPrng.init(0x1234); @@ -389,6 +391,11 @@ pub fn SparseSetAdvanced(comptime T: type, comptime SparseSize: u32) type { // to destroy an object // get handle and get the dense position, swap and remove. // Then insert the tombstone value into the sparse handle + + if (@hasDecl(T, "deinitECS")) { + self.get(handle).?.deinitECS(handle); + } + const denseIndex = self.sparseToDense(handle) orelse return; const tailDenseIndex = self.dense.items.len - 1; const sparseIndexToSwap = self.dense.items[tailDenseIndex].sparseIndex; @@ -608,6 +615,11 @@ pub fn SparseMap(comptime T: type) type { pub fn destroyObject(self: *@This(), handle: SetHandle) void { std.debug.assert(self.map.contains(handle)); + + if (@hasDecl(T, "deinitECS")) { + self.map.get(handle).?.deinitECS(handle); + } + const alloc = self.allocator(); const index = self.listEntriesByHandle.get(handle).?; diff --git a/projects/sampleGame/main.zig b/projects/sampleGame/main.zig index a414b07..0acdf09 100644 --- a/projects/sampleGame/main.zig +++ b/projects/sampleGame/main.zig @@ -366,6 +366,8 @@ pub fn tick(self: *@This(), dt: f64) void { _ = ig.dockSpaceOverViewport(ig.getMainViewport(), .{ .passthru_central_node = true }, null); + self.loadMap2() catch unreachable; + self.fileWatch -= dt; if (self.fileWatch < 0) { self.fileWatch = 3.0;