High-level wrapper around Flecs, a powerful ECS (Entity Component System) library, written in Zig language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB

const flecs = @import("./main.zig");
const c = flecs.c;
pub fn Iter(comptime ctx: anytype) type {
return struct {
const Self = @This();
const Context = flecs.Context(ctx);
const World = Context.World;
const Entity = Context.Entity;
const Id = Context.Id;
world: *World,
raw: *c.ecs_iter_t,
owned: bool,
pub fn fromRawPtr(world: *World, ptr: *c.ecs_iter_t) Self {
return .{ .world = world, .raw = ptr, .owned = false };
}
pub fn fromRawValue(world: *World, value: c.ecs_iter_t) !Self {
const 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 count(self: Self) usize {
return @intCast(self.raw.count);
}
pub fn deltaTime(self: Self) f32 {
return self.raw.delta_time;
}
pub fn field(self: Self, comptime T: type, index: usize) []T {
const 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.count() else 1;
return typed_ptr[0..count_];
}
pub fn fieldId(self: Self, index: usize) Id {
const raw = c.ecs_field_id(self.raw, @intCast(index));
return Id.fromRaw(self.world, raw);
}
pub fn fieldSource(self: Self, index: usize) Entity {
const raw = c.ecs_field_src(self.raw, @intCast(index));
return Entity.fromRaw(self.world, raw);
}
};
}