unify mach.Call and mach.Runner into one type
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
14ccd5a93c
commit
8054d03b4d
19 changed files with 125 additions and 97 deletions
|
|
@ -5,9 +5,9 @@ const sysaudio = mach.sysaudio;
|
|||
|
||||
pub const Opus = @import("mach-opus");
|
||||
|
||||
pub const name = .mach_audio;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .mach_audio;
|
||||
|
||||
// TODO(object)
|
||||
pub const components = .{
|
||||
.samples = .{ .type = []const f32 },
|
||||
.channels = .{ .type = u8 },
|
||||
|
|
@ -22,7 +22,7 @@ pub const systems = .{
|
|||
.audio_tick = .{ .handler = audioTick },
|
||||
};
|
||||
|
||||
const log = std.log.scoped(name);
|
||||
const log = std.log.scoped(mach_module);
|
||||
|
||||
// The number of milliseconds worth of audio to render ahead of time. The lower this number is, the
|
||||
// less latency there is in playing new audio. The higher this number is, the less chance there is
|
||||
|
|
|
|||
30
src/Core.zig
30
src/Core.zig
|
|
@ -233,17 +233,17 @@ pub fn init(core: *Core) !void {
|
|||
try core.input.start();
|
||||
}
|
||||
|
||||
pub fn tick(core: *Core, present_frame: mach.Call(Core, .present_frame), runner: mach.Runner) void {
|
||||
runner.run(core.on_tick.?);
|
||||
runner.run(present_frame.id);
|
||||
pub fn tick(core: *Core, core_mod: mach.Functions(Core)) void {
|
||||
core_mod.run(core.on_tick.?);
|
||||
core_mod.call(.presentFrame);
|
||||
}
|
||||
|
||||
pub fn main(core: *Core, present_frame: mach.Call(Core, .presentFrame), runner: mach.Runner) !void {
|
||||
pub fn main(core: *Core, core_mod: mach.Functions(Core)) !void {
|
||||
if (core.on_tick == null) @panic("core.on_tick callback must be set");
|
||||
if (core.on_exit == null) @panic("core.on_exit callback must be set");
|
||||
|
||||
runner.run(core.on_tick.?);
|
||||
runner.run(present_frame.id);
|
||||
core_mod.run(core.on_tick.?);
|
||||
core_mod.call(.presentFrame);
|
||||
|
||||
// If the user doesn't want mach.Core to take control of the main loop, we bail out - the next
|
||||
// app tick is already scheduled to run in the future and they'll .present_frame to return
|
||||
|
|
@ -259,8 +259,8 @@ pub fn main(core: *Core, present_frame: mach.Call(Core, .presentFrame), runner:
|
|||
// The user wants mach.Core to take control of the main loop.
|
||||
if (supports_non_blocking) {
|
||||
while (core.state().state != .exited) {
|
||||
runner.run(core.on_tick.?);
|
||||
runner.run(present_frame.id);
|
||||
core_mod.run(core.on_tick.?);
|
||||
core_mod.call(.presentFrame);
|
||||
}
|
||||
|
||||
// Don't return, because Platform.run wouldn't either (marked noreturn due to underlying
|
||||
|
|
@ -268,7 +268,7 @@ pub fn main(core: *Core, present_frame: mach.Call(Core, .presentFrame), runner:
|
|||
std.process.exit(0);
|
||||
} else {
|
||||
// Platform drives the main loop.
|
||||
Platform.run(platform_update_callback, .{ core, present_frame.id, runner });
|
||||
Platform.run(platform_update_callback, .{ core, core_mod });
|
||||
|
||||
// Platform.run should be marked noreturn, so this shouldn't ever run. But just in case we
|
||||
// accidentally introduce a different Platform.run in the future, we put an exit here for
|
||||
|
|
@ -277,9 +277,9 @@ pub fn main(core: *Core, present_frame: mach.Call(Core, .presentFrame), runner:
|
|||
}
|
||||
}
|
||||
|
||||
fn platform_update_callback(core: *Core, present_frame: mach.FunctionID, runner: mach.Runner) !bool {
|
||||
runner.run(core.on_tick.?);
|
||||
runner.run(present_frame);
|
||||
fn platform_update_callback(core: *Core, core_mod: mach.Functions(Core)) !bool {
|
||||
core_mod.run(core.on_tick.?);
|
||||
core_mod.call(.presentFrame);
|
||||
|
||||
return core.state != .exited;
|
||||
}
|
||||
|
|
@ -563,7 +563,7 @@ pub fn mousePosition(core: *@This()) Position {
|
|||
// return core.platform.nativeWindowWin32();
|
||||
// }
|
||||
|
||||
pub fn presentFrame(core: *Core, core_deinit: mach.Call(Core, .deinit), runner: mach.Runner) !void {
|
||||
pub fn presentFrame(core: *Core, core_mod: mach.Functions(Core)) !void {
|
||||
// TODO(object)(window-title)
|
||||
// // Update windows title
|
||||
// var num_windows: usize = 0;
|
||||
|
|
@ -622,8 +622,8 @@ pub fn presentFrame(core: *Core, core_deinit: mach.Call(Core, .deinit), runner:
|
|||
.running => {},
|
||||
.exiting => {
|
||||
core.state = .deinitializing;
|
||||
runner.run(core.on_exit.?);
|
||||
runner.run(core_deinit.id);
|
||||
core_mod.run(core.on_exit.?);
|
||||
core_mod.call(.deinit);
|
||||
},
|
||||
.deinitializing => {},
|
||||
.exited => @panic("application not running"),
|
||||
|
|
|
|||
|
|
@ -13,9 +13,9 @@ const Vec3 = math.Vec3;
|
|||
const Mat3x3 = math.Mat3x3;
|
||||
const Mat4x4 = math.Mat4x4;
|
||||
|
||||
pub const name = .mach_gfx_sprite;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .mach_gfx_sprite;
|
||||
|
||||
// TODO(object)
|
||||
pub const components = .{
|
||||
.transform = .{ .type = Mat4x4, .description =
|
||||
\\ The sprite model transformation matrix. A sprite is measured in pixel units, starting from
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ const mach = @import("../main.zig");
|
|||
const gpu = mach.gpu;
|
||||
const math = mach.math;
|
||||
|
||||
pub const name = .mach_gfx_sprite_pipeline;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .mach_gfx_sprite_pipeline;
|
||||
|
||||
// TODO(object)
|
||||
pub const components = .{
|
||||
.texture = .{ .type = *gpu.Texture, .description =
|
||||
\\ Texture to use when rendering. The default shader can handle only one texture input.
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ const Mat4x4 = math.Mat4x4;
|
|||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
pub const name = .mach_gfx_text;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .mach_gfx_text;
|
||||
|
||||
// TODO(object)
|
||||
pub const components = .{
|
||||
.transform = .{ .type = Mat4x4, .description =
|
||||
\\ The text model transformation matrix. Text is measured in pixel units, starting from
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ const gfx = mach.gfx;
|
|||
const gpu = mach.gpu;
|
||||
const math = mach.math;
|
||||
|
||||
pub const name = .mach_gfx_text_pipeline;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .mach_gfx_text_pipeline;
|
||||
|
||||
// TODO(object)
|
||||
pub const components = .{
|
||||
.is_pipeline = .{ .type = void, .description =
|
||||
\\ Tag to indicate an entity represents a text pipeline.
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
const mach = @import("../main.zig");
|
||||
const math = mach.math;
|
||||
|
||||
pub const name = .mach_gfx_text_style;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .mach_gfx_text_style;
|
||||
|
||||
// TODO(object)
|
||||
pub const components = .{
|
||||
// // TODO: ship a default font
|
||||
// .font_name = .{ .type = []const u8, .description =
|
||||
|
|
|
|||
|
|
@ -26,8 +26,7 @@ pub const Modules = @import("module.zig").Modules;
|
|||
pub const ModuleID = @import("module.zig").ModuleID;
|
||||
pub const ModuleFunctionID = @import("module.zig").ModuleFunctionID;
|
||||
pub const FunctionID = @import("module.zig").FunctionID;
|
||||
pub const Call = @import("module.zig").Call;
|
||||
pub const Runner = @import("module.zig").Runner;
|
||||
pub const Functions = @import("module.zig").Functions;
|
||||
|
||||
pub const ObjectID = u32;
|
||||
|
||||
|
|
|
|||
|
|
@ -9,27 +9,69 @@ pub const ModuleFunctionID = u16;
|
|||
/// Unique identifier for a function within a module, including those only known at runtime.
|
||||
pub const FunctionID = struct { module_id: ModuleID, fn_id: ModuleFunctionID };
|
||||
|
||||
pub fn Call(module_tag_or_type: anytype, fn_name_tag: anytype) type {
|
||||
pub fn Mod(comptime M: type) type {
|
||||
return struct {
|
||||
pub const IsMachCall = void;
|
||||
pub const IsMachMod = void;
|
||||
|
||||
pub const module_name = module_tag_or_type;
|
||||
pub const fn_name = fn_name_tag;
|
||||
pub const module_name = M.mach_module;
|
||||
pub const Module = M;
|
||||
|
||||
id: FunctionID,
|
||||
id: ModFunctionIDs(M),
|
||||
_ctx: *anyopaque,
|
||||
_run: *const fn (ctx: *anyopaque, fn_id: FunctionID) void,
|
||||
|
||||
pub fn run(r: *const @This(), fn_id: FunctionID) void {
|
||||
r._run(r._ctx, fn_id);
|
||||
}
|
||||
|
||||
pub fn call(r: *const @This(), comptime f: ModuleFunctionName2(M)) void {
|
||||
const fn_id = @field(r.id, @tagName(f));
|
||||
r.run(fn_id);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub const Runner = struct {
|
||||
pub const IsMachRunner = void;
|
||||
|
||||
ctx: *anyopaque,
|
||||
_run: *const fn (ctx: *anyopaque, fn_id: FunctionID) void,
|
||||
|
||||
pub fn run(r: *const @This(), fn_id: FunctionID) void {
|
||||
r._run(r.ctx, fn_id);
|
||||
pub fn ModFunctionIDs(comptime Module: type) type {
|
||||
var fields: []const std.builtin.Type.StructField = &[0]std.builtin.Type.StructField{};
|
||||
for (Module.mach_systems) |fn_name| {
|
||||
fields = fields ++ [_]std.builtin.Type.StructField{.{
|
||||
.name = @tagName(fn_name),
|
||||
.type = FunctionID,
|
||||
.default_value = null,
|
||||
.is_comptime = false,
|
||||
.alignment = @alignOf(FunctionID),
|
||||
}};
|
||||
}
|
||||
};
|
||||
return @Type(.{
|
||||
.Struct = .{
|
||||
.layout = .auto,
|
||||
.is_tuple = false,
|
||||
.fields = fields,
|
||||
.decls = &[_]std.builtin.Type.Declaration{},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/// Enum describing all declarations for a given comptime-known module.
|
||||
// TODO: unify with ModuleFunctionName
|
||||
fn ModuleFunctionName2(comptime M: type) type {
|
||||
validate(M);
|
||||
var enum_fields: []const std.builtin.Type.EnumField = &[0]std.builtin.Type.EnumField{};
|
||||
var i: u32 = 0;
|
||||
inline for (M.mach_systems) |fn_tag| {
|
||||
// TODO: verify decls are Fn or mach.schedule() decl
|
||||
enum_fields = enum_fields ++ [_]std.builtin.Type.EnumField{.{ .name = @tagName(fn_tag), .value = i }};
|
||||
i += 1;
|
||||
}
|
||||
return @Type(.{
|
||||
.Enum = .{
|
||||
.tag_type = if (enum_fields.len > 0) std.math.IntFittingRange(0, enum_fields.len - 1) else u0,
|
||||
.fields = enum_fields,
|
||||
.decls = &[_]std.builtin.Type.Declaration{},
|
||||
.is_exhaustive = true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
pub fn Modules(module_lists: anytype) type {
|
||||
inline for (moduleTuple(module_lists)) |module| {
|
||||
|
|
@ -147,16 +189,11 @@ pub fn Modules(module_lists: anytype) type {
|
|||
@field(args, arg.name) = &@field(m.mods, @tagName(std.meta.Child(arg.type).mach_module));
|
||||
continue :outer;
|
||||
}
|
||||
if (@typeInfo(arg.type) == .Struct and @hasDecl(arg.type, "IsMachCall")) {
|
||||
const machCall = arg.type;
|
||||
@field(args, arg.name) = .{
|
||||
.id = Module(@field(machCall, "module_name")).getFunction(@field(machCall, "fn_name")),
|
||||
};
|
||||
continue :outer;
|
||||
}
|
||||
if (@typeInfo(arg.type) == .Struct and @hasDecl(arg.type, "IsMachRunner")) {
|
||||
@field(args, arg.name) = Runner{
|
||||
.ctx = m.modules,
|
||||
if (@typeInfo(arg.type) == .Struct and @hasDecl(arg.type, "IsMachMod")) {
|
||||
const M = arg.type.Module;
|
||||
var mv: Mod(M) = .{
|
||||
.id = undefined,
|
||||
._ctx = m.modules,
|
||||
._run = (struct {
|
||||
pub fn run(ctx: *anyopaque, fn_id: FunctionID) void {
|
||||
const modules2: *Modules(module_lists) = @ptrCast(@alignCast(ctx));
|
||||
|
|
@ -164,6 +201,10 @@ pub fn Modules(module_lists: anytype) type {
|
|||
}
|
||||
}).run,
|
||||
};
|
||||
inline for (M.mach_systems) |m_fn_name| {
|
||||
@field(mv.id, @tagName(m_fn_name)) = Module(M).getFunction(m_fn_name);
|
||||
}
|
||||
@field(args, arg.name) = mv;
|
||||
continue :outer;
|
||||
}
|
||||
@compileError("mach: function " ++ debug_name ++ " has an invalid argument(" ++ arg.name ++ ") type: " ++ @typeName(arg.type));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue