Skip to content

Commit

Permalink
make Mod(.module_tag) take a Mod(ModuleType) instead
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
emidoots committed Dec 17, 2023
1 parent ef06fb6 commit 7267d8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test "example" {
pub const id = u32;
};

pub fn tick(physics: *World(.{ Renderer, Physics }).Mod(.physics)) !void {
pub fn tick(physics: *World(.{ Renderer, Physics }).Mod(Physics)) !void {
_ = physics;
}
});
Expand All @@ -64,8 +64,8 @@ test "example" {
};

pub fn tick(
physics: *World(.{ Renderer, Physics }).Mod(.physics),
renderer: *World(.{ Renderer, Physics }).Mod(.renderer),
physics: *World(.{ Renderer, Physics }).Mod(Physics),
renderer: *World(.{ Renderer, Physics }).Mod(Renderer),
) !void {
_ = renderer;
_ = physics;
Expand Down
43 changes: 6 additions & 37 deletions src/systems.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub fn World(comptime mods: anytype) type {

const Self = @This();

pub fn Mod(comptime module_tag: anytype) type {
pub fn Mod(comptime Module: anytype) type {
const module_tag = Module.name;
const State = @TypeOf(@field(@as(modules.State, undefined), @tagName(module_tag)));
const components = @field(modules.components, @tagName(module_tag));
return struct {
Expand Down Expand Up @@ -62,38 +63,6 @@ pub fn World(comptime mods: anytype) type {
try world.entities.removeComponent(entity, module_tag, component_name);
}

fn upper(c: u8) u8 {
return switch (c) {
'a' => 'A',
'b' => 'B',
'c' => 'C',
'd' => 'D',
'e' => 'E',
'f' => 'F',
'g' => 'G',
'h' => 'H',
'i' => 'I',
'j' => 'J',
'k' => 'K',
'l' => 'L',
'm' => 'M',
'n' => 'N',
'o' => 'O',
'p' => 'P',
'q' => 'Q',
'r' => 'R',
's' => 'S',
't' => 'T',
'u' => 'U',
'v' => 'V',
'w' => 'W',
'x' => 'X',
'y' => 'Y',
'z' => 'Z',
else => c,
};
}

pub fn send(m: *@This(), comptime msg_tag: anytype, args: anytype) !void {
const mod_ptr: *Self.Mods() = @alignCast(@fieldParentPtr(Mods(), @tagName(module_tag), m));
const world = @fieldParentPtr(Self, "mod", mod_ptr);
Expand Down Expand Up @@ -121,10 +90,10 @@ pub fn World(comptime mods: anytype) type {
inline for (modules.modules) |M| {
fields = fields ++ [_]std.builtin.Type.StructField{.{
.name = @tagName(M.name),
.type = Mod(M.name),
.type = Mod(M),
.default_value = null,
.is_comptime = false,
.alignment = @alignOf(Mod(M.name)),
.alignment = @alignOf(Mod(M)),
}};
}
return @Type(.{
Expand Down Expand Up @@ -190,12 +159,12 @@ pub fn World(comptime mods: anytype) type {
// Determine which parameters the handler function wants. e.g.:
//
// pub fn init(eng: *mach.Engine) !void
// pub fn init(eng: *mach.Engine, mach: *mach.Mod(.engine)) !void
// pub fn init(eng: *mach.Engine, mach: *mach.Engine.Mod) !void
//
const handler = @field(EventHandlers, msg);

// Build a tuple of parameters that we can pass to the function, based on what
// *mach.Mod(.foo) types it expects as arguments.
// *mach.Mod(T) types it expects as arguments.
var params: std.meta.ArgsTuple(@TypeOf(handler)) = undefined;
comptime var argIndex = 0;
inline for (@typeInfo(@TypeOf(params)).Struct.fields) |param| {
Expand Down

0 comments on commit 7267d8e

Please sign in to comment.