{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

@ -51,7 +51,7 @@ fn init(game: *Mod, core: *mach.Core.Mod) !void {
.title_timer = try mach.Timer.start(),
.pipeline = pipeline,
});
try updateWindowTitle();
try updateWindowTitle(core);
}
pub fn deinit(game: *Mod) void {
@ -109,13 +109,19 @@ fn tick(core: *mach.Core.Mod, game: *Mod) !void {
// update the window title every second
if (game.state().title_timer.read() >= 1.0) {
game.state().title_timer.reset();
try updateWindowTitle();
try updateWindowTitle(core);
}
}
fn updateWindowTitle() !void {
try mach.core.printTitle("mach.Core - custom entrypoint [ {d}fps ] [ Input {d}hz ]", .{
mach.core.frameRate(),
mach.core.inputRate(),
});
fn updateWindowTitle(core: *mach.Core.Mod) !void {
try mach.Core.printTitle(
core,
core.state().main_window,
"core-custom-entrypoint [ {d}fps ] [ Input {d}hz ]",
.{
mach.core.frameRate(),
mach.core.inputRate(),
},
);
core.send(.update, .{});
}

View file

@ -57,9 +57,6 @@ fn afterInit(
glyphs: *Glyphs.Mod,
game: *Mod,
) !void {
// The Mach .core is where we set window options, etc.
mach.core.setTitle("gfx.Sprite example");
// Create a sprite rendering pipeline
const texture = glyphs.state().texture;
const pipeline = try sprite_pipeline.newEntity();
@ -231,7 +228,7 @@ fn tick(
game.state().time += delta_time;
}
fn endFrame(game: *Mod) !void {
fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
// Finish render pass
game.state().frame_render_pass.end();
const label = @tagName(name) ++ ".endFrame";
@ -245,7 +242,13 @@ fn endFrame(game: *Mod) !void {
// Every second, update the window title with the FPS
if (game.state().fps_timer.read() >= 1.0) {
try mach.core.printTitle("gfx.Sprite example [ FPS: {d} ] [ Sprites: {d} ]", .{ game.state().frame_count, game.state().sprites });
try mach.Core.printTitle(
core,
core.state().main_window,
"glyphs [ FPS: {d} ] [ Sprites: {d} ]",
.{ game.state().frame_count, game.state().sprites },
);
core.send(.update, .{});
game.state().fps_timer.reset();
game.state().frame_count = 0;
}

View file

@ -51,9 +51,6 @@ fn init(
sprite_pipeline: *gfx.SpritePipeline.Mod,
game: *Mod,
) !void {
// The Mach .core is where we set window options, etc.
mach.core.setTitle("gfx.Sprite example");
// We can create entities, and set components on them. Note that components live in a module
// namespace, e.g. the `.mach_gfx_sprite` module could have a 3D `.location` component with a different
// type than the `.physics2d` module's `.location` component if you desire.
@ -215,7 +212,7 @@ fn tick(
game.state().time += delta_time;
}
fn endFrame(game: *Mod) !void {
fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
// Finish render pass
game.state().frame_render_pass.end();
const label = @tagName(name) ++ ".endFrame";
@ -229,7 +226,13 @@ fn endFrame(game: *Mod) !void {
// Every second, update the window title with the FPS
if (game.state().fps_timer.read() >= 1.0) {
try mach.core.printTitle("gfx.Sprite example [ FPS: {d} ] [ Sprites: {d} ]", .{ game.state().frame_count, game.state().sprites });
try mach.Core.printTitle(
core,
core.state().main_window,
"sprite [ FPS: {d} ] [ Sprites: {d} ]",
.{ game.state().frame_count, game.state().sprites },
);
core.send(.update, .{});
game.state().fps_timer.reset();
game.state().frame_count = 0;
}

View file

@ -258,7 +258,7 @@ fn tick(
game.state().time += delta_time;
}
fn endFrame(game: *Mod, text: *gfx.Text.Mod) !void {
fn endFrame(game: *Mod, text: *gfx.Text.Mod, core: *mach.Core.Mod) !void {
// Finish render pass
game.state().frame_render_pass.end();
const label = @tagName(name) ++ ".tick";
@ -288,7 +288,13 @@ fn endFrame(game: *Mod, text: *gfx.Text.Mod) !void {
}
}
try mach.core.printTitle("gfx.Text example [ FPS: {d} ] [ Texts: {d} ] [ Glyphs: {d} ]", .{ game.state().frame_count, num_texts, num_glyphs });
try mach.Core.printTitle(
core,
core.state().main_window,
"text [ FPS: {d} ] [ Texts: {d} ] [ Glyphs: {d} ]",
.{ game.state().frame_count, num_texts, num_glyphs },
);
core.send(.update, .{});
game.state().fps_timer.reset();
game.state().frame_count = 0;
}