Added [addAfter, addManyAfter] and [addBefore, addManyBefore] | Some cleanup

This commit is contained in:
abux 2026-07-14 15:48:44 +01:00
parent 3dec2867e5
commit 5ab65e1b67
6 changed files with 145 additions and 86 deletions

View file

@ -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 {};
};
```

View file

@ -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,
// });
}
}

View file

@ -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);
}

View file

@ -3,3 +3,5 @@ pub const Deinit = struct {};
pub const Inputs = struct {};
pub const Update = struct {};
pub const Draw = struct {};
pub const MainPass = struct {};

View file

@ -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", .{});
}

View file

@ -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;
}