From ff3a273416e2bce48ff98f39258104d3462a4c4a Mon Sep 17 00:00:00 2001 From: abux Date: Mon, 27 Jul 2026 22:40:15 +0100 Subject: [PATCH] Lowered cortisol levels by about 69% | Boilerplate be GONE (context) --- src/app.zig | 32 ++++++++++--------- src/example/simple/plugin.zig | 35 ++++++++++++++++++-- src/example/simple/system.zig | 56 ++++++++++++++++---------------- src/plugin/context.zig | 3 -- src/plugin/plugin.zig | 4 +-- src/plugin/root.zig | 1 - src/plugin/storage.zig | 6 ++-- src/scheduler/context.zig | 3 -- src/scheduler/root.zig | 1 - src/scheduler/storage.zig | 60 +++++++++++++++++++++++++++-------- src/scheduler/system.zig | 4 +-- 11 files changed, 131 insertions(+), 74 deletions(-) delete mode 100644 src/plugin/context.zig delete mode 100644 src/scheduler/context.zig diff --git a/src/app.zig b/src/app.zig index 37aaf51..f08344b 100644 --- a/src/app.zig +++ b/src/app.zig @@ -1,3 +1,6 @@ +//! ---------------------------------------------------- +//! ---------------------------------------------------- + // --- LIBS --- const std = @import("std"); const root = @import("root"); @@ -110,24 +113,23 @@ test "Main" { }); // --- BUILD PLUGINS --- - try app.plugins.build(.{ - .app = &app, - }); + try app.plugins.build(&app); } { // SYSTEMS - try app.schedules.runAll(.{ - .app = &app, - }); + // try app.schedules.runAll(&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, - // }); + 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); + + try app.schedules.runReversed( + example.simple.stage.Deinit, + &app, + ); } } diff --git a/src/example/simple/plugin.zig b/src/example/simple/plugin.zig index dfce6df..2e0273c 100644 --- a/src/example/simple/plugin.zig +++ b/src/example/simple/plugin.zig @@ -16,7 +16,7 @@ pub const plugin: App.plugin.Plugin = .{ /// ---------------------------------------------------- fn build( self: *App.plugin.Plugin, - ctx: App.plugin.Context, + app: *App, ) !void { // --- DEBUG --- std.debug.print( @@ -32,11 +32,40 @@ fn build( ); // --- ADD SYSTEMS --- - try ctx.app.schedules.addMany(stage.Init, &.{ + try app.schedules.addMany(stage.Init, &.{ system.test_resources, system.test_events, system.test_ecs, }); - try ctx.app.schedules.addBefore(stage.Init, stage.Init, system.draw); + // --- RUN BEFORE --- + try app.schedules.addBefore( + stage.Init, + stage.Init, + system.draw, + ); + + try app.schedules.addMany(stage.Deinit, &.{ + deinit_one, + deinit_two, + deinit_three, + }); +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +fn deinit_one(_: *App) !void { + std.debug.print("---> One\n", .{}); +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +fn deinit_two(_: *App) !void { + std.debug.print("---> Two\n", .{}); +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +fn deinit_three(_: *App) !void { + std.debug.print("---> Three\n", .{}); } diff --git a/src/example/simple/system.zig b/src/example/simple/system.zig index 9bbcd58..d923f95 100644 --- a/src/example/simple/system.zig +++ b/src/example/simple/system.zig @@ -8,11 +8,11 @@ const component = @import("component.zig"); /// ---------------------------------------------------- /// ---------------------------------------------------- pub fn test_resources( - ctx: App.scheduler.Context, + app: *App, ) !void { // --- SET NEW --- - try ctx.app.resources.set(resource.Server{}); - try ctx.app.resources.set(resource.Client{}); + try app.resources.set(resource.Server{}); + try app.resources.set(resource.Client{}); // --- CONTAINS DEBUG --- std.debug.print( @@ -22,13 +22,13 @@ pub fn test_resources( \\ , .{ - ctx.app.resources.contains(resource.Server), - ctx.app.resources.contains(resource.Client), + app.resources.contains(resource.Server), + app.resources.contains(resource.Client), }, ); // --- GET READONLY RESOURCES --- - _ = try ctx.app.resources.getMany(struct { + _ = try app.resources.getMany(struct { server: ?*const resource.Server, client: ?*const resource.Client, }); @@ -43,7 +43,7 @@ const User = struct { /// ---------------------------------------------------- /// ---------------------------------------------------- pub fn test_events( - ctx: App.scheduler.Context, + app: *App, ) !void { // --- DEBUG --- std.debug.print( @@ -52,25 +52,25 @@ pub fn test_events( ); // --- CLEAR EVENTS --- - defer ctx.app.events.clear(); + defer app.events.clear(); // --- USER LIST --- var users: std.ArrayList(User) = .empty; - defer users.deinit(ctx.app.alloc); + defer users.deinit(app.alloc); // --- ID`S --- var next_register_id: u32 = 0; var next_connected_id: u32 = 0; // --- ADD ADMIN USER --- - try users.append(ctx.app.alloc, .{ + try users.append(app.alloc, .{ .id = next_register_id, .username = "admin", .password = "1234", }); { // NEW USER - try ctx.app.events.sendMany(.{ + try app.events.sendMany(.{ event.Register{ .username = "abux", .password = "qwerty", @@ -83,10 +83,10 @@ pub fn test_events( } // --- REGISTER LOOP --- - if (ctx.app.events.get(event.Register)) |queue| { + if (app.events.get(event.Register)) |queue| { for (queue.items()) |reg| { next_register_id += 1; - try users.append(ctx.app.alloc, .{ + try users.append(app.alloc, .{ .id = next_register_id, .username = reg.username, .password = reg.password, @@ -96,7 +96,7 @@ pub fn test_events( } // --- LOGIN LOOP --- - if (ctx.app.events.get(event.Login)) |queue| { + if (app.events.get(event.Login)) |queue| { for (queue.items()) |login| { for (users.items) |user| { // --- CHECK CREDS --- @@ -105,7 +105,7 @@ pub fn test_events( // --- SEND ACCEPTED CONNECTION --- next_connected_id += 1; - try ctx.app.events.send(event.ConnectionAccepted{ + try app.events.send(event.ConnectionAccepted{ .id = next_connected_id, }); } @@ -114,7 +114,7 @@ pub fn test_events( } // --- CONNECTED --- - if (ctx.app.events.consume(event.ConnectionAccepted)) |ca| { + if (app.events.consume(event.ConnectionAccepted)) |ca| { std.debug.print( \\ | Connected: \\ | ID: {} @@ -150,15 +150,15 @@ pub fn test_events( /// ---------------------------------------------------- /// ---------------------------------------------------- pub fn test_ecs( - ctx: App.scheduler.Context, + app: *App, ) !void { // --- SPAWN ENTITY --- - const player = try ctx.app.world.spawnEntity(.{ + const player = try app.world.spawnEntity(.{ component.Player{ .name = "abux" }, component.Material{}, component.Transform{}, }); - try ctx.app.world.spawn(.{ + try app.world.spawn(.{ component.Material{}, component.Transform{}, }); @@ -170,7 +170,7 @@ pub fn test_ecs( ); // --- QUERY --- - var query = ctx.app.world.query(struct { + var query = app.world.query(struct { player: ?*component.Player, material: *component.Material, transform: *component.Transform, @@ -205,16 +205,16 @@ pub fn test_ecs( std.debug.print("\x1b[0m", .{}); } - if (ctx.app.world.getComponent(component.Dead, player)) |_| { + if (app.world.getComponent(component.Dead, player)) |_| { std.debug.print("------------------>\n", .{}); } - // try ctx.app.world.addComponents(player, .{component.Dead{}}); - try ctx.app.world.addComponents(player, .{ + // try app.world.addComponents(player, .{component.Dead{}}); + try app.world.addComponents(player, .{ component.Dead{}, }); - const cmps = try ctx.app.world.getComponents(struct { + const cmps = try app.world.getComponents(struct { mat: *component.Material, dead: ?*component.Dead, }, player); @@ -225,17 +225,17 @@ pub fn test_ecs( /// ---------------------------------------------------- /// ---------------------------------------------------- pub fn draw( - ctx: App.scheduler.Context, + app: *App, ) !void { - _ = ctx; + _ = app; std.debug.print("----> DRAW\n", .{}); } /// ---------------------------------------------------- /// ---------------------------------------------------- pub fn mainPass( - ctx: App.scheduler.Context, + app: *App, ) !void { - _ = ctx; + _ = app; std.debug.print("----> MAIN_PASS\n", .{}); } diff --git a/src/plugin/context.zig b/src/plugin/context.zig deleted file mode 100644 index d916681..0000000 --- a/src/plugin/context.zig +++ /dev/null @@ -1,3 +0,0 @@ -const App = @import("../app.zig"); - -app: *App, diff --git a/src/plugin/plugin.zig b/src/plugin/plugin.zig index fafb9af..ba33c19 100644 --- a/src/plugin/plugin.zig +++ b/src/plugin/plugin.zig @@ -1,4 +1,4 @@ -const Context = @import("context.zig"); +const App = @import("../app.zig"); const Self = @This(); // @@ -12,4 +12,4 @@ description: []const u8 = "", version: [3]u8 = .{ 0, 0, 0 }, // --- CALLBACKS --- -build: *const fn (*Self, Context) anyerror!void, +build: *const fn (*Self, *App) anyerror!void, diff --git a/src/plugin/root.zig b/src/plugin/root.zig index cfc824a..4c1fd60 100644 --- a/src/plugin/root.zig +++ b/src/plugin/root.zig @@ -1,3 +1,2 @@ pub const Plugin = @import("plugin.zig"); pub const Storage = @import("storage.zig"); -pub const Context = @import("context.zig"); diff --git a/src/plugin/storage.zig b/src/plugin/storage.zig index 76618d8..a81cc0d 100644 --- a/src/plugin/storage.zig +++ b/src/plugin/storage.zig @@ -1,7 +1,7 @@ // --- LIBS --- const std = @import("std"); +const App = @import("../app.zig"); const Plugin = @import("plugin.zig"); -const Context = @import("context.zig"); const Self = @This(); // @@ -53,7 +53,7 @@ pub fn addMany( /// ---------------------------------------------------- pub fn build( self: *Self, - ctx: Context, + app: *App, ) !void { // --- # --- var built: std.StringHashMap(void) = .init(self.alloc); @@ -79,7 +79,7 @@ pub fn build( // --- IF YE, BUILD --- if (ready) { - try plugin.build(plugin, ctx); + try plugin.build(plugin, app); try built.put(plugin.name, {}); progress = true; } diff --git a/src/scheduler/context.zig b/src/scheduler/context.zig deleted file mode 100644 index d916681..0000000 --- a/src/scheduler/context.zig +++ /dev/null @@ -1,3 +0,0 @@ -const App = @import("../app.zig"); - -app: *App, diff --git a/src/scheduler/root.zig b/src/scheduler/root.zig index cdf0dd1..71ff0c2 100644 --- a/src/scheduler/root.zig +++ b/src/scheduler/root.zig @@ -1,3 +1,2 @@ pub const System = @import("system.zig"); pub const Storage = @import("storage.zig"); -pub const Context = @import("context.zig"); diff --git a/src/scheduler/storage.zig b/src/scheduler/storage.zig index 4fc131b..f1d0e3d 100644 --- a/src/scheduler/storage.zig +++ b/src/scheduler/storage.zig @@ -1,7 +1,7 @@ // --- LIBS --- const std = @import("std"); +const App = @import("../app.zig"); const System = @import("system.zig"); -const Context = @import("context.zig"); const TypeID = @import("../template/type_id.zig").of; const Self = @This(); @@ -32,7 +32,7 @@ pub fn deinit(self: *Self) void { pub fn add( self: *Self, comptime T: type, - execute: *const fn (Context) anyerror!void, + execute: *const fn (*App) anyerror!void, ) !void { // --- GET ID --- const stage_id = TypeID(T); @@ -50,7 +50,7 @@ pub fn add( pub fn addMany( self: *Self, comptime T: type, - values: []const *const fn (Context) anyerror!void, + values: []const *const fn (*App) anyerror!void, ) !void { for (values) |exe| { try self.add(T, exe); @@ -63,7 +63,7 @@ pub fn addBefore( self: *Self, comptime Stage: type, comptime Before: type, - execute: *const fn (Context) anyerror!void, + execute: *const fn (*App) anyerror!void, ) !void { const stage_id = TypeID(Stage); const insert_idx = self.firstStageIndex(TypeID(Before)); @@ -81,7 +81,7 @@ pub fn addManyBefore( self: *Self, comptime Stage: type, comptime Before: type, - values: []const *const fn (Context) anyerror!void, + values: []const *const fn (*App) anyerror!void, ) !void { const stage_id = TypeID(Stage); var insert_idx = self.firstStageIndex(TypeID(Before)); @@ -101,7 +101,7 @@ pub fn addAfter( self: *Self, comptime Stage: type, comptime After: type, - execute: *const fn (Context) anyerror!void, + execute: *const fn (*App) anyerror!void, ) !void { const stage_id = TypeID(Stage); const insert_idx = self.lastStageIndex(TypeID(After)); @@ -119,7 +119,7 @@ pub fn addManyAfter( self: *Self, comptime Stage: type, comptime After: type, - values: []const *const fn (Context) anyerror!void, + values: []const *const fn (*App) anyerror!void, ) !void { const stage_id = TypeID(Stage); var insert_idx = self.lastStageIndex(TypeID(After)); @@ -137,10 +137,10 @@ pub fn addManyAfter( /// ---------------------------------------------------- pub fn runAll( self: *Self, - ctx: Context, + app: *App, ) !void { for (self.data.items) |sys| { - try sys.execute(ctx); + try sys.execute(app); } } @@ -149,7 +149,7 @@ pub fn runAll( pub fn run( self: *Self, comptime T: type, - ctx: Context, + app: *App, ) !void { // --- GET ID --- const stage_id = TypeID(T); @@ -157,7 +157,7 @@ pub fn run( // --- RUN --- for (self.data.items) |sys| { if (sys.stage_id != stage_id) continue; - try sys.execute(ctx); + try sys.execute(app); } } @@ -166,10 +166,44 @@ pub fn run( pub fn runMany( self: *Self, comptime Ts: []const type, - ctx: Context, + app: *App, ) !void { inline for (Ts) |T| { - try self.run(T, ctx); + try self.run(T, app); + } +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn runReversed( + self: *Self, + comptime T: type, + app: *App, +) !void { + // --- GET ID --- + const stage_id = TypeID(T); + + // --- RUN --- + var i: usize = self.data.items.len; + while (i > 0) { + i -= 1; + + const sys = self.data.items[i]; + if (sys.stage_id != stage_id) continue; + + try sys.execute(app); + } +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn runManyReversed( + self: *Self, + comptime Ts: []const type, + app: *App, +) !void { + inline for (Ts) |T| { + try self.runReversed(T, app); } } diff --git a/src/scheduler/system.zig b/src/scheduler/system.zig index ad3a8a2..cf63af2 100644 --- a/src/scheduler/system.zig +++ b/src/scheduler/system.zig @@ -1,4 +1,4 @@ -const Context = @import("context.zig"); +const App = @import("../app.zig"); stage_id: u64, -execute: *const fn (ctx: Context) anyerror!void, +execute: *const fn (*App) anyerror!void,