Don't use zeroes and zeroInit for C structs

main
copygirl 9 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| { .name => |n| {
const found = c.ecs_lookup_child(world.raw, parent_, n.ptr); const found = c.ecs_lookup_child(world.raw, parent_, n.ptr);
if (found == 0) { 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_); desc.add[0] = c.ecs_pair(c.EcsChildOf, parent_);
scope = c.ecs_entity_init(world.raw, &desc); scope = c.ecs_entity_init(world.raw, &desc);
if (scope == 0) return errors.getLastErrorOrUnknown(); 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; const previous = if (scope) |s| world.setScope(s) else null;
defer _ = if (scope != null) world.setScope(previous); 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. .sep = "".ptr, // Disable tokenization.
.id = if (id) |i| i else 0, .id = if (id) |i| i else 0,
.name = if (name_) |n| n.ptr else null, .name = if (name_) |n| n.ptr else null,
.symbol = if (config.symbol) |s| s.ptr else null, .symbol = if (config.symbol) |s| s.ptr else null,
.use_low_id = config.use_low_id, .use_low_id = config.use_low_id,
}); };
inline for (ids, 0..) |a, i| inline for (ids, 0..) |a, i|
desc.add[i] = Context.anyToId(a); desc.add[i] = Context.anyToId(a);

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

Loading…
Cancel
Save