Split Context.lookup into lookup and lookupMut

main
copygirl 3 months ago
parent 33dfbe8a9e
commit deb1104944
  1. 20
      src/main.zig
  2. 8
      src/test/flecs/world.zig
  3. 4
      src/world.zig

@ -42,13 +42,21 @@ pub fn Context(comptime ctx: anytype) type {
pub const Pair = @import("./pair.zig").Pair(ctx);
pub const World = @import("./world.zig").World(ctx);
/// Returns a pointer unique to this context and the provided type
/// Looks up an entity ID unique to this `Context` for the provided
/// type that has been registered previously, typically done by
/// calling functions such as `World.component(...)`.
pub fn lookup(comptime T: type) c.ecs_entity_t {
return lookupMut(T).*;
}
/// Returns a pointer unique to this `Context` and the provided type
/// that holds an entity ID. Useful for associating types to entities.
pub fn lookup(comptime T: type) *c.ecs_entity_t {
pub fn lookupMut(comptime T: type) *c.ecs_entity_t {
_ = T; // Only necessary to create a unique type.
return &(struct {
const EntityHolder = struct {
pub var id: c.ecs_entity_t = 0;
}).id;
};
return &EntityHolder.id;
}
/// Converts the specified value to an `ecs_entity_t`.
@ -67,7 +75,7 @@ pub fn Context(comptime ctx: anytype) type {
c.ecs_entity_t => value,
Entity => value.raw,
?Entity => if (value) |v| v.raw else 0,
type => lookup(value).*,
type => lookup(value),
else => @compileError("Value of type " ++ @typeName(@TypeOf(value)) ++ " can't be converted to Entity"),
};
}
@ -105,7 +113,7 @@ pub fn Context(comptime ctx: anytype) type {
// c.ecs_entity_t => value,
Entity => value.raw,
?Entity => if (value) |v| v.raw else 0,
type => lookup(value).*,
type => lookup(value),
else => @compileError("Value of type " ++ @typeName(T) ++ " can't be converted to Id"),
};
}

@ -56,8 +56,8 @@ test "World_progress_w_0" {
try expect.equal(null, ctx.param);
try expect.equal(e1.raw, ctx.e[0]);
try expect.equal(Context.lookup(Position).*, ctx.c[0][0]);
try expect.equal(Context.lookup(Velocity).*, ctx.c[0][1]);
try expect.equal(Context.lookup(Position), ctx.c[0][0]);
try expect.equal(Context.lookup(Velocity), ctx.c[0][1]);
try expect.equal(0, ctx.s[0][0]);
try expect.equal(0, ctx.s[0][1]);
@ -93,8 +93,8 @@ test "World_progress_w_t" {
try expect.equal(null, ctx.param);
try expect.equal(e1.raw, ctx.e[0]);
try expect.equal(Context.lookup(Position).*, ctx.c[0][0]);
try expect.equal(Context.lookup(Velocity).*, ctx.c[0][1]);
try expect.equal(Context.lookup(Position), ctx.c[0][0]);
try expect.equal(Context.lookup(Velocity), ctx.c[0][1]);
try expect.equal(0, ctx.s[0][0]);
try expect.equal(0, ctx.s[0][1]);

@ -98,7 +98,7 @@ pub fn World(comptime ctx: anytype) type {
if (@sizeOf(T) > 0) @compileError("'" ++ @typeName(T) ++ "' must be a zero-sized type");
const name = meta.simpleTypeName(T);
const result = try self.entity(.{ .name = name, .symbol = name }, .{});
Context.lookup(T).* = result.raw;
Context.lookupMut(T).* = result.raw;
return result;
}
@ -114,7 +114,7 @@ pub fn World(comptime ctx: anytype) type {
const result = c.ecs_component_init(self.raw, &desc);
if (result == 0) return flecs.getLastErrorOrUnknown();
Context.lookup(T).* = result;
Context.lookupMut(T).* = result;
return Entity.fromRaw(self, result);
}

Loading…
Cancel
Save