Lowered cortisol levels by about 69% | Boilerplate be GONE (context)

This commit is contained in:
abux 2026-07-27 22:40:15 +01:00
parent 55411d8fa7
commit ff3a273416
11 changed files with 131 additions and 74 deletions

View file

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

View file

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

View file

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

View file

@ -1,3 +0,0 @@
const App = @import("../app.zig");
app: *App,

View file

@ -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,

View file

@ -1,3 +1,2 @@
pub const Plugin = @import("plugin.zig");
pub const Storage = @import("storage.zig");
pub const Context = @import("context.zig");

View file

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

View file

@ -1,3 +0,0 @@
const App = @import("../app.zig");
app: *App,

View file

@ -1,3 +1,2 @@
pub const System = @import("system.zig");
pub const Storage = @import("storage.zig");
pub const Context = @import("context.zig");

View file

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

View file

@ -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,