Merge pull request #23 from rudedogg/master

Update to build on latest master
master
prime31 3 years ago committed by GitHub
commit a7d224247b

@ -6,7 +6,7 @@ pub fn build(b: *Builder) void {
const build_mode = b.standardReleaseOptions();
// use a different cache folder for macos arm builds
b.cache_root = if (builtin.os.tag == .macos and builtin.arch == builtin.Arch.aarch64) "zig-arm-cache" else "zig-cache";
b.cache_root = if (builtin.os.tag == .macos and builtin.target.cpu.arch == .aarch64) "zig-arm-cache" else "zig-cache";
const examples = [_][2][]const u8{
[_][]const u8{ "view_vs_group", "examples/view_vs_group.zig" },

@ -32,7 +32,7 @@ fn createEntities(reg: *ecs.Registry) void {
}
var end = timer.lap();
std.debug.warn("create {d} entities: {d}\n", .{ total_entities, @intToFloat(f64, end) / 1000000000 });
std.debug.print("create {d} entities: {d}\n", .{ total_entities, @intToFloat(f64, end) / 1000000000 });
}
fn owningGroup(reg: *ecs.Registry) void {
@ -40,7 +40,7 @@ fn owningGroup(reg: *ecs.Registry) void {
// var group_iter = group.iterator(struct { vel: *Velocity, pos: *Position });
// while (group_iter.next()) |e| {
// std.debug.warn("pos.y {d:.3}, ent: {}\n", .{e.pos.y, group_iter.entity()});
// std.debug.print("pos.y {d:.3}, ent: {}\n", .{e.pos.y, group_iter.entity()});
// }
const SortContext = struct {
@ -52,15 +52,15 @@ fn owningGroup(reg: *ecs.Registry) void {
var timer = std.time.Timer.start() catch unreachable;
group.sort(Position, {}, SortContext.sort);
var end = timer.lap();
std.debug.warn("group (sort): {d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("group (sort): {d}\n", .{@intToFloat(f64, end) / 1000000000});
timer.reset();
group.sort(Position, {}, SortContext.sort);
end = timer.lap();
std.debug.warn("group (sort 2): {d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("group (sort 2): {d}\n", .{@intToFloat(f64, end) / 1000000000});
// var group_iter2 = group.iterator(struct { vel: *Velocity, pos: *Position });
// while (group_iter2.next()) |e| {
// std.debug.warn("pos.y {d:.3}, ent: {}\n", .{e.pos.y, group_iter2.entity()});
// std.debug.print("pos.y {d:.3}, ent: {}\n", .{e.pos.y, group_iter2.entity()});
// }
}

@ -25,17 +25,17 @@ pub fn main() !void {
while (iter.next()) |entity| {
var pos = view.get(Position, entity);
const vel = view.getConst(Velocity, entity);
std.debug.warn("entity: {}, pos: {d}, vel: {d}\n", .{ entity, pos.*, vel });
std.debug.print("entity: {}, pos: {d}, vel: {d}\n", .{ entity, pos.*, vel });
pos.*.x += vel.x;
pos.*.y += vel.y;
}
std.debug.warn("---- resetting iter\n", .{});
std.debug.print("---- resetting iter\n", .{});
iter.reset();
while (iter.next()) |entity| {
const pos = view.getConst(Position, entity);
const vel = view.getConst(Velocity, entity);
std.debug.warn("entity: {}, pos: {d}, vel: {d}\n", .{ entity, pos, vel });
std.debug.print("entity: {}, pos: {d}, vel: {d}\n", .{ entity, pos, vel });
}
}

@ -30,11 +30,11 @@ fn createEntities(reg: *ecs.Registry) void {
}
var end = timer.lap();
std.debug.warn("create entities: \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("create entities: \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
}
fn iterateView(reg: *ecs.Registry) void {
std.debug.warn("--- multi-view ---\n", .{});
std.debug.print("--- multi-view ---\n", .{});
var view = reg.view(.{ Velocity, Position }, .{});
var timer = std.time.Timer.start() catch unreachable;
@ -48,15 +48,15 @@ fn iterateView(reg: *ecs.Registry) void {
}
var end = timer.lap();
std.debug.warn("view (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("view (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
}
fn nonOwningGroup(reg: *ecs.Registry) void {
std.debug.warn("--- non-owning ---\n", .{});
std.debug.print("--- non-owning ---\n", .{});
var timer = std.time.Timer.start() catch unreachable;
var group = reg.group(.{}, .{ Velocity, Position }, .{});
var end = timer.lap();
std.debug.warn("group (create): {d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("group (create): {d}\n", .{@intToFloat(f64, end) / 1000000000});
timer.reset();
var group_iter = group.iterator();
@ -69,15 +69,15 @@ fn nonOwningGroup(reg: *ecs.Registry) void {
}
end = timer.lap();
std.debug.warn("group (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("group (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
}
fn owningGroup(reg: *ecs.Registry) void {
std.debug.warn("--- owning ---\n", .{});
std.debug.print("--- owning ---\n", .{});
var timer = std.time.Timer.start() catch unreachable;
var group = reg.group(.{ Velocity, Position }, .{}, .{});
var end = timer.lap();
std.debug.warn("group (create): {d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("group (create): {d}\n", .{@intToFloat(f64, end) / 1000000000});
timer.reset();
var group_iter = group.iterator(struct { vel: *Velocity, pos: *Position });
@ -87,12 +87,12 @@ fn owningGroup(reg: *ecs.Registry) void {
}
end = timer.lap();
std.debug.warn("group (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("group (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
timer.reset();
group.each(each);
end = timer.lap();
std.debug.warn("group (each): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("group (each): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
timer.reset();
@ -110,7 +110,7 @@ fn owningGroup(reg: *ecs.Registry) void {
}
end = timer.lap();
std.debug.warn("group (direct): {d}\n", .{@intToFloat(f64, end) / 1000000000});
std.debug.print("group (direct): {d}\n", .{@intToFloat(f64, end) / 1000000000});
}
fn each(e: struct { vel: *Velocity, pos: *Position }) void {

@ -1,5 +1,4 @@
const std = @import("std");
const warn = std.debug.warn;
const utils = @import("utils.zig");
const SparseSet = @import("sparse_set.zig").SparseSet;
@ -24,7 +23,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type {
set: *SparseSet(Entity),
instances: std.ArrayList(ComponentOrDummy),
allocator: ?*std.mem.Allocator,
allocator: ?std.mem.Allocator,
/// doesnt really belong here...used to denote group ownership
super: usize = 0,
safeDeinit: fn (*Self) void,
@ -34,7 +33,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type {
update: Signal(Entity),
destruction: Signal(Entity),
pub fn init(allocator: *std.mem.Allocator) Self {
pub fn init(allocator: std.mem.Allocator) Self {
var store = Self{
.set = SparseSet(Entity).initPtr(allocator),
.instances = undefined,
@ -73,7 +72,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type {
return store;
}
pub fn initPtr(allocator: *std.mem.Allocator) *Self {
pub fn initPtr(allocator: std.mem.Allocator) *Self {
var store = allocator.create(Self) catch unreachable;
store.set = SparseSet(Entity).initPtr(allocator);
if (!is_empty_struct) {

@ -18,7 +18,7 @@ pub fn Handles(comptime HandleType: type, comptime IndexType: type, comptime Ver
handles: []HandleType,
append_cursor: IndexType = 0,
last_destroyed: ?IndexType = null,
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
const invalid_id = std.math.maxInt(IndexType);
@ -43,11 +43,11 @@ pub fn Handles(comptime HandleType: type, comptime IndexType: type, comptime Ver
}
};
pub fn init(allocator: *std.mem.Allocator) Self {
pub fn init(allocator: std.mem.Allocator) Self {
return initWithCapacity(allocator, 32);
}
pub fn initWithCapacity(allocator: *std.mem.Allocator, capacity: usize) Self {
pub fn initWithCapacity(allocator: std.mem.Allocator, capacity: usize) Self {
return Self{
.handles = allocator.alloc(HandleType, capacity) catch unreachable,
.allocator = allocator,

@ -35,7 +35,7 @@ pub const Registry = struct {
contexts: std.AutoHashMap(u32, usize),
groups: std.ArrayList(*GroupData),
singletons: TypeStore,
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
/// internal, persistant data structure to manage the entities in a group
pub const GroupData = struct {
@ -49,7 +49,7 @@ pub const Registry = struct {
registry: *Registry,
current: usize,
pub fn initPtr(allocator: *std.mem.Allocator, registry: *Registry, hash: u64, owned: []u32, include: []u32, exclude: []u32) *GroupData {
pub fn initPtr(allocator: std.mem.Allocator, registry: *Registry, hash: u64, owned: []u32, include: []u32, exclude: []u32) *GroupData {
// std.debug.assert(std.mem.indexOfAny(u32, owned, include) == null);
// std.debug.assert(std.mem.indexOfAny(u32, owned, exclude) == null);
// std.debug.assert(std.mem.indexOfAny(u32, include, exclude) == null);
@ -59,16 +59,16 @@ pub const Registry = struct {
if (owned.len == 0) {
group_data.entity_set = SparseSet(Entity).init(allocator);
}
group_data.owned = std.mem.dupe(allocator, u32, owned) catch unreachable;
group_data.include = std.mem.dupe(allocator, u32, include) catch unreachable;
group_data.exclude = std.mem.dupe(allocator, u32, exclude) catch unreachable;
group_data.owned = allocator.dupe(u32, owned) catch unreachable;
group_data.include = allocator.dupe(u32, include) catch unreachable;
group_data.exclude = allocator.dupe(u32, exclude) catch unreachable;
group_data.registry = registry;
group_data.current = 0;
return group_data;
}
pub fn deinit(self: *GroupData, allocator: *std.mem.Allocator) void {
pub fn deinit(self: *GroupData, allocator: std.mem.Allocator) void {
// only deinit th SparseSet for non-owning groups
if (self.owned.len == 0) {
self.entity_set.deinit();
@ -184,7 +184,7 @@ pub const Registry = struct {
}
};
pub fn init(allocator: *std.mem.Allocator) Registry {
pub fn init(allocator: std.mem.Allocator) Registry {
return Registry{
.handles = EntityHandles.init(allocator),
.components = std.AutoHashMap(u32, usize).init(allocator),

@ -1,5 +1,4 @@
const std = @import("std");
const warn = std.debug.warn;
const utils = @import("utils.zig");
const registry = @import("registry.zig");
const ReverseSliceIterator = @import("utils.zig").ReverseSliceIterator;
@ -13,9 +12,9 @@ pub fn SparseSet(comptime SparseT: type) type {
sparse: std.ArrayList(?[]SparseT),
dense: std.ArrayList(SparseT),
entity_mask: SparseT,
allocator: ?*std.mem.Allocator,
allocator: ?std.mem.Allocator,
pub fn initPtr(allocator: *std.mem.Allocator) *Self {
pub fn initPtr(allocator: std.mem.Allocator) *Self {
var set = allocator.create(Self) catch unreachable;
set.sparse = std.ArrayList(?[]SparseT).initCapacity(allocator, 16) catch unreachable;
set.dense = std.ArrayList(SparseT).initCapacity(allocator, 16) catch unreachable;
@ -24,7 +23,7 @@ pub fn SparseSet(comptime SparseT: type) type {
return set;
}
pub fn init(allocator: *std.mem.Allocator) Self {
pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.sparse = std.ArrayList(?[]SparseT).init(allocator),
.dense = std.ArrayList(SparseT).init(allocator),
@ -104,8 +103,8 @@ pub fn SparseSet(comptime SparseT: type) type {
pub fn contains(self: Self, sparse: SparseT) bool {
const curr = self.page(sparse);
return curr < self.sparse.items.len and
self.sparse.items[curr] != null and
self.sparse.items[curr].?[self.offset(sparse)] != std.math.maxInt(SparseT);
self.sparse.items[curr] != null and
self.sparse.items[curr].?[self.offset(sparse)] != std.math.maxInt(SparseT);
}
/// Returns the position of an entity in a sparse set
@ -210,16 +209,16 @@ pub fn SparseSet(comptime SparseT: type) type {
}
fn printSet(set: *SparseSet(u32, u8)) void {
std.debug.warn("\nsparse -----\n", .{});
std.debug.print("\nsparse -----\n", .{});
for (set.sparse.items) |sparse| {
std.debug.warn("{}\t", .{sparse});
std.debug.print("{}\t", .{sparse});
}
std.debug.warn("\ndense -----\n", .{});
std.debug.print("\ndense -----\n", .{});
for (set.dense.items) |dense| {
std.debug.warn("{}\t", .{dense});
std.debug.print("{}\t", .{dense});
}
std.debug.warn("\n\n", .{});
std.debug.print("\n\n", .{});
}
test "add/remove/clear" {

@ -4,9 +4,9 @@ const utils = @import("utils.zig");
/// stores a single object of type T for each T added
pub const TypeStore = struct {
map: std.AutoHashMap(u32, []u8),
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
pub fn init(allocator: *std.mem.Allocator) TypeStore {
pub fn init(allocator: std.mem.Allocator) TypeStore {
return TypeStore{
.map = std.AutoHashMap(u32, []u8).init(allocator),
.allocator = allocator,

@ -3,16 +3,14 @@ const std = @import("std");
/// Processes are run by the Scheduler. They use a similar pattern to Allocators in that they are created and
/// added as fields in a parent struct, your actual process that will be run.
pub const Process = struct {
const State = enum(u8) {
uninitialized, running, paused, succeeded, failed, aborted, finished
};
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,
deinit: fn (self: *Process, allocator: std.mem.Allocator) void = undefined,
state: State = .uninitialized,
stopped: bool = false,

@ -12,16 +12,16 @@ const Process = @import("process.zig").Process;
/// - in any callback you can get your oiginal struct back via `process.getParent(@This())`
pub const Scheduler = struct {
processes: std.ArrayList(*Process),
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
/// helper to create and prepare a process
fn createProcessHandler(comptime T: type, data: anytype, allocator: *std.mem.Allocator) *Process {
fn createProcessHandler(comptime T: type, data: anytype, allocator: std.mem.Allocator) *Process {
var proc = allocator.create(T) catch unreachable;
proc.initialize(data);
// get a closure so that we can safely deinit this later
proc.process.deinit = struct {
fn deinit(process: *Process, alloc: *std.mem.Allocator) void {
fn deinit(process: *Process, alloc: std.mem.Allocator) void {
if (process.next) |next_process| {
next_process.deinit(next_process, alloc);
}
@ -35,9 +35,9 @@ pub const Scheduler = struct {
/// returned when appending a process so that sub-processes can be added to the process
const Continuation = struct {
process: *Process,
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
pub fn init(process: *Process, allocator: *std.mem.Allocator) Continuation {
pub fn init(process: *Process, allocator: std.mem.Allocator) Continuation {
return .{ .process = process, .allocator = allocator };
}
@ -48,7 +48,7 @@ pub const Scheduler = struct {
}
};
pub fn init(allocator: *std.mem.Allocator) Scheduler {
pub fn init(allocator: std.mem.Allocator) Scheduler {
return .{
.processes = std.ArrayList(*Process).init(allocator),
.allocator = allocator,
@ -72,7 +72,7 @@ pub const Scheduler = struct {
return Continuation.init(process, self.allocator);
}
fn updateProcess(process: **Process, allocator: *std.mem.Allocator) bool {
fn updateProcess(process: **Process, allocator: std.mem.Allocator) bool {
const current_process = process.*;
current_process.tick();
@ -131,7 +131,7 @@ pub const Scheduler = struct {
};
test "" {
std.debug.warn("\n", .{});
std.debug.print("\n", .{});
const Tester = struct {
process: Process,
@ -150,27 +150,27 @@ test "" {
fn start(process: *Process) void {
_ = process.getParent(@This());
// std.debug.warn("start {}\n", .{self.fart});
// std.debug.print("start {}\n", .{self.fart});
}
fn aborted(process: *Process) void {
_ = process.getParent(@This());
// std.debug.warn("aborted {}\n", .{self.fart});
// std.debug.print("aborted {}\n", .{self.fart});
}
fn failed(process: *Process) void {
_ = process.getParent(@This());
// std.debug.warn("failed {}\n", .{self.fart});
// std.debug.print("failed {}\n", .{self.fart});
}
fn succeeded(process: *Process) void {
_ = process.getParent(@This());
// std.debug.warn("succeeded {}\n", .{self.fart});
// std.debug.print("succeeded {}\n", .{self.fart});
}
fn update(process: *Process) void {
_ = process.getParent(@This());
// std.debug.warn("update {}\n", .{self.fart});
// std.debug.print("update {}\n", .{self.fart});
process.succeed();
}
};

@ -4,9 +4,9 @@ const Cache = @import("cache.zig").Cache;
pub const Assets = struct {
caches: std.AutoHashMap(u32, usize),
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
pub fn init(allocator: *std.mem.Allocator) Assets {
pub fn init(allocator: std.mem.Allocator) Assets {
return Assets{
.caches = std.AutoHashMap(u32, usize).init(allocator),
.allocator = allocator,

@ -10,9 +10,9 @@ pub fn Cache(comptime T: type) type {
safe_deinit: fn (*@This()) void,
resources: std.AutoHashMap(u32, *T),
allocator: ?*std.mem.Allocator = null,
allocator: ?std.mem.Allocator = null,
pub fn initPtr(allocator: *std.mem.Allocator) *@This() {
pub fn initPtr(allocator: std.mem.Allocator) *@This() {
var cache = allocator.create(@This()) catch unreachable;
cache.safe_deinit = struct {
fn deinit(self: *Self) void {
@ -26,7 +26,7 @@ pub fn Cache(comptime T: type) type {
return cache;
}
pub fn init(allocator: *std.mem.Allocator) @This() {
pub fn init(allocator: std.mem.Allocator) @This() {
return .{
.safe_deinit = struct {
fn deinit(self: *Self) void {

@ -5,9 +5,9 @@ const utils = @import("../ecs/utils.zig");
pub const Dispatcher = struct {
signals: std.AutoHashMap(u32, usize),
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
pub fn init(allocator: *std.mem.Allocator) Dispatcher {
pub fn init(allocator: std.mem.Allocator) Dispatcher {
return Dispatcher{
.signals = std.AutoHashMap(u32, usize).init(allocator),
.allocator = allocator,

@ -7,9 +7,9 @@ pub fn Signal(comptime Event: type) type {
const Self = @This();
calls: std.ArrayList(Delegate(Event)),
allocator: ?*std.mem.Allocator = null,
allocator: ?std.mem.Allocator = null,
pub fn init(allocator: *std.mem.Allocator) Self {
pub fn init(allocator: std.mem.Allocator) Self {
// we purposely do not store the allocator locally in this case so we know not to destroy ourself in deint!
return Self{
.calls = std.ArrayList(Delegate(Event)).init(allocator),
@ -17,7 +17,7 @@ pub fn Signal(comptime Event: type) type {
}
/// heap allocates a Signal
pub fn create(allocator: *std.mem.Allocator) *Self {
pub fn create(allocator: std.mem.Allocator) *Self {
var signal = allocator.create(Self) catch unreachable;
signal.calls = std.ArrayList(Delegate(Event)).init(allocator);
signal.allocator = allocator;

@ -1,5 +1,4 @@
const std = @import("std");
const warn = std.debug.warn;
const ecs = @import("ecs");
const Registry = @import("ecs").Registry;
const BasicGroup = @import("ecs").BasicGroup;
@ -14,12 +13,12 @@ const Renderable = struct { x: f32 = 0 };
const Rotation = struct { x: f32 = 0 };
fn printStore(store: anytype, name: []const u8) void {
warn("--- {} ---\n", .{name});
std.debug.print("--- {} ---\n", .{name});
for (store.set.dense.items) |e, i| {
warn("e[{}] s[{}]{}", .{ e, store.set.page(store.set.dense.items[i]), store.set.sparse.items[store.set.page(store.set.dense.items[i])] });
warn(" ({d:.2}) ", .{store.instances.items[i]});
std.debug.print("e[{}] s[{}]{}", .{ e, store.set.page(store.set.dense.items[i]), store.set.sparse.items[store.set.page(store.set.dense.items[i])] });
std.debug.print(" ({d:.2}) ", .{store.instances.items[i]});
}
warn("\n", .{});
std.debug.print("\n", .{});
}
test "sort BasicGroup by Entity" {
@ -239,5 +238,5 @@ test "nested OwningGroups entity order" {
// printStore(sprite_store, "Sprite");
// printStore(transform_store, "Transform");
// warn("group2.current: {}\n", .{group2.group_data.current});
// std.debug.print("group2.current: {}\n", .{group2.group_data.current});
}

Loading…
Cancel
Save