diff --git a/zig-ecs/src/ecs/groups.zig b/zig-ecs/src/ecs/groups.zig index a630db4..2c61038 100644 --- a/zig-ecs/src/ecs/groups.zig +++ b/zig-ecs/src/ecs/groups.zig @@ -179,8 +179,33 @@ pub const OwningGroup = struct { else => std.debug.assert("invalid func"), }; - var iter = self.iterator(Components); - while (iter.next()) |comps| { + // optionally we could just use an Interator here + // var iter = self.iterator(Components); + // while (iter.next()) |comps| { + // @call(.{ .modifier = .always_inline }, func, .{comps}); + // } + + const component_info = @typeInfo(Components).Struct; + + // get the data pointers for the chunks + var component_ptrs: [component_info.fields.len][*]u8 = undefined; + inline for (component_info.fields) |field, i| { + const storage = self.registry.assure(field.field_type.Child); + component_ptrs[i] = @ptrCast([*]u8, storage.instances.items.ptr); + } + + var storage = self.firstOwnedStorage(); + var index: usize = 0; + while (index < self.group_data.current) : (index += 1) { + const ent = storage.set.dense.items[index]; + const entity_index = storage.set.index(ent); + + var comps: Components = undefined; + inline for (component_info.fields) |field, i| { + const typed_ptr = @ptrCast([*]field.field_type.Child, @alignCast(@alignOf(field.field_type.Child), component_ptrs[i])); + @field(comps, field.name) = &typed_ptr[entity_index]; + } + @call(.{ .modifier = .always_inline }, func, .{comps}); } }