From 4ebf238c5e0e8b22f43b2a7fa842d2a2b45d7555 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 27 Mar 2023 09:10:26 -0700 Subject: [PATCH] mach: use a minimal core App, with a module implementing everything Signed-off-by: Stephen Gutekanst --- src/engine.zig | 64 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/src/engine.zig b/src/engine.zig index f4a55a1b..127f48cb 100644 --- a/src/engine.zig +++ b/src/engine.zig @@ -4,48 +4,68 @@ const gpu = @import("core").gpu; const std = @import("std"); const ecs = @import("ecs"); -/// The Mach engine ECS module. This enables access to `engine.get(.mach, .core)` `*Core` APIs, as -/// to for example `.setOptions(.{.title = "foobar"})`, or to access the GPU device via -/// `engine.get(.mach, .device)` +var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +const allocator = gpa.allocator(); + +/// The main Mach engine ECS module. pub const Module = struct { + _core: Core, core: *Core, device: *gpu.Device, exit: bool, pub const name = .mach; + + pub fn machInit(adapter: anytype) !void { + var mach = adapter.mod(.mach); + + mach.initState(.{ + ._core = undefined, + .core = undefined, + .device = undefined, + .exit = false, + }); + + var state = mach.state(); + try state._core.init(allocator, .{}); + state.core = &state._core; + state.device = state.core.device(); + try adapter.send(.init); + } + + pub fn machDeinit(adapter: anytype) !void { + try adapter.send(.deinit); + + var state = adapter.mod(.mach).state(); + state.core.deinit(); + adapter.deinit(); + _ = gpa.deinit(); + } + + pub fn machExit(adapter: anytype) !void { + try adapter.send(.exit); + + var state = adapter.mod(.mach).state(); + state.exit = true; + } }; -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); - pub fn App(comptime modules: anytype) type { - // TODO: validate modules.mach is the expected type. - return struct { engine: ecs.World(modules), - core: Core, pub fn init(app: *@This()) !void { - try app.core.init(allocator, .{}); - app.* = .{ - .core = app.core, - .engine = try ecs.World(modules).init(allocator), - }; - var mach = app.engine.mod(.mach); - mach.setState(.core, &app.core); - mach.setState(.device, app.core.device()); - try app.engine.send(.init); + app.* = .{ .engine = try ecs.World(modules).init(allocator) }; + try app.engine.send(.machInit); } pub fn deinit(app: *@This()) void { - app.core.deinit(); - app.engine.deinit(); - _ = gpa.deinit(); + try app.engine.send(.machDeinit); } pub fn update(app: *@This()) !bool { try app.engine.send(.tick); - return app.engine.mod(.mach).getState(.exit); + return app.engine.mod(.mach).state().exit; } }; }