module: *World is no longer an injectable parameter
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
3bfafe102d
commit
15d9efcf26
5 changed files with 44 additions and 38 deletions
|
|
@ -19,8 +19,6 @@ pub fn World(comptime mods: anytype) type {
|
|||
|
||||
const Modules = mach.Modules(mods);
|
||||
|
||||
pub const IsInjectedArgument = void;
|
||||
|
||||
const WorldT = @This();
|
||||
pub fn Mod(comptime M: anytype) type {
|
||||
const module_tag = M.name;
|
||||
|
|
@ -76,6 +74,18 @@ pub fn World(comptime mods: anytype) type {
|
|||
world.modules.sendToModule(module_tag, event_name, args);
|
||||
}
|
||||
|
||||
pub inline fn sendGlobal(m: *@This(), comptime event_name: anytype, args: anytype) void {
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(WorldT, "mod", mod_ptr);
|
||||
world.modules.send(event_name, args);
|
||||
}
|
||||
|
||||
pub fn dispatchNoError(m: *@This()) void {
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
const world = @fieldParentPtr(WorldT, "mod", mod_ptr);
|
||||
world.modules.dispatch(world.injectable()) catch |err| @panic(@errorName(err));
|
||||
}
|
||||
|
||||
/// Returns a new entity.
|
||||
pub fn newEntity(m: *@This()) !EntityID {
|
||||
const mod_ptr: *Mods = @alignCast(@fieldParentPtr(Mods, @tagName(module_tag), m));
|
||||
|
|
@ -115,7 +125,6 @@ pub fn World(comptime mods: anytype) type {
|
|||
|
||||
const Injectable = blk: {
|
||||
var types: []const type = &[0]type{};
|
||||
types = types ++ [_]type{*@This()};
|
||||
for (@typeInfo(Mods).Struct.fields) |field| {
|
||||
const ModPtr = @TypeOf(@as(*field.type, undefined));
|
||||
types = types ++ [_]type{ModPtr};
|
||||
|
|
@ -125,19 +134,14 @@ pub fn World(comptime mods: anytype) type {
|
|||
fn injectable(world: *@This()) Injectable {
|
||||
var v: Injectable = undefined;
|
||||
outer: inline for (@typeInfo(Injectable).Struct.fields) |field| {
|
||||
if (field.type == *@This()) {
|
||||
@field(v, field.name) = world;
|
||||
continue :outer;
|
||||
} else {
|
||||
inline for (@typeInfo(Mods).Struct.fields) |injectable_field| {
|
||||
if (*injectable_field.type == field.type) {
|
||||
@field(v, field.name) = &@field(world.mod, injectable_field.name);
|
||||
inline for (@typeInfo(Mods).Struct.fields) |injectable_field| {
|
||||
if (*injectable_field.type == field.type) {
|
||||
@field(v, field.name) = &@field(world.mod, injectable_field.name);
|
||||
|
||||
// TODO: better module initialization location
|
||||
@field(v, field.name).entities = &world.entities;
|
||||
@field(v, field.name).allocator = world.allocator;
|
||||
continue :outer;
|
||||
}
|
||||
// TODO: better module initialization location
|
||||
@field(v, field.name).entities = &world.entities;
|
||||
@field(v, field.name).allocator = world.allocator;
|
||||
continue :outer;
|
||||
}
|
||||
}
|
||||
@compileError("failed to initialize Injectable field (this is a bug): " ++ field.name ++ " " ++ @typeName(field.type));
|
||||
|
|
@ -149,10 +153,6 @@ pub fn World(comptime mods: anytype) type {
|
|||
try world.modules.dispatch(world.injectable());
|
||||
}
|
||||
|
||||
pub fn dispatchNoError(world: *@This()) void {
|
||||
world.modules.dispatch(world.injectable()) catch |err| @panic(@errorName(err));
|
||||
}
|
||||
|
||||
pub fn init(world: *@This(), allocator: mem.Allocator) !void {
|
||||
// TODO: switch Entities to stack allocation like Modules and World
|
||||
var entities = try Entities(ns_components).init(allocator);
|
||||
|
|
|
|||
|
|
@ -29,34 +29,31 @@ pub const Engine = struct {
|
|||
.{ .global = .exit, .handler = fn () void },
|
||||
};
|
||||
|
||||
fn init(world: *World) !void {
|
||||
fn init(engine: *Mod) !void {
|
||||
core.allocator = allocator;
|
||||
try core.init(.{});
|
||||
const state = &world.mod.engine.state;
|
||||
const state = &engine.state;
|
||||
state.device = core.device;
|
||||
state.queue = core.device.getQueue();
|
||||
state.should_exit = false;
|
||||
state.encoder = state.device.createCommandEncoder(&gpu.CommandEncoder.Descriptor{
|
||||
.label = "engine.state.encoder",
|
||||
});
|
||||
|
||||
world.modules.send(.init, .{});
|
||||
engine.sendGlobal(.init, .{});
|
||||
}
|
||||
|
||||
fn deinit(world: *World, engine: *Mod) void {
|
||||
fn deinit(engine: *Mod) void {
|
||||
// TODO: this triggers a device loss error, which we should handle correctly
|
||||
// engine.state.device.release();
|
||||
engine.state.queue.release();
|
||||
world.modules.send(.deinit, .{});
|
||||
engine.sendGlobal(.deinit, .{});
|
||||
core.deinit();
|
||||
world.deinit();
|
||||
_ = gpa.deinit();
|
||||
}
|
||||
|
||||
// Engine module's exit handler
|
||||
fn exit(world: *World) void {
|
||||
world.modules.send(.exit, .{});
|
||||
world.mod.engine.state.should_exit = true;
|
||||
fn exit(engine: *Mod) void {
|
||||
engine.sendGlobal(.exit, .{});
|
||||
engine.state.should_exit = true;
|
||||
}
|
||||
|
||||
fn beginPass(engine: *Mod, clear_color: gpu.Color) void {
|
||||
|
|
@ -110,6 +107,10 @@ pub const App = struct {
|
|||
|
||||
pub fn deinit(app: *@This()) void {
|
||||
app.world.modules.sendToModule(.engine, .deinit, .{});
|
||||
// TODO: improve error handling
|
||||
app.world.dispatch() catch |err| @panic(@errorName(err)); // dispatch .deinit
|
||||
app.world.deinit();
|
||||
_ = gpa.deinit();
|
||||
}
|
||||
|
||||
pub fn update(app: *@This()) !bool {
|
||||
|
|
@ -134,5 +135,7 @@ fn modules() Modules() {
|
|||
if (!@hasDecl(@import("root"), "modules")) {
|
||||
@compileError("expected `pub const modules = .{};` in root file");
|
||||
}
|
||||
// TODO: verify modules (causes loop currently)
|
||||
// _ = @import("module.zig").Modules(@import("root").modules);
|
||||
return @import("root").modules;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -115,7 +115,11 @@ pub fn Modules(comptime mods: anytype) type {
|
|||
},
|
||||
else => unreachable,
|
||||
};
|
||||
return UninjectedArgsTuple(std.meta.Tuple, Handler);
|
||||
|
||||
// TODO: passing std.meta.Tuple here instead of TupleHACK results in a compiler
|
||||
// segfault. The only difference is that TupleHACk does not produce a real tuple,
|
||||
// `@Type(.{.Struct = .{ .is_tuple = false }})` instead of `.is_tuple = true`.
|
||||
return UninjectedArgsTuple(TupleHACK, Handler);
|
||||
}
|
||||
}
|
||||
@compileError("No global event handler ." ++ @tagName(event_name) ++ " is defined in any module.");
|
||||
|
|
@ -986,7 +990,7 @@ test "dispatch" {
|
|||
// Global events which are not handled by anyone yet can be written as `pub const fooBar = fn() void;`
|
||||
// within a module, which allows pre-declaring that `fooBar` is a valid global event, and enables
|
||||
// its arguments to be inferred still like this:
|
||||
modules.send(.frame_done, .{1337});
|
||||
modules.send(.frame_done, .{ .@"0" = 1337 });
|
||||
|
||||
// Local events
|
||||
modules.sendToModule(.engine_renderer, .update, .{});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue