make it clear how to use module system without mach.Core (remove mach.App)
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
7ac5bef717
commit
642cc9b7f7
20 changed files with 567 additions and 334 deletions
|
|
@ -38,9 +38,9 @@ pub const name = .app;
|
|||
pub const Mod = mach.Mod(@This());
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.after_init = .{ .handler = afterInit },
|
||||
.tick = .{ .handler = tick },
|
||||
.end_frame = .{ .handler = endFrame },
|
||||
};
|
||||
|
|
@ -53,23 +53,26 @@ fn deinit(
|
|||
core.schedule(.deinit);
|
||||
}
|
||||
|
||||
fn init(
|
||||
fn start(
|
||||
core: *mach.Core.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
game: *Mod,
|
||||
app: *Mod,
|
||||
) !void {
|
||||
core.schedule(.init);
|
||||
sprite_pipeline.schedule(.init);
|
||||
game.schedule(.after_init);
|
||||
app.schedule(.init);
|
||||
}
|
||||
|
||||
fn afterInit(
|
||||
fn init(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
game: *Mod,
|
||||
app: *Mod,
|
||||
) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
|
||||
// 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.
|
||||
|
|
@ -88,7 +91,7 @@ fn afterInit(
|
|||
try sprite.set(player, .pipeline, pipeline);
|
||||
sprite.schedule(.update);
|
||||
|
||||
game.init(.{
|
||||
app.init(.{
|
||||
.timer = try mach.Timer.start(),
|
||||
.spawn_timer = try mach.Timer.start(),
|
||||
.player = player,
|
||||
|
|
@ -109,13 +112,13 @@ fn tick(
|
|||
core: *mach.Core.Mod,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
game: *Mod,
|
||||
app: *Mod,
|
||||
) !void {
|
||||
// TODO(important): event polling should occur in mach.Core module and get fired as ECS events.
|
||||
// TODO(Core)
|
||||
var iter = core.state().pollEvents();
|
||||
var direction = game.state().direction;
|
||||
var spawning = game.state().spawning;
|
||||
var direction = app.state().direction;
|
||||
var spawning = app.state().spawning;
|
||||
while (iter.next()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
|
|
@ -142,30 +145,30 @@ fn tick(
|
|||
else => {},
|
||||
}
|
||||
}
|
||||
game.state().direction = direction;
|
||||
game.state().spawning = spawning;
|
||||
app.state().direction = direction;
|
||||
app.state().spawning = spawning;
|
||||
|
||||
var player_transform = sprite.get(game.state().player, .transform).?;
|
||||
var player_transform = sprite.get(app.state().player, .transform).?;
|
||||
var player_pos = player_transform.translation();
|
||||
if (spawning and game.state().spawn_timer.read() > 1.0 / 60.0) {
|
||||
if (spawning and app.state().spawn_timer.read() > 1.0 / 60.0) {
|
||||
// Spawn new entities
|
||||
_ = game.state().spawn_timer.lap();
|
||||
_ = app.state().spawn_timer.lap();
|
||||
for (0..100) |_| {
|
||||
var new_pos = player_pos;
|
||||
new_pos.v[0] += game.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += game.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[0] += app.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += app.state().rand.random().floatNorm(f32) * 25;
|
||||
|
||||
const new_entity = try entities.new();
|
||||
try sprite.set(new_entity, .transform, Mat4x4.translate(new_pos).mul(&Mat4x4.scale(Vec3.splat(0.3))));
|
||||
try sprite.set(new_entity, .size, vec2(32, 32));
|
||||
try sprite.set(new_entity, .uv_transform, Mat3x3.translate(vec2(0, 0)));
|
||||
try sprite.set(new_entity, .pipeline, game.state().pipeline);
|
||||
game.state().sprites += 1;
|
||||
try sprite.set(new_entity, .pipeline, app.state().pipeline);
|
||||
app.state().sprites += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
|
||||
const delta_time = game.state().timer.lap();
|
||||
const delta_time = app.state().timer.lap();
|
||||
|
||||
// Rotate entities
|
||||
var q = try entities.query(.{
|
||||
|
|
@ -179,8 +182,8 @@ fn tick(
|
|||
// transform = transform.mul(&Mat4x4.translate(location));
|
||||
var transform = Mat4x4.ident;
|
||||
transform = transform.mul(&Mat4x4.translate(location));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * game.state().time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(game.state().time / 2.0), 0.5)));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.state().time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.state().time / 2.0), 0.5)));
|
||||
entity_transform.* = transform;
|
||||
}
|
||||
}
|
||||
|
|
@ -190,7 +193,7 @@ fn tick(
|
|||
const speed = 200.0;
|
||||
player_pos.v[0] += direction.x() * speed * delta_time;
|
||||
player_pos.v[1] += direction.y() * speed * delta_time;
|
||||
try sprite.set(game.state().player, .transform, Mat4x4.translate(player_pos));
|
||||
try sprite.set(app.state().player, .transform, Mat4x4.translate(player_pos));
|
||||
sprite.schedule(.update);
|
||||
|
||||
// Perform pre-render work
|
||||
|
|
@ -198,7 +201,7 @@ fn tick(
|
|||
|
||||
// Create a command encoder for this frame
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
game.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
app.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
|
|
@ -213,46 +216,46 @@ fn tick(
|
|||
.load_op = .clear,
|
||||
.store_op = .store,
|
||||
}};
|
||||
game.state().frame_render_pass = game.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
app.state().frame_render_pass = app.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
.label = label,
|
||||
.color_attachments = &color_attachments,
|
||||
}));
|
||||
|
||||
// Render our sprite batch
|
||||
sprite_pipeline.state().render_pass = game.state().frame_render_pass;
|
||||
sprite_pipeline.state().render_pass = app.state().frame_render_pass;
|
||||
sprite_pipeline.schedule(.render);
|
||||
|
||||
// Finish the frame once rendering is done.
|
||||
game.schedule(.end_frame);
|
||||
app.schedule(.end_frame);
|
||||
|
||||
game.state().time += delta_time;
|
||||
app.state().time += delta_time;
|
||||
}
|
||||
|
||||
fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
|
||||
fn endFrame(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
// Finish render pass
|
||||
game.state().frame_render_pass.end();
|
||||
app.state().frame_render_pass.end();
|
||||
const label = @tagName(name) ++ ".endFrame";
|
||||
var command = game.state().frame_encoder.finish(&.{ .label = label });
|
||||
var command = app.state().frame_encoder.finish(&.{ .label = label });
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
command.release();
|
||||
game.state().frame_encoder.release();
|
||||
game.state().frame_render_pass.release();
|
||||
app.state().frame_encoder.release();
|
||||
app.state().frame_render_pass.release();
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
|
||||
// Every second, update the window title with the FPS
|
||||
if (game.state().fps_timer.read() >= 1.0) {
|
||||
if (app.state().fps_timer.read() >= 1.0) {
|
||||
try core.state().printTitle(
|
||||
core.state().main_window,
|
||||
"sprite [ FPS: {d} ] [ Sprites: {d} ]",
|
||||
.{ game.state().frame_count, game.state().sprites },
|
||||
.{ app.state().frame_count, app.state().sprites },
|
||||
);
|
||||
core.schedule(.update);
|
||||
game.state().fps_timer.reset();
|
||||
game.state().frame_count = 0;
|
||||
app.state().fps_timer.reset();
|
||||
app.state().frame_count = 0;
|
||||
}
|
||||
game.state().frame_count += 1;
|
||||
app.state().frame_count += 1;
|
||||
}
|
||||
|
||||
// TODO: move this helper into gfx module
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue