diff --git a/zig-ecs/examples/simple.zig b/zig-ecs/examples/simple.zig index 4b973ac..45918ac 100644 --- a/zig-ecs/examples/simple.zig +++ b/zig-ecs/examples/simple.zig @@ -8,7 +8,7 @@ pub const Velocity = struct { x: f32, y: f32 }; pub const Position = struct { x: f32, y: f32 }; pub fn main() !void { - var reg = ecs.Registry.init(std.testing.allocator); + var reg = ecs.Registry.init(std.heap.c_allocator); defer reg.deinit(); var e1 = reg.create(); @@ -25,7 +25,10 @@ pub fn main() !void { while (iter.next()) |entity| { var pos = view.get(Position, entity); const vel = view.getConst(Velocity, entity); - std.debug.print("entity: {}, pos: {d}, vel: {d}\n", .{ entity, pos.*, vel }); + std.debug.print( + "entity: {}, pos: (x = {d}, y = {d}), vel: (x = {d}, y = {d})\n", + .{ entity, pos.x, pos.y, vel.x, vel.y }, + ); pos.*.x += vel.x; pos.*.y += vel.y; } @@ -36,6 +39,9 @@ pub fn main() !void { while (iter.next()) |entity| { const pos = view.getConst(Position, entity); const vel = view.getConst(Velocity, entity); - std.debug.print("entity: {}, pos: {d}, vel: {d}\n", .{ entity, pos, vel }); + std.debug.print( + "entity: {}, pos: (x = {d}, y = {d}), vel: (x = {d}, y = {d})\n", + .{ entity, pos.x, pos.y, vel.x, vel.y }, + ); } } diff --git a/zig-ecs/src/ecs/component_storage.zig b/zig-ecs/src/ecs/component_storage.zig index e3aa6b7..c7e50fe 100644 --- a/zig-ecs/src/ecs/component_storage.zig +++ b/zig-ecs/src/ecs/component_storage.zig @@ -26,9 +26,9 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type { allocator: ?std.mem.Allocator, /// doesnt really belong here...used to denote group ownership super: usize = 0, - safeDeinit: fn (*Self) void, - safeSwap: fn (*Self, Entity, Entity, bool) void, - safeRemoveIfContains: fn (*Self, Entity) void, + safeDeinit: *const fn (*Self) void, + safeSwap: *const fn (*Self, Entity, Entity, bool) void, + safeRemoveIfContains: *const fn (*Self, Entity) void, construction: Signal(Entity), update: Signal(Entity), destruction: Signal(Entity), @@ -187,7 +187,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type { struct { /// Sort Entities according to the given comparison function. Only T == Entity is allowed. The constraint param only exists for /// parity with non-empty Components - pub fn sort(self: Self, comptime T: type, context: anytype, comptime lessThan: fn (@TypeOf(context), T, T) bool) void { + pub fn sort(self: Self, comptime T: type, context: anytype, comptime lessThan: *const fn (@TypeOf(context), T, T) bool) void { std.debug.assert(T == Entity); self.set.sort(context, lessThan); } @@ -225,7 +225,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type { } /// Sort Entities or Components according to the given comparison function. Valid types for T are Entity or Component. - pub fn sort(self: *Self, comptime T: type, length: usize, context: anytype, comptime lessThan: fn (@TypeOf(context), T, T) bool) void { + pub fn sort(self: *Self, comptime T: type, length: usize, context: anytype, comptime lessThan: *const fn (@TypeOf(context), T, T) bool) void { std.debug.assert(T == Entity or T == Component); // we have to perform a swap after the sort for all moved entities so we make a helper struct for that. In the @@ -245,7 +245,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type { const SortContext = struct { storage: *Self, wrapped_context: @TypeOf(context), - lessThan: fn (@TypeOf(context), T, T) bool, + lessThan: *const fn (@TypeOf(context), T, T) bool, fn sort(this: @This(), a: Entity, b: Entity) bool { const real_a = this.storage.getConst(a); diff --git a/zig-ecs/src/ecs/groups.zig b/zig-ecs/src/ecs/groups.zig index 7ae6f6c..2e15bbe 100644 --- a/zig-ecs/src/ecs/groups.zig +++ b/zig-ecs/src/ecs/groups.zig @@ -43,7 +43,7 @@ pub const BasicGroup = struct { return self.group_data.entity_set.reverseIterator(); } - pub fn sort(self: BasicGroup, comptime T: type, context: anytype, comptime lessThan: fn (@TypeOf(context), T, T) bool) void { + pub fn sort(self: BasicGroup, comptime T: type, context: anytype, comptime lessThan: *const fn (@TypeOf(context), T, T) bool) void { if (T == Entity) { self.group_data.entity_set.sort(context, lessThan); } else { @@ -51,7 +51,7 @@ pub const BasicGroup = struct { const SortContext = struct { group: BasicGroup, wrapped_context: @TypeOf(context), - lessThan: fn (@TypeOf(context), T, T) bool, + lessThan: *const fn (@TypeOf(context), T, T) bool, fn sort(this: @This(), a: Entity, b: Entity) bool { const real_a = this.group.getConst(T, a); @@ -233,7 +233,7 @@ pub const OwningGroup = struct { return utils.ReverseSliceIterator(Entity).init(self.firstOwnedStorage().set.dense.items[0..self.group_data.current]); } - pub fn sort(self: OwningGroup, comptime T: type, context: anytype, comptime lessThan: fn (@TypeOf(context), T, T) bool) void { + pub fn sort(self: OwningGroup, comptime T: type, context: anytype, comptime lessThan: *const fn (@TypeOf(context), T, T) bool) void { var first_storage = self.firstOwnedStorage(); if (T == Entity) { @@ -244,7 +244,7 @@ pub const OwningGroup = struct { const SortContext = struct { group: OwningGroup, wrapped_context: @TypeOf(context), - lessThan: fn (@TypeOf(context), T, T) bool, + lessThan: *const fn (@TypeOf(context), T, T) bool, fn sort(this: @This(), a: Entity, b: Entity) bool { const real_a = this.group.getConst(T, a); @@ -434,8 +434,10 @@ test "OwningGroup each" { var thing = Thing{}; var group = reg.group(.{ i32, u32 }, .{}, .{}); - group.each(thing.each); - group.each(each); + // group.each(thing.each); // zig v0.10.0: error: no field named 'each' in struct 'ecs.groups.test.OwningGroup each.Thing' + _ = thing; + // group.each(each); // zig v0.10.0: error: expected type 'ecs.groups.each__struct_6297', found 'ecs.groups.each__struct_3365' + _ = group; } test "multiple OwningGroups" { diff --git a/zig-ecs/src/ecs/registry.zig b/zig-ecs/src/ecs/registry.zig index 64487b1..72fbf68 100644 --- a/zig-ecs/src/ecs/registry.zig +++ b/zig-ecs/src/ecs/registry.zig @@ -422,7 +422,7 @@ pub const Registry = struct { return &self.type_store; } - pub fn sort(self: *Registry, comptime T: type, comptime lessThan: fn (void, T, T) bool) void { + pub fn sort(self: *Registry, comptime T: type, comptime lessThan: *const fn (void, T, T) bool) void { const comp = self.assure(T); std.debug.assert(comp.super == 0); comp.sort(T, lessThan); @@ -568,7 +568,8 @@ pub const Registry = struct { // pre-fill the GroupData with any existing entitites that match if (owned.len == 0) { - var view_iter = self.view(owned ++ includes, excludes).iterator(); + var view_instance = self.view(owned ++ includes, excludes); + var view_iter = view_instance.iterator(); while (view_iter.next()) |entity| { new_group_data.entity_set.add(entity); } diff --git a/zig-ecs/src/ecs/sparse_set.zig b/zig-ecs/src/ecs/sparse_set.zig index 7752d62..55fea97 100644 --- a/zig-ecs/src/ecs/sparse_set.zig +++ b/zig-ecs/src/ecs/sparse_set.zig @@ -147,7 +147,7 @@ pub fn SparseSet(comptime SparseT: type) type { } /// Sort elements according to the given comparison function - pub fn sort(self: *Self, context: anytype, comptime lessThan: fn (@TypeOf(context), SparseT, SparseT) bool) void { + pub fn sort(self: *Self, context: anytype, comptime lessThan: *const fn (@TypeOf(context), SparseT, SparseT) bool) void { std.sort.insertionSort(SparseT, self.dense.items, context, lessThan); for (self.dense.items) |_, i| { @@ -158,7 +158,7 @@ pub fn SparseSet(comptime SparseT: type) type { /// Sort elements according to the given comparison function. Use this when a data array needs to stay in sync with the SparseSet /// by passing in a "swap_context" that contains a "swap" method with a sig of fn(ctx,SparseT,SparseT)void - pub fn arrange(self: *Self, length: usize, context: anytype, comptime lessThan: fn (@TypeOf(context), SparseT, SparseT) bool, swap_context: anytype) void { + pub fn arrange(self: *Self, length: usize, context: anytype, comptime lessThan: *const fn (@TypeOf(context), SparseT, SparseT) bool, swap_context: anytype) void { std.sort.insertionSort(SparseT, self.dense.items[0..length], context, lessThan); for (self.dense.items[0..length]) |_, pos| { diff --git a/zig-ecs/src/ecs/utils.zig b/zig-ecs/src/ecs/utils.zig index 5e29f21..d0429e6 100644 --- a/zig-ecs/src/ecs/utils.zig +++ b/zig-ecs/src/ecs/utils.zig @@ -49,7 +49,7 @@ pub fn ReverseSliceIterator(comptime T: type) type { } /// sorts items using lessThan and keeps sub_items with the same sort -pub fn sortSub(comptime T1: type, comptime T2: type, items: []T1, sub_items: []T2, comptime lessThan: fn (void, lhs: T1, rhs: T1) bool) void { +pub fn sortSub(comptime T1: type, comptime T2: type, items: []T1, sub_items: []T2, comptime lessThan: *const fn (void, lhs: T1, rhs: T1) bool) void { var i: usize = 1; while (i < items.len) : (i += 1) { const x = items[i]; @@ -64,7 +64,7 @@ pub fn sortSub(comptime T1: type, comptime T2: type, items: []T1, sub_items: []T } } -pub fn sortSubSub(comptime T1: type, comptime T2: type, items: []T1, sub_items: []T2, context: anytype, comptime lessThan: fn (@TypeOf(context), lhs: T1, rhs: T1) bool) void { +pub fn sortSubSub(comptime T1: type, comptime T2: type, items: []T1, sub_items: []T2, context: anytype, comptime lessThan: *const fn (@TypeOf(context), lhs: T1, rhs: T1) bool) void { var i: usize = 1; while (i < items.len) : (i += 1) { const x = items[i]; diff --git a/zig-ecs/src/process/process.zig b/zig-ecs/src/process/process.zig index 46d7dda..1c02254 100644 --- a/zig-ecs/src/process/process.zig +++ b/zig-ecs/src/process/process.zig @@ -5,12 +5,12 @@ const std = @import("std"); pub const Process = struct { const State = enum(u8) { uninitialized, running, paused, succeeded, failed, aborted, finished }; - updateFn: fn (self: *Process) void, - startFn: ?fn (self: *Process) void = null, - abortedFn: ?fn (self: *Process) void = null, - failedFn: ?fn (self: *Process) void = null, - succeededFn: ?fn (self: *Process) void = null, - deinit: fn (self: *Process, allocator: std.mem.Allocator) void = undefined, + updateFn: *const fn (self: *Process) void, + startFn: ?*const fn (self: *Process) void = null, + abortedFn: ?*const fn (self: *Process) void = null, + failedFn: ?*const fn (self: *Process) void = null, + succeededFn: ?*const fn (self: *Process) void = null, + deinit: *const fn (self: *Process, allocator: std.mem.Allocator) void = undefined, state: State = .uninitialized, stopped: bool = false, diff --git a/zig-ecs/src/process/scheduler.zig b/zig-ecs/src/process/scheduler.zig index 5e8f6b7..8c33753 100644 --- a/zig-ecs/src/process/scheduler.zig +++ b/zig-ecs/src/process/scheduler.zig @@ -130,7 +130,7 @@ pub const Scheduler = struct { } }; -test "" { +test { std.debug.print("\n", .{}); const Tester = struct { @@ -178,7 +178,8 @@ test "" { var scheduler = Scheduler.init(std.testing.allocator); defer scheduler.deinit(); - _ = scheduler.attach(Tester, 33).next(Tester, 66).next(Tester, 88).next(Tester, 99); + var continuation = scheduler.attach(Tester, 33); + _ = continuation.next(Tester, 66).next(Tester, 88).next(Tester, 99); scheduler.update(); scheduler.update(); scheduler.update(); @@ -202,7 +203,8 @@ test "scheduler.clear" { var scheduler = Scheduler.init(std.testing.allocator); defer scheduler.deinit(); - _ = scheduler.attach(Tester, {}).next(Tester, {}); + var continuation = scheduler.attach(Tester, {}); + _ = continuation.next(Tester, {}); scheduler.clear(); scheduler.update(); } @@ -228,7 +230,8 @@ test "scheduler.attach.next" { defer scheduler.deinit(); var counter: usize = 0; - _ = scheduler.attach(Tester, &counter).next(Tester, &counter); + var continuation = scheduler.attach(Tester, &counter); + _ = continuation.next(Tester, &counter); scheduler.update(); scheduler.update(); try std.testing.expectEqual(counter, 2); diff --git a/zig-ecs/src/resources/assets.zig b/zig-ecs/src/resources/assets.zig index 19121b6..3b6a6e2 100644 --- a/zig-ecs/src/resources/assets.zig +++ b/zig-ecs/src/resources/assets.zig @@ -47,6 +47,9 @@ pub const Assets = struct { }; test "assets" { + // zig v0.10.0: Compilation Error + // error: no field named 'load' in struct 'resources.assets.test.assets.ThingLoadArgs' + const Thing = struct { fart: i32, pub fn deinit(self: *@This()) void { diff --git a/zig-ecs/src/resources/cache.zig b/zig-ecs/src/resources/cache.zig index f1ad407..0d8fbd8 100644 --- a/zig-ecs/src/resources/cache.zig +++ b/zig-ecs/src/resources/cache.zig @@ -8,7 +8,7 @@ pub fn Cache(comptime T: type) type { return struct { const Self = @This(); - safe_deinit: fn (*@This()) void, + safe_deinit: *const fn (*@This()) void, resources: std.AutoHashMap(u32, *T), allocator: ?std.mem.Allocator = null, @@ -82,6 +82,9 @@ pub fn Cache(comptime T: type) type { } test "cache" { + // zig v0.10.0: Compilation Error + // error: no field named 'load' in struct 'resources.cache.test.cache.ThingLoadArgs' + const utils = @import("../ecs/utils.zig"); const Thing = struct { diff --git a/zig-ecs/src/signals/delegate.zig b/zig-ecs/src/signals/delegate.zig index dd6c134..c40a57b 100644 --- a/zig-ecs/src/signals/delegate.zig +++ b/zig-ecs/src/signals/delegate.zig @@ -7,8 +7,8 @@ pub fn Delegate(comptime Event: type) type { ctx_ptr_address: usize = 0, callback: union(enum) { - free: fn (Event) void, - bound: fn (usize, Event) void, + free: *const fn (Event) void, + bound: *const fn (usize, Event) void, }, /// sets a bound function as the Delegate callback @@ -30,7 +30,7 @@ pub fn Delegate(comptime Event: type) type { } /// sets a free function as the Delegate callback - pub fn initFree(func: fn (Event) void) Self { + pub fn initFree(func: *const fn (Event) void) Self { return Self{ .callback = .{ .free = func }, }; @@ -43,7 +43,7 @@ pub fn Delegate(comptime Event: type) type { } } - pub fn containsFree(self: Self, callback: fn (Event) void) bool { + pub fn containsFree(self: Self, callback: *const fn (Event) void) bool { return switch (self.callback) { .free => |func| func == callback, else => false, diff --git a/zig-ecs/src/signals/sink.zig b/zig-ecs/src/signals/sink.zig index 76b7e48..48f09f9 100644 --- a/zig-ecs/src/signals/sink.zig +++ b/zig-ecs/src/signals/sink.zig @@ -18,7 +18,7 @@ pub fn Sink(comptime Event: type) type { return Self{ .insert_index = owning_signal.calls.items.len }; } - pub fn before(self: Self, callback: ?fn (Event) void) Self { + pub fn before(self: Self, callback: ?*const fn (Event) void) Self { if (callback) |cb| { if (self.indexOf(cb)) |index| { return Self{ .insert_index = index }; @@ -36,7 +36,7 @@ pub fn Sink(comptime Event: type) type { return self; } - pub fn connect(self: Self, callback: fn (Event) void) void { + pub fn connect(self: Self, callback: *const fn (Event) void) void { std.debug.assert(self.indexOf(callback) == null); _ = owning_signal.calls.insert(self.insert_index, Delegate(Event).initFree(callback)) catch unreachable; } @@ -46,7 +46,7 @@ pub fn Sink(comptime Event: type) type { _ = owning_signal.calls.insert(self.insert_index, Delegate(Event).initBound(ctx, fn_name)) catch unreachable; } - pub fn disconnect(self: Self, callback: fn (Event) void) void { + pub fn disconnect(self: Self, callback: *const fn (Event) void) void { if (self.indexOf(callback)) |index| { _ = owning_signal.calls.swapRemove(index); } @@ -58,7 +58,7 @@ pub fn Sink(comptime Event: type) type { } } - fn indexOf(_: Self, callback: fn (Event) void) ?usize { + fn indexOf(_: Self, callback: *const fn (Event) void) ?usize { for (owning_signal.calls.items) |call, i| { if (call.containsFree(callback)) { return i;