Don't use zeroes and zeroInit for C structs

main
copygirl 3 months ago
parent 12864ba14a
commit e0bdf5f182
  1. 6
      src/entity.zig
  2. 13
      src/world.zig

@ -130,7 +130,7 @@ pub fn Entity(comptime ctx: anytype) type {
.name => |n| {
const found = c.ecs_lookup_child(world.raw, parent_, n.ptr);
if (found == 0) {
var desc = std.mem.zeroInit(c.ecs_entity_desc_t, .{ .sep = "".ptr, .name = n.ptr });
var desc = c.ecs_entity_desc_t{ .sep = "".ptr, .name = n.ptr };
desc.add[0] = c.ecs_pair(c.EcsChildOf, parent_);
scope = c.ecs_entity_init(world.raw, &desc);
if (scope == 0) return errors.getLastErrorOrUnknown();
@ -145,13 +145,13 @@ pub fn Entity(comptime ctx: anytype) type {
const previous = if (scope) |s| world.setScope(s) else null;
defer _ = if (scope != null) world.setScope(previous);
var desc = std.mem.zeroInit(c.ecs_entity_desc_t, .{
var desc = c.ecs_entity_desc_t{
.sep = "".ptr, // Disable tokenization.
.id = if (id) |i| i else 0,
.name = if (name_) |n| n.ptr else null,
.symbol = if (config.symbol) |s| s.ptr else null,
.use_low_id = config.use_low_id,
});
};
inline for (ids, 0..) |a, i|
desc.add[i] = Context.anyToId(a);

@ -107,12 +107,10 @@ pub fn World(comptime ctx: anytype) type {
const name = meta.simpleTypeName(T);
const entity_ = try self.entity(.{ .name = name, .symbol = name, .use_low_id = true }, .{});
const desc = std.mem.zeroInit(c.ecs_component_desc_t, .{
const result = c.ecs_component_init(self.raw, &.{
.entity = entity_.raw,
.type = .{ .size = @sizeOf(T), .alignment = @alignOf(T) },
});
const result = c.ecs_component_init(self.raw, &desc);
if (result == 0) return errors.getLastErrorOrUnknown();
Context.lookupMut(T).* = result;
return Entity.fromRaw(self, result);
@ -167,15 +165,13 @@ pub fn World(comptime ctx: anytype) type {
self.entity(.{ .name = name }, .{});
const context = try SystemCallbackContext.init(self, callback);
var desc = std.mem.zeroInit(c.ecs_system_desc_t, .{
const result = c.ecs_system_init(self.raw, &.{
.entity = entity_.raw,
.callback = &SystemCallbackContext.invoke,
.binding_ctx = context,
.binding_ctx_free = &SystemCallbackContext.free,
.query = .{ .filter = .{ .expr = expr } },
});
desc.query.filter.expr = expr;
const result = c.ecs_system_init(self.raw, &desc);
if (result == 0) return errors.getLastErrorOrUnknown();
return Entity.fromRaw(self, result);
}
@ -212,8 +208,7 @@ pub fn World(comptime ctx: anytype) type {
/// that have a specific `Id`. This function supports wildcards.
pub fn term(self: *Self, id: anytype) TermIterator {
const id_ = Context.anyToId(id);
var term_ = std.mem.zeroInit(c.ecs_term_t, .{ .id = id_ });
const iter = c.ecs_term_iter(self.raw, &term_);
const iter = c.ecs_term_iter(self.raw, &.{ .id = id_ });
return .{ .world = self, .iter = iter };
}

Loading…
Cancel
Save