examples: text example updated to new object system
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
68251d95b7
commit
eda3e68b6e
3 changed files with 147 additions and 144 deletions
|
|
@ -80,7 +80,7 @@ pub fn build(b: *std.Build) !void {
|
||||||
.{ .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 } },
|
||||||
// .{ .name = "text", .deps = &.{.assets} },
|
.{ .name = "text", .deps = &.{.assets} },
|
||||||
};
|
};
|
||||||
|
|
||||||
var sysaudio_tests = [_]SysAudioTest{
|
var sysaudio_tests = [_]SysAudioTest{
|
||||||
|
|
|
||||||
|
|
@ -18,21 +18,29 @@ const App = @This();
|
||||||
|
|
||||||
pub const mach_module = .app;
|
pub const mach_module = .app;
|
||||||
|
|
||||||
pub const mach_systems = .{ .start, .init, .deinit, .tick, .end_frame };
|
pub const mach_systems = .{ .main, .init, .tick, .deinit };
|
||||||
|
|
||||||
|
pub const main = mach.schedule(.{
|
||||||
|
.{ mach.Core, .init },
|
||||||
|
.{ gfx.Text, .init },
|
||||||
|
.{ App, .init },
|
||||||
|
.{ mach.Core, .main },
|
||||||
|
});
|
||||||
|
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
window: mach.ObjectID,
|
||||||
timer: mach.time.Timer,
|
timer: mach.time.Timer,
|
||||||
player: mach.EntityID,
|
|
||||||
direction: Vec2 = vec2(0, 0),
|
|
||||||
spawning: 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,
|
|
||||||
rand: std.Random.DefaultPrng,
|
rand: std.Random.DefaultPrng,
|
||||||
time: f32,
|
|
||||||
style1: mach.EntityID,
|
frame_count: usize = 0,
|
||||||
pipeline: mach.EntityID,
|
time: f32 = 0,
|
||||||
frame_encoder: *gpu.CommandEncoder = undefined,
|
direction: Vec2 = vec2(0, 0),
|
||||||
frame_render_pass: *gpu.RenderPassEncoder = undefined,
|
spawning: bool = false,
|
||||||
|
player_id: mach.ObjectID = undefined,
|
||||||
|
style1_id: mach.ObjectID = undefined,
|
||||||
|
pipeline_id: mach.ObjectID = undefined,
|
||||||
|
|
||||||
const upscale = 1.0;
|
const upscale = 1.0;
|
||||||
|
|
||||||
|
|
@ -44,75 +52,78 @@ const text1: []const []const u8 = &.{
|
||||||
|
|
||||||
const text2: []const []const u8 = &.{"$!?"};
|
const text2: []const []const u8 = &.{"$!?"};
|
||||||
|
|
||||||
fn deinit(text_pipeline: *gfx.TextPipeline.Mod) !void {
|
pub fn init(
|
||||||
text_pipeline.schedule(.deinit);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn start(
|
|
||||||
core: *mach.Core,
|
core: *mach.Core,
|
||||||
text: *gfx.Text.Mod,
|
|
||||||
text_pipeline: *gfx.TextPipeline.Mod,
|
|
||||||
app: *App,
|
|
||||||
) !void {
|
|
||||||
core.schedule(.init);
|
|
||||||
text.schedule(.init);
|
|
||||||
text_pipeline.schedule(.init);
|
|
||||||
app.schedule(.init);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init(
|
|
||||||
entities: *mach.Entities.Mod,
|
|
||||||
core: *mach.Core,
|
|
||||||
text: *gfx.Text.Mod,
|
|
||||||
text_pipeline: *gfx.TextPipeline.Mod,
|
|
||||||
text_style: *gfx.TextStyle.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;
|
||||||
|
|
||||||
// TODO: a better way to initialize entities with default values
|
const window = try core.windows.new(.{
|
||||||
// TODO(text): ability to specify other style options (custom font name, font color, italic/bold, etc.)
|
.title = "gfx.Text",
|
||||||
const style1 = try entities.new();
|
});
|
||||||
try text_style.set(style1, .font_size, 48 * gfx.px_per_pt); // 48pt
|
|
||||||
|
|
||||||
// Create a text rendering pipeline
|
// TODO(allocator): find a better way to get an allocator here
|
||||||
const pipeline = try entities.new();
|
const allocator = std.heap.c_allocator;
|
||||||
try text_pipeline.set(pipeline, .is_pipeline, {});
|
|
||||||
text_pipeline.schedule(.update);
|
|
||||||
|
|
||||||
// Create some text
|
app.* = .{
|
||||||
const player = try entities.new();
|
.allocator = allocator,
|
||||||
try text.set(player, .pipeline, pipeline);
|
.window = window,
|
||||||
try text.set(player, .transform, Mat4x4.translate(vec3(0, 0, 0)));
|
|
||||||
try gfx.Text.allocPrintText(text, player, style1,
|
|
||||||
\\ Text with spaces
|
|
||||||
\\ and newlines
|
|
||||||
\\ but nothing fancy yet
|
|
||||||
, .{});
|
|
||||||
text.schedule(.update);
|
|
||||||
|
|
||||||
app.init(.{
|
|
||||||
.timer = try mach.time.Timer.start(),
|
.timer = try mach.time.Timer.start(),
|
||||||
.spawn_timer = try mach.time.Timer.start(),
|
.spawn_timer = try mach.time.Timer.start(),
|
||||||
.player = player,
|
|
||||||
.fps_timer = try mach.time.Timer.start(),
|
.fps_timer = try mach.time.Timer.start(),
|
||||||
.frame_count = 0,
|
|
||||||
.rand = std.Random.DefaultPrng.init(1337),
|
.rand = std.Random.DefaultPrng.init(1337),
|
||||||
.time = 0,
|
};
|
||||||
.style1 = style1,
|
|
||||||
.pipeline = pipeline,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tick(
|
fn setupPipeline(
|
||||||
entities: *mach.Entities.Mod,
|
|
||||||
core: *mach.Core,
|
|
||||||
text: *gfx.Text.Mod,
|
|
||||||
text_pipeline: *gfx.TextPipeline.Mod,
|
|
||||||
app: *App,
|
app: *App,
|
||||||
|
text: *gfx.Text,
|
||||||
|
window_id: mach.ObjectID,
|
||||||
) !void {
|
) !void {
|
||||||
|
// Create a text rendering pipeline
|
||||||
|
app.pipeline_id = try text.pipelines.new(.{
|
||||||
|
.window = window_id,
|
||||||
|
.render_pass = undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a text style
|
||||||
|
app.style1_id = try text.styles.new(.{
|
||||||
|
.font_size = 48 * gfx.px_per_pt, // 48pt
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO(text): release this memory somewhere
|
||||||
|
const player_text_value =
|
||||||
|
\\ Text with spaces
|
||||||
|
\\ and newlines
|
||||||
|
\\ but nothing fancy (yet)
|
||||||
|
;
|
||||||
|
const player_text = try app.allocator.alloc(u8, player_text_value.len);
|
||||||
|
@memcpy(player_text, player_text_value);
|
||||||
|
const player_segments = try app.allocator.alloc(gfx.Text.Segment, 1);
|
||||||
|
player_segments[0] = .{
|
||||||
|
.text = player_text,
|
||||||
|
.style = app.style1_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create our player text
|
||||||
|
app.player_id = try text.objects.new(.{
|
||||||
|
.transform = Mat4x4.translate(vec3(-0.02, 0, 0)),
|
||||||
|
.segments = player_segments,
|
||||||
|
});
|
||||||
|
// Attach the text object to our text rendering pipeline.
|
||||||
|
try text.pipelines.setParent(app.player_id, app.pipeline_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tick(
|
||||||
|
core: *mach.Core,
|
||||||
|
app: *App,
|
||||||
|
text: *gfx.Text,
|
||||||
|
text_mod: mach.Mod(gfx.Text),
|
||||||
|
) !void {
|
||||||
|
const label = @tagName(mach_module) ++ ".tick";
|
||||||
|
|
||||||
var direction = app.direction;
|
var direction = app.direction;
|
||||||
var spawning = app.spawning;
|
var spawning = app.spawning;
|
||||||
while (core.nextEvent()) |event| {
|
while (core.nextEvent()) |event| {
|
||||||
|
|
@ -137,6 +148,7 @@ fn tick(
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
.window_open => |ev| try setupPipeline(app, text, ev.window_id),
|
||||||
.close => core.exit(),
|
.close => core.exit(),
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
@ -144,9 +156,9 @@ fn tick(
|
||||||
app.direction = direction;
|
app.direction = direction;
|
||||||
app.spawning = spawning;
|
app.spawning = spawning;
|
||||||
|
|
||||||
var player_transform = text.get(app.player, .transform).?;
|
var player = text.objects.getValue(app.player_id);
|
||||||
var player_pos = player_transform.translation().divScalar(upscale);
|
var player_pos = player.transform.translation();
|
||||||
if (spawning and app.spawn_timer.read() > (1.0 / 60.0)) {
|
if (spawning and app.spawn_timer.read() > 1.0 / 60.0) {
|
||||||
// Spawn new entities
|
// Spawn new entities
|
||||||
_ = app.spawn_timer.lap();
|
_ = app.spawn_timer.lap();
|
||||||
for (0..10) |_| {
|
for (0..10) |_| {
|
||||||
|
|
@ -154,56 +166,61 @@ fn tick(
|
||||||
new_pos.v[0] += app.rand.random().floatNorm(f32) * 50;
|
new_pos.v[0] += app.rand.random().floatNorm(f32) * 50;
|
||||||
new_pos.v[1] += app.rand.random().floatNorm(f32) * 50;
|
new_pos.v[1] += app.rand.random().floatNorm(f32) * 50;
|
||||||
|
|
||||||
// Create some text
|
// TODO(text): release this memory somewhere
|
||||||
const new_entity = try entities.new();
|
const new_text_value = "?!";
|
||||||
try text.set(new_entity, .pipeline, app.pipeline);
|
const new_text = try app.allocator.alloc(u8, new_text_value.len);
|
||||||
try text.set(new_entity, .transform, Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(new_pos)));
|
@memcpy(new_text, new_text_value);
|
||||||
try gfx.Text.allocPrintText(text, new_entity, app.style1, "?!$", .{});
|
const new_segments = try app.allocator.alloc(gfx.Text.Segment, 1);
|
||||||
|
new_segments[0] = .{
|
||||||
|
.text = new_text,
|
||||||
|
.style = app.style1_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
const new_text_id = try text.objects.new(.{
|
||||||
|
.transform = Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(new_pos)),
|
||||||
|
.segments = new_segments,
|
||||||
|
});
|
||||||
|
try text.pipelines.setParent(new_text_id, app.pipeline_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
// Rotate entities
|
// Rotate all text objects in the pipeline.
|
||||||
var q = try entities.query(.{
|
var pipeline_children = try text.pipelines.getChildren(app.pipeline_id);
|
||||||
.transforms = gfx.Text.Mod.write(.transform),
|
defer pipeline_children.deinit();
|
||||||
});
|
for (pipeline_children.items) |text_id| {
|
||||||
while (q.next()) |v| {
|
if (!text.objects.is(text_id)) continue;
|
||||||
for (v.transforms) |*entity_transform| {
|
if (text_id == app.player_id) continue; // don't rotate the player
|
||||||
const location = entity_transform.*.translation();
|
var s = text.objects.getValue(text_id);
|
||||||
// var transform = old_transform.mul(&Mat4x4.translate(-location));
|
|
||||||
// transform = mat.rotateZ(0.3 * delta_time).mul(&transform);
|
const location = s.transform.translation();
|
||||||
// transform = transform.mul(&Mat4x4.translate(location));
|
var transform = Mat4x4.ident;
|
||||||
var transform = Mat4x4.ident;
|
transform = transform.mul(&Mat4x4.translate(location));
|
||||||
transform = transform.mul(&Mat4x4.translate(location));
|
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.time));
|
||||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.time));
|
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.time / 2.0), 0.5)));
|
||||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.time / 2.0), 0.5)));
|
text.objects.set(text_id, .transform, transform);
|
||||||
entity_transform.* = transform;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the player position, by moving in the direction the player wants to go
|
// Calculate the player position, by moving in the direction the player wants to go
|
||||||
// by the speed amount.
|
// by the speed amount.
|
||||||
const speed = 200.0 / upscale;
|
const speed = 200.0;
|
||||||
player_pos.v[0] += direction.x() * speed * delta_time;
|
player_pos.v[0] += direction.x() * speed * delta_time;
|
||||||
player_pos.v[1] += direction.y() * speed * delta_time;
|
player_pos.v[1] += direction.y() * speed * delta_time;
|
||||||
try text.set(app.player, .transform, Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(player_pos)));
|
text.objects.set(app.player_id, .transform, Mat4x4.translate(player_pos));
|
||||||
try text.set(app.player, .dirty, true);
|
|
||||||
text.schedule(.update);
|
|
||||||
|
|
||||||
// Perform pre-render work
|
const window = core.windows.getValue(app.window);
|
||||||
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{.{
|
||||||
|
|
@ -212,58 +229,44 @@ 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 text batch
|
// Render text
|
||||||
text_pipeline.state().render_pass = app.frame_render_pass;
|
text.pipelines.set(app.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(
|
|
||||||
entities: *mach.Entities.Mod,
|
|
||||||
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();
|
|
||||||
|
|
||||||
// Every second, update the window title with the FPS
|
|
||||||
if (app.fps_timer.read() >= 1.0) {
|
|
||||||
// Gather some text rendering stats
|
|
||||||
var num_texts: u32 = 0;
|
|
||||||
var num_glyphs: usize = 0;
|
|
||||||
var q = try entities.query(.{
|
|
||||||
.built_pipelines = gfx.Text.Mod.read(.built),
|
|
||||||
});
|
|
||||||
while (q.next()) |v| {
|
|
||||||
for (v.built_pipelines) |built| {
|
|
||||||
num_texts += 1;
|
|
||||||
num_glyphs += built.glyphs.items.len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try core.printTitle(
|
|
||||||
core.main_window,
|
|
||||||
"text [ FPS: {d} ] [ Texts: {d} ] [ Glyphs: {d} ]",
|
|
||||||
.{ app.frame_count, num_texts, num_glyphs },
|
|
||||||
);
|
|
||||||
core.schedule(.update);
|
|
||||||
app.fps_timer.reset();
|
|
||||||
app.frame_count = 0;
|
|
||||||
}
|
|
||||||
app.frame_count += 1;
|
app.frame_count += 1;
|
||||||
|
app.time += delta_time;
|
||||||
|
|
||||||
|
// TODO(object): window-title
|
||||||
|
// // Every second, update the window title with the FPS
|
||||||
|
// if (app.fps_timer.read() >= 1.0) {
|
||||||
|
// const pipeline = text.pipelines.getValue(app.pipeline_id);
|
||||||
|
// try core.printTitle(
|
||||||
|
// core.main_window,
|
||||||
|
// "text [ FPS: {d} ] [ Texts: {d} ] [ Segments: {d} ] [ Styles: {d} ]",
|
||||||
|
// .{ app.frame_count, pipeline.num_texts, pipeline.num_segments, pipeline.num_styles },
|
||||||
|
// );
|
||||||
|
// core.schedule(.update);
|
||||||
|
// app.fps_timer.reset();
|
||||||
|
// app.frame_count = 0;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(
|
||||||
|
app: *App,
|
||||||
|
text: *gfx.Text,
|
||||||
|
) void {
|
||||||
|
// Cleanup here, if desired.
|
||||||
|
text.objects.delete(app.player_id);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ 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.text_modules,
|
mach.gfx.Text,
|
||||||
@import("App.zig"),
|
@import("App.zig"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue