{Core,examples}: set window title via component

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-04-21 22:48:30 -07:00 committed by Stephen Gutekanst
parent 79dccb4d73
commit 68677b3448
5 changed files with 104 additions and 19 deletions

View file

@ -15,6 +15,10 @@ pub const global_events = .{
};
pub const local_events = .{
.update = .{ .handler = update, .description =
\\ Send this when window entities have been updated and you want the new values respected.
},
.init = .{ .handler = init },
.init_done = .{ .handler = fn () void },
@ -26,10 +30,40 @@ pub const local_events = .{
.exit = .{ .handler = exit },
};
pub const components = .{
.title = .{ .type = [:0]u8, .description =
\\ Window title slice. Can be set with a format string and arguments via:
\\
\\ ```
\\ try mach.Core.printTitle(core_mod, core_mod.state().main_window, "Hello, {s}!", .{"Mach"});
\\ ```
\\
\\ If setting this component yourself, ensure the buffer is allocated using core.state().allocator
\\ as it will be freed for you as part of the .deinit event.
},
};
/// Prints into the window title buffer using a format string and arguments. e.g.
///
/// ```
/// try mach.Core.printTitle(core_mod, core_mod.state().main_window, "Hello, {s}!", .{"Mach"});
/// ```
pub fn printTitle(
core: *mach.Core.Mod,
window_id: mach.EntityID,
comptime fmt: []const u8,
args: anytype,
) !void {
const slice = try std.fmt.allocPrintZ(core.state().allocator, fmt, args);
try core.set(window_id, .title, slice);
}
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
allocator: std.mem.Allocator,
device: *mach.gpu.Device,
queue: *mach.gpu.Queue,
main_window: mach.EntityID,
should_exit: bool = false,
fn init(core: *Mod) !void {
@ -41,19 +75,52 @@ fn init(core: *Mod) !void {
try mach.core.init(.{});
core.init(.{
.allocator = mach.core.allocator,
.device = mach.core.device,
.queue = mach.core.device.getQueue(),
.main_window = try core.newEntity(),
});
core.sendGlobal(.init, .{});
core.send(.init_done, .{});
}
fn update(core: *Mod) !void {
var archetypes_iter = core.entities.query(.{ .all = &.{
.{ .mach_core = &.{
.title,
} },
} });
var num_windows: usize = 0;
while (archetypes_iter.next()) |archetype| {
for (
archetype.slice(.entity, .id),
archetype.slice(.mach_core, .title),
) |window_id, title| {
num_windows += 1;
_ = window_id;
try mach.core.printTitle("{s}", .{title});
}
}
if (num_windows > 1) @panic("mach: Core currently only supports a single window");
}
fn deinit(core: *Mod) void {
core.state().queue.release();
// TODO: this triggers a device loss error, which we should handle correctly
// core.state().device.release();
mach.core.deinit();
var archetypes_iter = core.entities.query(.{ .all = &.{
.{ .mach_core = &.{
.title,
} },
} });
while (archetypes_iter.next()) |archetype| {
for (archetype.slice(.mach_core, .title)) |title| core.state().allocator.free(title);
}
_ = gpa.deinit();
}