mach: use a minimal core App, with a module implementing everything
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
7602bb5357
commit
4ebf238c5e
1 changed files with 42 additions and 22 deletions
|
|
@ -4,48 +4,68 @@ const gpu = @import("core").gpu;
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const ecs = @import("ecs");
|
const ecs = @import("ecs");
|
||||||
|
|
||||||
/// The Mach engine ECS module. This enables access to `engine.get(.mach, .core)` `*Core` APIs, as
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
/// to for example `.setOptions(.{.title = "foobar"})`, or to access the GPU device via
|
const allocator = gpa.allocator();
|
||||||
/// `engine.get(.mach, .device)`
|
|
||||||
|
/// The main Mach engine ECS module.
|
||||||
pub const Module = struct {
|
pub const Module = struct {
|
||||||
|
_core: Core,
|
||||||
core: *Core,
|
core: *Core,
|
||||||
device: *gpu.Device,
|
device: *gpu.Device,
|
||||||
exit: bool,
|
exit: bool,
|
||||||
|
|
||||||
pub const name = .mach;
|
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 {
|
pub fn App(comptime modules: anytype) type {
|
||||||
// TODO: validate modules.mach is the expected type.
|
|
||||||
|
|
||||||
return struct {
|
return struct {
|
||||||
engine: ecs.World(modules),
|
engine: ecs.World(modules),
|
||||||
core: Core,
|
|
||||||
|
|
||||||
pub fn init(app: *@This()) !void {
|
pub fn init(app: *@This()) !void {
|
||||||
try app.core.init(allocator, .{});
|
app.* = .{ .engine = try ecs.World(modules).init(allocator) };
|
||||||
app.* = .{
|
try app.engine.send(.machInit);
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *@This()) void {
|
pub fn deinit(app: *@This()) void {
|
||||||
app.core.deinit();
|
try app.engine.send(.machDeinit);
|
||||||
app.engine.deinit();
|
|
||||||
_ = gpa.deinit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *@This()) !bool {
|
pub fn update(app: *@This()) !bool {
|
||||||
try app.engine.send(.tick);
|
try app.engine.send(.tick);
|
||||||
return app.engine.mod(.mach).getState(.exit);
|
return app.engine.mod(.mach).state().exit;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue