examples: update hardware-check example to use new object system

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-12-27 17:39:30 -07:00
parent f352670464
commit d62650276f
3 changed files with 230 additions and 216 deletions

View file

@ -76,7 +76,7 @@ pub fn build(b: *std.Build) !void {
.{ .name = "core-transparent-window", .deps = &.{} }, .{ .name = "core-transparent-window", .deps = &.{} },
.{ .name = "custom-renderer", .deps = &.{} }, .{ .name = "custom-renderer", .deps = &.{} },
.{ .name = "glyphs", .deps = &.{ .assets, .freetype } }, .{ .name = "glyphs", .deps = &.{ .assets, .freetype } },
// .{ .name = "hardware-check", .deps = &.{ .assets, .zigimg } }, .{ .name = "hardware-check", .deps = &.{ .assets, .zigimg } },
.{ .name = "piano", .deps = &.{} }, .{ .name = "piano", .deps = &.{} },
.{ .name = "play-opus", .deps = &.{.assets} }, .{ .name = "play-opus", .deps = &.{.assets} },
.{ .name = "sprite", .deps = &.{ .zigimg, .assets } }, .{ .name = "sprite", .deps = &.{ .zigimg, .assets } },

View file

@ -1,4 +1,3 @@
// TODO(important): review all code in this file in-depth
const std = @import("std"); const std = @import("std");
const zigimg = @import("zigimg"); const zigimg = @import("zigimg");
const assets = @import("assets"); const assets = @import("assets");
@ -9,9 +8,7 @@ const math = mach.math;
const vec2 = math.vec2; const vec2 = math.vec2;
const vec3 = math.vec3; const vec3 = math.vec3;
const vec4 = math.vec4;
const Vec2 = math.Vec2; const Vec2 = math.Vec2;
const Vec3 = math.Vec3;
const Mat3x3 = math.Mat3x3; const Mat3x3 = math.Mat3x3;
const Mat4x4 = math.Mat4x4; const Mat4x4 = math.Mat4x4;
@ -19,271 +16,292 @@ const App = @This();
pub const mach_module = .app; pub const mach_module = .app;
pub const mach_systems = .{ .start, .init, .deinit, .tick, .end_frame, .audio_state_change }; pub const mach_systems = .{ .main, .init, .tick, .deinit, .deinit2, .audioStateChange };
// TODO: banish global allocator pub const main = mach.schedule(.{
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; .{ mach.Core, .init },
.{ mach.Audio, .init },
.{ gfx.Text, .init },
.{ App, .init },
.{ mach.Core, .main },
});
info_text: mach.EntityID, pub const deinit = mach.schedule(.{
info_text_style: mach.EntityID, .{ mach.Audio, .deinit },
.{ App, .deinit2 },
});
allocator: std.mem.Allocator,
window: mach.ObjectID,
timer: mach.time.Timer, timer: mach.time.Timer,
gotta_go_fast: bool = false,
spawn_timer: mach.time.Timer, spawn_timer: mach.time.Timer,
fps_timer: mach.time.Timer, fps_timer: mach.time.Timer,
frame_count: usize,
frame_rate: usize,
num_sprites_spawned: usize,
score: usize,
rand: std.Random.DefaultPrng, rand: std.Random.DefaultPrng,
time: f32,
allocator: std.mem.Allocator,
pipeline: mach.EntityID,
frame_encoder: *gpu.CommandEncoder = undefined,
frame_render_pass: *gpu.RenderPassEncoder = undefined,
sfx: mach.Audio.Opus,
fn deinit( frame_count: usize = 0,
text_pipeline: *gfx.TextPipeline.Mod, fps: usize = 0,
sprite_pipeline: *gfx.SpritePipeline.Mod, score: usize = 0,
audio: *mach.Audio, num_sprites_spawned: usize = 0,
) !void { time: f32 = 0,
text_pipeline.schedule(.deinit); direction: Vec2 = vec2(0, 0),
sprite_pipeline.schedule(.deinit); spawning: bool = false,
audio.schedule(.deinit); gotta_go_fast: bool = false,
}
fn start( info_text: []u8 = undefined,
audio: *mach.Audio, info_text_id: mach.ObjectID = undefined,
text_pipeline: *gfx.TextPipeline.Mod, info_text_style_id: mach.ObjectID = undefined,
text: *gfx.Text.Mod, sprite_pipeline_id: mach.ObjectID = undefined,
sprite_pipeline: *gfx.SpritePipeline.Mod, text_pipeline_id: mach.ObjectID = undefined,
core: *mach.Core, sfx: mach.Audio.Opus = undefined,
app: *App,
) !void {
// If you want to try fullscreen:
// try core.set(core.main_window, .fullscreen, true);
core.schedule(.init); pub fn init(
audio.schedule(.init);
text.schedule(.init);
text_pipeline.schedule(.init);
sprite_pipeline.schedule(.init);
app.schedule(.init);
}
fn init(
entities: *mach.Entities.Mod,
core: *mach.Core, core: *mach.Core,
audio: *mach.Audio, audio: *mach.Audio,
text_pipeline: *gfx.TextPipeline.Mod,
text_style: *gfx.TextStyle.Mod,
text: *gfx.Text.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod,
app: *App, app: *App,
app_mod: mach.Mod(App), app_mod: mach.Mod(App),
) !void { ) !void {
core.on_tick = app_mod.id.tick; core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit; core.on_exit = app_mod.id.deinit;
// Configure the audio module to run our audio_state_change system when entities audio finishes // Configure the audio module to call our App.audioStateChange function when a sound buffer
// playing // finishes playing.
audio.on_state_change = app_audio_state_change.id; audio.on_state_change = app_mod.id.audioStateChange;
// Create a sprite rendering pipeline const window = try core.windows.new(.{
const allocator = gpa.allocator(); .title = "hardware check",
const pipeline = try entities.new(); });
try sprite_pipeline.set(pipeline, .texture, try loadTexture(core, allocator));
sprite_pipeline.schedule(.update);
// TODO: a better way to initialize entities with default values // TODO(allocator): find a better way to get an allocator here
// TODO(text): ability to specify other style options (custom font name, font color, italic/bold, etc.) const allocator = std.heap.c_allocator;
const style1 = try entities.new();
try text_style.set(style1, .font_size, 48 * gfx.px_per_pt); // 48pt
// Create a text rendering pipeline app.* = .{
const text_rendering_pipeline = try entities.new(); .allocator = allocator,
try text_pipeline.set(text_rendering_pipeline, .is_pipeline, {}); .window = window,
text_pipeline.schedule(.update); .timer = try mach.time.Timer.start(),
.spawn_timer = try mach.time.Timer.start(),
.fps_timer = try mach.time.Timer.start(),
.rand = std.Random.DefaultPrng.init(1337),
};
}
// Create some text pub fn deinit2(
const text1 = try entities.new(); app: *App,
try text.set(text1, .pipeline, text_rendering_pipeline); text: *gfx.Text,
try text.set(text1, .transform, Mat4x4.translate(vec3(0, 0, 0))); ) void {
try gfx.Text.allocPrintText(text, text1, style1, // Cleanup here, if desired.
\\ Mach is probably working if you: text.objects.delete(app.info_text_id);
\\ * See this text }
\\ * See sprites to the left
\\ * Hear sounds when sprites die
\\ * Hold space and things go faster
, .{});
text.schedule(.update);
const win = core.windows.get(core.main_window).?; /// Called on the high-priority audio OS thread when the audio driver needs more audio samples, so
const window_height: f32 = @floatFromInt(win.height); /// this callback should be fast to respond.
const info_text = try entities.new(); pub fn audioStateChange(audio: *mach.Audio, app: *App) !void {
try text.set(info_text, .pipeline, text_rendering_pipeline); audio.buffers.lock();
try text.set(info_text, .transform, Mat4x4.translate(vec3(0, (window_height / 2.0) - 50.0, 0))); defer audio.buffers.unlock();
try gfx.Text.allocPrintText(text, info_text, style1, "[info]", .{});
// Find audio objects that are no longer playing
var buffers = audio.buffers.slice();
while (buffers.next()) |buf_id| {
if (audio.buffers.get(buf_id, .playing)) continue;
// Remove the audio buffer that is no longer playing
const samples = audio.buffers.get(buf_id, .samples);
audio.buffers.delete(buf_id);
app.allocator.free(samples);
}
}
fn setupPipeline(
core: *mach.Core,
app: *App,
sprite: *gfx.Sprite,
text: *gfx.Text,
window_id: mach.ObjectID,
) !void {
const window = core.windows.getValue(window_id);
// Load sfx // Load sfx
const sfx_fbs = std.io.fixedBufferStream(assets.sfx.scifi_gun); const sfx_fbs = std.io.fixedBufferStream(assets.sfx.scifi_gun);
const sfx_sound_stream = std.io.StreamSource{ .const_buffer = sfx_fbs }; const sfx_sound_stream = std.io.StreamSource{ .const_buffer = sfx_fbs };
const sfx = try mach.Audio.Opus.decodeStream(gpa.allocator(), sfx_sound_stream); app.sfx = try mach.Audio.Opus.decodeStream(app.allocator, sfx_sound_stream);
app.init(.{ // Create a sprite rendering pipeline
.info_text = info_text, app.sprite_pipeline_id = try sprite.pipelines.new(.{
.info_text_style = style1, .window = window_id,
.timer = try mach.time.Timer.start(), .render_pass = undefined,
.spawn_timer = try mach.time.Timer.start(), .texture = try loadTexture(window.device, window.queue, app.allocator),
.fps_timer = try mach.time.Timer.start(),
.frame_count = 0,
.frame_rate = 0,
.num_sprites_spawned = 0,
.score = 0,
.rand = std.Random.DefaultPrng.init(1337),
.time = 0,
.allocator = allocator,
.pipeline = pipeline,
.sfx = sfx,
}); });
}
fn audioStateChange(entities: *mach.Entities.Mod) !void { // Create a text rendering pipeline
// Find audio entities that are no longer playing app.text_pipeline_id = try text.pipelines.new(.{
var q = try entities.query(.{ .window = window_id,
.ids = mach.Entities.Mod.read(.id), .render_pass = undefined,
.playings = mach.Audio.read(.playing),
}); });
while (q.next()) |v| {
for (v.ids, v.playings) |id, playing| {
if (playing) continue;
// Remove the entity for the old sound // Create a text style
try entities.remove(id); app.info_text_style_id = try text.styles.new(.{
} .font_size = 48 * gfx.px_per_pt, // 48pt
});
// Create documentation text
{
// TODO(text): release this memory somewhere
const text_value =
\\ Mach is probably working if you:
\\ * See this text
\\ * See sprites to the left
\\ * Hear sounds when sprites disappear
\\ * Hold space and things go faster
;
const text_buf = try app.allocator.alloc(u8, text_value.len);
@memcpy(text_buf, text_value);
const segments = try app.allocator.alloc(gfx.Text.Segment, 1);
segments[0] = .{
.text = text_buf,
.style = app.info_text_style_id,
};
// Create our player text
const text_id = try text.objects.new(.{
.transform = Mat4x4.translate(vec3(-0.02, 0, 0)),
.segments = segments,
});
// Attach the text object to our text rendering pipeline.
try text.pipelines.setParent(text_id, app.text_pipeline_id);
}
// Create info text to be updated dynamically later
{
// TODO(text): release this memory somewhere
const text_value = "[info]";
app.info_text = try app.allocator.alloc(u8, text_value.len);
@memcpy(app.info_text, text_value);
const segments = try app.allocator.alloc(gfx.Text.Segment, 1);
segments[0] = .{
.text = app.info_text,
.style = app.info_text_style_id,
};
// Create our player text
app.info_text_id = try text.objects.new(.{
.transform = Mat4x4.translate(vec3(0, (@as(f32, @floatFromInt(window.height)) / 2.0) - 50.0, 0)),
.segments = segments,
});
// Attach the text object to our text rendering pipeline.
try text.pipelines.setParent(app.info_text_id, app.sprite_pipeline_id);
} }
} }
fn tick( pub fn tick(
entities: *mach.Entities.Mod,
core: *mach.Core, core: *mach.Core,
sprite: *gfx.Sprite.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod,
text: *gfx.Text.Mod,
text_pipeline: *gfx.TextPipeline.Mod,
app: *App, app: *App,
sprite: *gfx.Sprite,
sprite_mod: mach.Mod(gfx.Sprite),
text: *gfx.Text,
text_mod: mach.Mod(gfx.Text),
audio: *mach.Audio, audio: *mach.Audio,
) !void { ) !void {
// TODO(important): event polling should occur in mach.Core module and get fired as ECS events. const label = @tagName(mach_module) ++ ".tick";
// TODO(Core) const window = core.windows.getValue(app.window);
var gotta_go_fast = app.gotta_go_fast;
while (core.nextEvent()) |event| { while (core.nextEvent()) |event| {
switch (event) { switch (event) {
.key_press => |ev| { .key_press => |ev| {
switch (ev.key) { switch (ev.key) {
.space => gotta_go_fast = true, .space => app.gotta_go_fast = true,
else => {}, else => {},
} }
}, },
.key_release => |ev| { .key_release => |ev| {
switch (ev.key) { switch (ev.key) {
.space => gotta_go_fast = false, .space => app.gotta_go_fast = false,
else => {}, else => {},
} }
}, },
.window_open => |ev| try setupPipeline(core, app, sprite, text, ev.window_id),
.close => core.exit(), .close => core.exit(),
else => {}, else => {},
} }
} }
app.gotta_go_fast = gotta_go_fast;
// Every second, update the frame rate // TODO(text): make updating text easier
if (app.fps_timer.read() >= 1.0) { app.allocator.free(app.info_text);
app.frame_rate = app.frame_count; app.info_text = try std.fmt.allocPrint(
app.fps_timer.reset(); app.allocator,
app.frame_count = 0;
}
try gfx.Text.allocPrintText(
text,
app.info_text,
app.info_text_style,
"[ FPS: {d} ]\n[ Sprites spawned: {d} ]", "[ FPS: {d} ]\n[ Sprites spawned: {d} ]",
.{ app.frame_rate, app.num_sprites_spawned }, .{ app.fps, app.num_sprites_spawned },
); );
text.schedule(.update); var segments: []gfx.Text.Segment = @constCast(text.objects.get(app.info_text_id, .segments));
segments[0] = .{
// var player_transform = sprite.get(app.player, .transform).?; .text = app.info_text,
// var player_pos = player_transform.translation(); .style = segments[0].style,
const win = core.windows.get(core.main_window).?; };
const window_width: f32 = @floatFromInt(win.width); text.objects.set(app.info_text_id, .segments, segments);
const entities_per_second: f32 = @floatFromInt( const entities_per_second: f32 = @floatFromInt(
app.rand.random().intRangeAtMost(usize, 0, if (gotta_go_fast) 50 else 10), app.rand.random().intRangeAtMost(usize, 0, if (app.gotta_go_fast) 50 else 10),
); );
if (app.spawn_timer.read() > 1.0 / entities_per_second) { if (app.spawn_timer.read() > 1.0 / entities_per_second) {
// Spawn new entities // Spawn new entities
_ = app.spawn_timer.lap(); _ = app.spawn_timer.lap();
var new_pos = vec3(-(window_width / 2), 0, 0); var new_pos = vec3(-(@as(f32, @floatFromInt(window.width)) / 2), 0, 0);
new_pos.v[1] += app.rand.random().floatNorm(f32) * 50; new_pos.v[1] += app.rand.random().floatNorm(f32) * 50;
const new_entity = try entities.new(); const new_sprite_id = try sprite.objects.new(.{
try sprite.set(new_entity, .transform, Mat4x4.translate(new_pos)); .transform = Mat4x4.translate(new_pos),
try sprite.set(new_entity, .size, vec2(32, 32)); .size = vec2(32, 32),
try sprite.set(new_entity, .uv_transform, Mat3x3.translate(vec2(0, 0))); .uv_transform = Mat3x3.translate(vec2(0, 0)),
try sprite.set(new_entity, .pipeline, app.pipeline); });
try sprite.pipelines.setParent(new_sprite_id, app.sprite_pipeline_id);
app.num_sprites_spawned += 1; app.num_sprites_spawned += 1;
} }
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate. // Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
const delta_time = app.timer.lap(); const delta_time = app.timer.lap();
// Move entities to the right, and make them smaller the further they travel // Move sprites to the right, and make them smaller the further they travel
var q = try entities.query(.{ var pipeline_children = try sprite.pipelines.getChildren(app.sprite_pipeline_id);
.ids = mach.Entities.Mod.read(.id), defer pipeline_children.deinit();
.transforms = gfx.Sprite.Mod.write(.transform), for (pipeline_children.items) |sprite_id| {
}); if (!sprite.objects.is(sprite_id)) continue;
while (q.next()) |v| { var s = sprite.objects.getValue(sprite_id);
for (v.ids, v.transforms) |id, *entity_transform| {
const location = entity_transform.*.translation();
const speed: f32 = if (gotta_go_fast) 2000 else 100;
const progression = std.math.clamp((location.v[0] + (window_width / 2.0)) / window_width, 0, 1);
const scale = mach.math.lerp(2, 0, progression);
if (progression >= 0.6) {
try entities.remove(id);
// Play a new SFX const location = s.transform.translation();
const e = try entities.new(); const speed: f32 = if (app.gotta_go_fast) 2000 else 100;
try audio.set(e, .samples, app.sfx.samples); const progression = std.math.clamp((location.v[0] + (@as(f32, @floatFromInt(window.height)) / 2.0)) / @as(f32, @floatFromInt(window.height)), 0, 1);
try audio.set(e, .channels, app.sfx.channels); const scale = mach.math.lerp(2, 0, progression);
try audio.set(e, .index, 0); if (progression >= 0.6) {
try audio.set(e, .playing, true); try sprite.pipelines.removeChild(app.sprite_pipeline_id, sprite_id);
app.score += 1; sprite.objects.delete(sprite_id);
} else {
var transform = Mat4x4.ident; // Play a new sound
transform = transform.mul(&Mat4x4.translate(location.add(&vec3(speed * delta_time, (speed / 2.0) * delta_time * progression, 0)))); const samples = try app.allocator.dupe(f32, app.sfx.samples);
transform = transform.mul(&Mat4x4.scaleScalar(scale)); @memcpy(samples, app.sfx.samples);
entity_transform.* = transform; audio.buffers.lock();
} defer audio.buffers.unlock();
const sound_id = try audio.buffers.new(.{
.samples = samples,
.channels = app.sfx.channels,
});
_ = sound_id; // autofix
app.score += 1;
} else {
var transform = Mat4x4.ident;
transform = transform.mul(&Mat4x4.translate(location.add(&vec3(speed * delta_time, (speed / 2.0) * delta_time * progression, 0))));
transform = transform.mul(&Mat4x4.scaleScalar(scale));
sprite.objects.set(sprite_id, .transform, transform);
} }
} }
sprite.schedule(.update);
// Perform pre-render work
sprite_pipeline.schedule(.pre_render);
text_pipeline.schedule(.pre_render);
// Create a command encoder for this frame
const label = @tagName(mach_module) ++ ".tick";
app.frame_encoder = core.device.createCommandEncoder(&.{ .label = label });
// Grab the back buffer of the swapchain // Grab the back buffer of the swapchain
// TODO(Core) // TODO(Core)
const back_buffer_view = core.swap_chain.getCurrentTextureView().?; const back_buffer_view = window.swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release(); defer back_buffer_view.release();
// Create a command encoder
const encoder = window.device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
// Begin render pass // Begin render pass
const sky_blue = gpu.Color{ .r = 0.776, .g = 0.988, .b = 1, .a = 1 }; const sky_blue = gpu.Color{ .r = 0.776, .g = 0.988, .b = 1, .a = 1 };
const color_attachments = [_]gpu.RenderPassColorAttachment{.{ const color_attachments = [_]gpu.RenderPassColorAttachment{.{
@ -292,43 +310,39 @@ fn tick(
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}}; }};
app.frame_render_pass = app.frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{ const render_pass = encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
.label = label, .label = label,
.color_attachments = &color_attachments, .color_attachments = &color_attachments,
})); }));
// Render our sprite batch // Render sprites
sprite_pipeline.state().render_pass = app.frame_render_pass; sprite.pipelines.set(app.sprite_pipeline_id, .render_pass, render_pass);
sprite_pipeline.schedule(.render); sprite_mod.call(.tick);
// Render our text batch // Render text
text_pipeline.state().render_pass = app.frame_render_pass; text.pipelines.set(app.text_pipeline_id, .render_pass, render_pass);
text_pipeline.schedule(.render); text_mod.call(.tick);
// Finish the frame once rendering is done.
app.schedule(.end_frame);
app.time += delta_time;
}
fn endFrame(app: *App, core: *mach.Core) !void {
// Finish render pass // Finish render pass
app.frame_render_pass.end(); render_pass.end();
const label = @tagName(mach_module) ++ ".endFrame"; var command = encoder.finish(&.{ .label = label });
var command = app.frame_encoder.finish(&.{ .label = label }); window.queue.submit(&[_]*gpu.CommandBuffer{command});
core.queue.submit(&[_]*gpu.CommandBuffer{command});
command.release(); command.release();
app.frame_encoder.release(); render_pass.release();
app.frame_render_pass.release();
app.frame_count += 1; app.frame_count += 1;
app.time += delta_time;
// Every second, update the window title with the FPS
if (app.fps_timer.read() >= 1.0) {
app.fps_timer.reset();
app.fps = app.frame_count;
app.frame_count = 0;
}
} }
// TODO: move this helper into gfx module // TODO(sprite): don't require users to copy / write this helper themselves
fn loadTexture(core: *mach.Core, allocator: std.mem.Allocator) !*gpu.Texture { fn loadTexture(device: *gpu.Device, queue: *gpu.Queue, allocator: std.mem.Allocator) !*gpu.Texture {
const device = core.device;
const queue = core.queue;
// Load the image from memory // Load the image from memory
var img = try zigimg.Image.fromMemory(allocator, assets.sprites_sheet_png); var img = try zigimg.Image.fromMemory(allocator, assets.sprites_sheet_png);
defer img.deinit(); defer img.deinit();

View file

@ -4,8 +4,8 @@ const mach = @import("mach");
// The set of Mach modules our application may use. // The set of Mach modules our application may use.
const Modules = mach.Modules(.{ const Modules = mach.Modules(.{
mach.Core, mach.Core,
mach.gfx.sprite_modules, mach.gfx.Sprite,
mach.gfx.text_modules, mach.gfx.Text,
mach.Audio, mach.Audio,
@import("App.zig"), @import("App.zig"),
}); });