//! ---------------------------------------------------- //! ---------------------------------------------------- // --- 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"); const Self = @This(); /// ---------------------------------------------------- /// Default app stages /// ---------------------------------------------------- pub const stage = struct { // --- LIFETIME --- pub const init = struct {}; pub const deinit = struct {}; // --- FRAME --- pub const begin_frame = struct {}; pub const end_frame = struct {}; // --- CORE --- pub const inputs = struct {}; pub const fixed_update = struct {}; pub const update = struct {}; pub const draw = struct {}; }; /// ---------------------------------------------------- /// ---------------------------------------------------- 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; } } { // PLUGINS // --- ADD PLUGINS --- try app.plugins.addMany(&.{ example.simple.plugin, }); // --- BUILD PLUGINS --- try app.plugins.build(&app); } { // SYSTEMS // 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); try app.schedules.runReversed( example.simple.stage.Deinit, &app, ); } }