const flecs = @import("./main.zig"); const c = flecs.c; const Entity = @import("./entity.zig").Entity; const Id = @import("./id.zig").Id; const World = @import("./world.zig").World; pub fn Iter(comptime ctx: anytype) type { return struct { world: *World(ctx), raw: *c.ecs_iter_t, owned: bool, const Self = @This(); pub fn fromRawPtr(world: *World(ctx), ptr: *c.ecs_iter_t) Self { return .{ .world = world, .raw = ptr, .owned = false }; } pub fn fromRawValue(world: *World(ctx), value: c.ecs_iter_t) !Self { var raw = try flecs.allocator.create(c.ecs_iter_t); raw.* = value; return .{ .world = world, .raw = raw, .owned = true }; } pub fn deinit(self: Self) void { if (self.isValid()) c.ecs_iter_fini(self.raw); if (self.owned) flecs.allocator.destroy(self.raw); } pub fn isValid(self: Self) bool { return (self.raw.flags & c.EcsIterIsValid) != 0; } pub fn getCount(self: Self) usize { return @intCast(self.raw.count); } pub fn getDeltaTime(self: Self) f32 { return self.raw.delta_time; } pub fn field(self: Self, comptime T: type, index: usize) []T { var raw_ptr = c.ecs_field_w_size(self.raw, @sizeOf(T), @intCast(index)); var typed_ptr: [*]T = @alignCast(@ptrCast(raw_ptr)); const is_self = c.ecs_field_is_self(self.raw, @intCast(index)); const count = if (is_self) self.getCount() else 1; return typed_ptr[0..count]; } pub fn fieldId(self: Self, index: usize) Id(ctx) { const raw = c.ecs_field_id(self.raw, @intCast(index)); return Id(ctx).fromRaw(self.world, raw); } pub fn fieldSource(self: Self, index: usize) Entity(ctx) { const raw = c.ecs_field_src(self.raw, @intCast(index)); return Entity(ctx).fromRaw(self.world, raw); } }; }