NYXA/src/app.zig

156 lines
3.9 KiB
Zig
Raw Normal View History

2026-07-09 22:30:04 +01:00
// --- LIBS ---
const std = @import("std");
const root = @import("root");
pub const event = @import("event/root.zig");
pub const plugin = @import("plugin/root.zig");
pub const resource = @import("resource/root.zig");
pub const scheduler = @import("scheduler/root.zig");
pub const ecs = @import("world/root.zig");
const template = @import("template/root.zig");
2026-07-09 22:30:04 +01:00
const Self = @This();
2026-07-10 16:36:30 +01:00
/// ----------------------------------------------------
/// Default app stages
/// ----------------------------------------------------
2026-07-09 22:30:04 +01:00
pub const stage = struct {
2026-07-10 16:36:30 +01:00
// --- INIT ---
pub const pre_init = struct {};
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 {};
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 {};
2026-07-10 16:36:30 +01:00
// --- INPUTS ---
pub const pre_inputs = struct {};
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 {};
2026-07-09 22:30:04 +01:00
};
/// ----------------------------------------------------
/// ----------------------------------------------------
pub const Options = struct {
seed: u64 = 8490694,
};
pub const options: Options = if (@hasDecl(
root,
"app_options",
)) root.app_options else .{};
//
// FIELDS
//
events: event.Storage,
plugins: plugin.Storage,
resources: resource.Storage,
schedules: scheduler.Storage,
world: ecs.World,
alloc: std.mem.Allocator,
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn init(alloc: std.mem.Allocator) Self {
return .{
.events = .init(alloc),
.plugins = .init(alloc),
.resources = .init(alloc),
.schedules = .init(alloc),
.world = .init(alloc),
.alloc = alloc,
};
}
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn deinit(self: *Self) void {
self.world.deinit();
self.events.deinit();
self.plugins.deinit();
self.resources.deinit();
self.schedules.deinit();
}
//
// EXAMPLES
//
const example = @import("example/root.zig");
// ----------------------------------------------------
// ----------------------------------------------------
test "Main" {
// --- APP ---
var app: Self = .init(std.testing.allocator);
defer app.deinit();
// --- SEND MANY EVENTS ---
try app.events.sendMany(&.{
event.AppExit{
.reason = "Yes",
},
});
// --- MAIN GAME LOOP ---
while (true) {
// --- USE AND EAT EVENT ---
if (app.events.consume(event.AppExit)) |app_exit| {
std.debug.print("| {s}\n", .{
app_exit.reason,
});
break;
}
}
2026-07-09 22:30:04 +01:00
{ // PLUGINS
// --- ADD PLUGINS ---
try app.plugins.addMany(&.{
example.simple.plugin,
});
// --- BUILD PLUGINS ---
try app.plugins.build(.{
.app = &app,
});
}
2026-07-10 22:27:43 +01:00
2026-07-09 22:30:04 +01:00
{ // 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,
}, .{
.app = &app,
});
}
}