From 6ec27861b4df113b5392d03738612a3a1e2998bf Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 4 Jul 2022 22:17:58 -0700 Subject: [PATCH] mach: initial support for high-level ECS applications Adds experimental support for high-level ECS-based applications following hexops/mach#349 Signed-off-by: Stephen Gutekanst --- src/engine.zig | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 6 ++++++ 2 files changed, 56 insertions(+) create mode 100644 src/engine.zig diff --git a/src/engine.zig b/src/engine.zig new file mode 100644 index 00000000..b146b7f7 --- /dev/null +++ b/src/engine.zig @@ -0,0 +1,50 @@ +pub const Core = @import("Core.zig"); +pub const gpu = @import("gpu"); +pub 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)` +pub const module = ecs.Module(.{ + .globals = struct { + core: *Core, + device: gpu.Device, + }, +}); + +pub fn App( + modules: anytype, + init: anytype, // fn (engine: *ecs.World(modules)) !void +) type { + // TODO: validate modules.mach is the expected type. + // TODO: validate init has the right function signature + + return struct { + engine: ecs.World(modules), + + pub fn init(app: *@This(), core: *Core) !void { + app.* = .{ + .engine = try ecs.World(modules).init(core.allocator), + }; + app.*.engine.set(.mach, .core, core); + app.*.engine.set(.mach, .device, core.device); + try init(&app.engine); + } + + pub fn deinit(app: *@This(), _: *Core) void { + app.engine.deinit(); + } + + pub fn update(app: *@This(), _: *Core) !void { + app.engine.tick(); + } + + pub fn resize(app: *@This(), core: *Core, width: u32, height: u32) !void { + _ = app; + _ = core; + _ = width; + _ = height; + // TODO: send resize messages to ECS modules + } + }; +} diff --git a/src/main.zig b/src/main.zig index 9cd5e332..12880dab 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,3 +3,9 @@ pub usingnamespace @import("enums.zig"); pub const Core = @import("Core.zig"); pub const Timer = @import("Timer.zig"); pub const ResourceManager = @import("resource/ResourceManager.zig"); +pub const gpu = @import("gpu"); +pub const ecs = @import("ecs"); + +// Engine exports +pub const App = @import("engine.zig").App; +pub const module = @import("engine.zig").module;