From 5ab65e1b677c9c5fa48b7948135e7ebc6f9beb97 Mon Sep 17 00:00:00 2001 From: abux Date: Tue, 14 Jul 2026 15:48:44 +0100 Subject: [PATCH] Added [addAfter, addManyAfter] and [addBefore, addManyBefore] | Some cleanup --- README.md | 32 +-------- src/app.zig | 50 ++++---------- src/example/simple/plugin.zig | 3 +- src/example/simple/stage.zig | 2 + src/example/simple/system.zig | 18 +++++ src/scheduler/storage.zig | 126 ++++++++++++++++++++++++++++------ 6 files changed, 145 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index 0a875ed..7163114 100644 --- a/README.md +++ b/README.md @@ -108,44 +108,18 @@ test "Main" { **Default App Stages** ``` zig pub const stage = struct { - // --- INIT --- - pub const pre_init = struct {}; + // --- LIFETIME --- pub const init = struct {}; - pub const post_init = struct {}; - - // --- DEINIT --- - pub const pre_deinit = struct {}; pub const deinit = struct {}; - pub const post_deinit = struct {}; - // --- BEGIN FRAME --- - pub const pre_begin_frame = struct {}; + // --- FRAME --- pub const begin_frame = struct {}; - pub const post_begin_frame = struct {}; - - // --- END FRAME --- - pub const pre_end_frame = struct {}; pub const end_frame = struct {}; - pub const post_end_frame = struct {}; - // --- INPUTS --- - pub const pre_inputs = struct {}; + // --- Core --- pub const inputs = struct {}; - pub const post_inputs = struct {}; - - // --- FIXED UPDATE --- - pub const pre_fixed_update = struct {}; pub const fixed_update = struct {}; - pub const post_fixed_update = struct {}; - - // --- UPDATE --- - pub const pre_update = struct {}; pub const update = struct {}; - pub const post_update = struct {}; - - // --- DRAW --- - pub const pre_draw = struct {}; pub const draw = struct {}; - pub const post_draw = struct {}; }; ``` diff --git a/src/app.zig b/src/app.zig index 5cf5512..37d4b03 100644 --- a/src/app.zig +++ b/src/app.zig @@ -13,45 +13,19 @@ const Self = @This(); /// Default app stages /// ---------------------------------------------------- pub const stage = struct { - // --- INIT --- - pub const pre_init = struct {}; + // --- LIFETIME --- pub const init = struct {}; - pub const post_init = struct {}; - - // --- DEINIT --- - pub const pre_deinit = struct {}; pub const deinit = struct {}; - pub const post_deinit = struct {}; - // --- BEGIN FRAME --- - pub const pre_begin_frame = struct {}; + // --- FRAME --- pub const begin_frame = struct {}; - pub const post_begin_frame = struct {}; - - // --- END FRAME --- - pub const pre_end_frame = struct {}; pub const end_frame = struct {}; - pub const post_end_frame = struct {}; - // --- INPUTS --- - pub const pre_inputs = struct {}; + // --- Core --- pub const inputs = struct {}; - pub const post_inputs = struct {}; - - // --- FIXED UPDATE --- - pub const pre_fixed_update = struct {}; pub const fixed_update = struct {}; - pub const post_fixed_update = struct {}; - - // --- UPDATE --- - pub const pre_update = struct {}; pub const update = struct {}; - pub const post_update = struct {}; - - // --- DRAW --- - pub const pre_draw = struct {}; pub const draw = struct {}; - pub const post_draw = struct {}; }; /// ---------------------------------------------------- @@ -142,14 +116,18 @@ test "Main" { } { // SYSTEMS - try app.schedules.runMany(&.{ - example.simple.stage.Init, - example.simple.stage.Inputs, - example.simple.stage.Update, - example.simple.stage.Draw, - example.simple.stage.Deinit, - }, .{ + try app.schedules.runAll(.{ .app = &app, }); + + // try app.schedules.runMany(&.{ + // example.simple.stage.Init, + // example.simple.stage.Inputs, + // example.simple.stage.Update, + // example.simple.stage.Draw, + // example.simple.stage.Deinit, + // }, .{ + // .app = &app, + // }); } } diff --git a/src/example/simple/plugin.zig b/src/example/simple/plugin.zig index 2321900..32eb504 100644 --- a/src/example/simple/plugin.zig +++ b/src/example/simple/plugin.zig @@ -17,7 +17,6 @@ fn build( self: *App.plugin.Plugin, ctx: App.plugin.Context, ) !void { - // --- DEBUG --- std.debug.print( "\x1b[33mPLUGIN\x1b[0m\n" ++ @@ -37,4 +36,6 @@ fn build( system.test_events, system.test_ecs, }); + + try ctx.app.schedules.addBefore(stage.Init, stage.Init, system.draw); } diff --git a/src/example/simple/stage.zig b/src/example/simple/stage.zig index 7136761..ccb11de 100644 --- a/src/example/simple/stage.zig +++ b/src/example/simple/stage.zig @@ -3,3 +3,5 @@ pub const Deinit = struct {}; pub const Inputs = struct {}; pub const Update = struct {}; pub const Draw = struct {}; + +pub const MainPass = struct {}; diff --git a/src/example/simple/system.zig b/src/example/simple/system.zig index d137ca5..9bbcd58 100644 --- a/src/example/simple/system.zig +++ b/src/example/simple/system.zig @@ -221,3 +221,21 @@ pub fn test_ecs( if (cmps.dead) |_| {} } + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn draw( + ctx: App.scheduler.Context, +) !void { + _ = ctx; + std.debug.print("----> DRAW\n", .{}); +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn mainPass( + ctx: App.scheduler.Context, +) !void { + _ = ctx; + std.debug.print("----> MAIN_PASS\n", .{}); +} diff --git a/src/scheduler/storage.zig b/src/scheduler/storage.zig index a054544..4fc131b 100644 --- a/src/scheduler/storage.zig +++ b/src/scheduler/storage.zig @@ -10,7 +10,6 @@ const Self = @This(); // data: std.ArrayList(System), -stages: std.AutoHashMap(u64, usize), alloc: std.mem.Allocator, /// ---------------------------------------------------- @@ -18,7 +17,6 @@ alloc: std.mem.Allocator, pub fn init(alloc: std.mem.Allocator) Self { return .{ .data = .empty, - .stages = .init(alloc), .alloc = alloc, }; } @@ -27,21 +25,6 @@ pub fn init(alloc: std.mem.Allocator) Self { /// ---------------------------------------------------- pub fn deinit(self: *Self) void { self.data.deinit(self.alloc); - self.stages.deinit(); -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn addStage( - self: *Self, - comptime T: type, -) !void { - // --- GET ID --- - const stage_id = TypeID(T); - - // --- INSERT --- - if (self.stages.contains(stage_id)) return; - try self.stages.put(stage_id, 0); } /// ---------------------------------------------------- @@ -60,9 +43,6 @@ pub fn add( .execute = execute, }); errdefer _ = self.data.swapRemove(self.data.items.len - 1); - - // --- NEW STAGE --- - try self.addStage(T); } /// ---------------------------------------------------- @@ -77,6 +57,93 @@ pub fn addMany( } } +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn addBefore( + self: *Self, + comptime Stage: type, + comptime Before: type, + execute: *const fn (Context) anyerror!void, +) !void { + const stage_id = TypeID(Stage); + const insert_idx = self.firstStageIndex(TypeID(Before)); + + try self.data.insert(self.alloc, insert_idx, .{ + .stage_id = stage_id, + .execute = execute, + }); + errdefer _ = self.data.swapRemove(insert_idx); +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn addManyBefore( + self: *Self, + comptime Stage: type, + comptime Before: type, + values: []const *const fn (Context) anyerror!void, +) !void { + const stage_id = TypeID(Stage); + var insert_idx = self.firstStageIndex(TypeID(Before)); + + for (values) |exe| { + try self.data.insert(self.alloc, insert_idx, .{ + .stage_id = stage_id, + .execute = exe, + }); + insert_idx += 1; + } +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn addAfter( + self: *Self, + comptime Stage: type, + comptime After: type, + execute: *const fn (Context) anyerror!void, +) !void { + const stage_id = TypeID(Stage); + const insert_idx = self.lastStageIndex(TypeID(After)); + + try self.data.insert(self.alloc, insert_idx, .{ + .stage_id = stage_id, + .execute = execute, + }); + errdefer _ = self.data.swapRemove(insert_idx); +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn addManyAfter( + self: *Self, + comptime Stage: type, + comptime After: type, + values: []const *const fn (Context) anyerror!void, +) !void { + const stage_id = TypeID(Stage); + var insert_idx = self.lastStageIndex(TypeID(After)); + + for (values) |exe| { + try self.data.insert(self.alloc, insert_idx, .{ + .stage_id = stage_id, + .execute = exe, + }); + insert_idx += 1; + } +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn runAll( + self: *Self, + ctx: Context, +) !void { + for (self.data.items) |sys| { + try sys.execute(ctx); + } +} + /// ---------------------------------------------------- /// ---------------------------------------------------- pub fn run( @@ -105,3 +172,22 @@ pub fn runMany( try self.run(T, ctx); } } + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +fn firstStageIndex(self: *Self, stage_id: u64) usize { + for (self.data.items, 0..) |sys, i| { + if (sys.stage_id == stage_id) return i; + } + return self.data.items.len; +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +fn lastStageIndex(self: *Self, stage_id: u64) usize { + var last: usize = self.data.items.len; + for (self.data.items, 0..) |sys, i| { + if (sys.stage_id == stage_id) last = i + 1; + } + return last; +}