diff options
author | Lorenzo Torres <torres@sideros.org> | 2025-03-29 16:15:56 +0100 |
---|---|---|
committer | Lorenzo Torres <torres@sideros.org> | 2025-03-29 16:15:56 +0100 |
commit | 16a2a404189cc6b5cd3493df8d19298af7e542ce (patch) | |
tree | eb53b701149498ef0716922c755524f2a8a42821 /src/ecs | |
parent | fd7973173f163e068deb0ae8f9d6ff0fc31fc71b (diff) |
moved rendering to a system.
To ensure that the rendering system is being run in the main thread, I
added the concept of "Sync systems", so that when a system group is
created it's possible to specify whether it's possible to run it on a
separate thread or not.
Diffstat (limited to 'src/ecs')
-rw-r--r-- | src/ecs/ecs.zig | 7 | ||||
-rw-r--r-- | src/ecs/entities.zig | 24 |
2 files changed, 26 insertions, 5 deletions
diff --git a/src/ecs/ecs.zig b/src/ecs/ecs.zig index e389223..4ec765f 100644 --- a/src/ecs/ecs.zig +++ b/src/ecs/ecs.zig @@ -1,7 +1,12 @@ pub const components = @import("components.zig"); pub const entities = @import("entities.zig"); +pub const SystemError = error{ + fail, + die, +}; + pub const Pool = entities.Pool; pub const Resources = entities.Resources; -pub const System = *const fn (*Pool) void; +pub const System = *const fn (*Pool) anyerror!void; pub const SystemGroup = []const System; diff --git a/src/ecs/entities.zig b/src/ecs/entities.zig index b50055e..fc573ce 100644 --- a/src/ecs/entities.zig +++ b/src/ecs/entities.zig @@ -4,14 +4,17 @@ const components = @import("components.zig"); const sparse = @import("sparse.zig"); const Renderer = @import("renderer"); const Input = @import("sideros").Input; +const ecs = @import("ecs.zig"); -pub const System = *const fn (*Pool) void; +pub const System = ecs.System; pub const SystemGroup = []const System; +pub const SyncGroup = []const System; pub const Resources = struct { window: Renderer.Window, renderer: Renderer, input: Input, + delta_time: f64 = 0.0, }; pub const Human = struct { @@ -24,6 +27,7 @@ pub const Pool = struct { resources: Resources, allocator: Allocator, system_groups: std.ArrayList(SystemGroup), + sync_groups: std.ArrayList(SyncGroup), thread_pool: *std.Thread.Pool, wait_group: std.Thread.WaitGroup, mutex: std.Thread.Mutex, @@ -33,6 +37,7 @@ pub const Pool = struct { .humans = .{}, .resources = resources, .system_groups = std.ArrayList(SystemGroup).init(allocator), + .sync_groups = std.ArrayList(SystemGroup).init(allocator), .thread_pool = try allocator.create(std.Thread.Pool), .wait_group = .{}, .mutex = .{}, @@ -47,8 +52,12 @@ pub const Pool = struct { return pool; } - pub fn addSystemGroup(self: *@This(), group: SystemGroup) !void { - try self.system_groups.append(group); + pub fn addSystemGroup(self: *@This(), group: SystemGroup, sync: bool) !void { + if (sync) { + try self.sync_groups.append(group); + } else { + try self.system_groups.append(group); + } } pub fn deinit(self: *@This()) void { @@ -65,11 +74,18 @@ pub const Pool = struct { fn run(pool: *Pool, index: usize) void { const group = pool.system_groups.items[index]; for (group) |system| { - system(pool); + // TODO: system errors should be correctly handled + system(pool) catch unreachable; } } }.run, .{ self, i }); } + for (0..self.sync_groups.items.len) |i| { + const group = self.sync_groups.items[i]; + for (group) |system| { + system(self) catch unreachable; + } + } } fn getEntities(self: *@This(), T: type) *std.MultiArrayList(T) { |