examples/core: building without ECS
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
2a13c07d9e
commit
0e12857154
35 changed files with 1365 additions and 4176 deletions
|
|
@ -14,6 +14,12 @@ const Vec3 = math.Vec3;
|
|||
const Mat3x3 = math.Mat3x3;
|
||||
const Mat4x4 = math.Mat4x4;
|
||||
|
||||
const App = @This();
|
||||
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .start, .init, .deinit, .tick, .end_frame };
|
||||
|
||||
// TODO: banish global allocator
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
|
|
@ -32,31 +38,14 @@ pipeline: mach.EntityID,
|
|||
frame_encoder: *gpu.CommandEncoder = undefined,
|
||||
frame_render_pass: *gpu.RenderPassEncoder = undefined,
|
||||
|
||||
// Define the globally unique name of our module. You can use any name here, but keep in mind no
|
||||
// two modules in the program can have the same name.
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
.end_frame = .{ .handler = endFrame },
|
||||
};
|
||||
|
||||
fn deinit(
|
||||
core: *mach.Core.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
) !void {
|
||||
fn deinit(sprite_pipeline: *gfx.SpritePipeline.Mod) !void {
|
||||
sprite_pipeline.schedule(.deinit);
|
||||
core.schedule(.deinit);
|
||||
}
|
||||
|
||||
fn start(
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
) !void {
|
||||
core.schedule(.init);
|
||||
sprite_pipeline.schedule(.init);
|
||||
|
|
@ -65,13 +54,15 @@ fn start(
|
|||
|
||||
fn init(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// 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
|
||||
|
|
@ -103,20 +94,18 @@ fn init(
|
|||
.allocator = allocator,
|
||||
.pipeline = pipeline,
|
||||
});
|
||||
|
||||
core.schedule(.start);
|
||||
}
|
||||
|
||||
fn tick(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
) !void {
|
||||
var direction = app.state().direction;
|
||||
var spawning = app.state().spawning;
|
||||
while (core.state().nextEvent()) |event| {
|
||||
var direction = app.direction;
|
||||
var spawning = app.spawning;
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
switch (ev.key) {
|
||||
|
|
@ -138,34 +127,34 @@ fn tick(
|
|||
else => {},
|
||||
}
|
||||
},
|
||||
.close => core.schedule(.exit),
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
app.state().direction = direction;
|
||||
app.state().spawning = spawning;
|
||||
app.direction = direction;
|
||||
app.spawning = spawning;
|
||||
|
||||
var player_transform = sprite.get(app.state().player, .transform).?;
|
||||
var player_transform = sprite.get(app.player, .transform).?;
|
||||
var player_pos = player_transform.translation();
|
||||
if (spawning and app.state().spawn_timer.read() > 1.0 / 60.0) {
|
||||
if (spawning and app.spawn_timer.read() > 1.0 / 60.0) {
|
||||
// Spawn new entities
|
||||
_ = app.state().spawn_timer.lap();
|
||||
_ = app.spawn_timer.lap();
|
||||
for (0..100) |_| {
|
||||
var new_pos = player_pos;
|
||||
new_pos.v[0] += app.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += app.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[0] += app.rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += app.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, app.state().pipeline);
|
||||
app.state().sprites += 1;
|
||||
try sprite.set(new_entity, .pipeline, app.pipeline);
|
||||
app.sprites += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
|
||||
const delta_time = app.state().timer.lap();
|
||||
const delta_time = app.timer.lap();
|
||||
|
||||
// Rotate entities
|
||||
var q = try entities.query(.{
|
||||
|
|
@ -179,8 +168,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 * app.state().time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.state().time / 2.0), 0.5)));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.time / 2.0), 0.5)));
|
||||
entity_transform.* = transform;
|
||||
}
|
||||
}
|
||||
|
|
@ -190,19 +179,19 @@ 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(app.state().player, .transform, Mat4x4.translate(player_pos));
|
||||
try sprite.set(app.player, .transform, Mat4x4.translate(player_pos));
|
||||
sprite.schedule(.update);
|
||||
|
||||
// Perform pre-render work
|
||||
sprite_pipeline.schedule(.pre_render);
|
||||
|
||||
// Create a command encoder for this frame
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
app.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
app.frame_encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -213,52 +202,49 @@ fn tick(
|
|||
.load_op = .clear,
|
||||
.store_op = .store,
|
||||
}};
|
||||
app.state().frame_render_pass = app.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
app.frame_render_pass = app.frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
.label = label,
|
||||
.color_attachments = &color_attachments,
|
||||
}));
|
||||
|
||||
// Render our sprite batch
|
||||
sprite_pipeline.state().render_pass = app.state().frame_render_pass;
|
||||
sprite_pipeline.state().render_pass = app.frame_render_pass;
|
||||
sprite_pipeline.schedule(.render);
|
||||
|
||||
// Finish the frame once rendering is done.
|
||||
app.schedule(.end_frame);
|
||||
|
||||
app.state().time += delta_time;
|
||||
app.time += delta_time;
|
||||
}
|
||||
|
||||
fn endFrame(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
fn endFrame(app: *App, core: *mach.Core) !void {
|
||||
// Finish render pass
|
||||
app.state().frame_render_pass.end();
|
||||
const label = @tagName(name) ++ ".endFrame";
|
||||
var command = app.state().frame_encoder.finish(&.{ .label = label });
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
app.frame_render_pass.end();
|
||||
const label = @tagName(mach_module) ++ ".endFrame";
|
||||
var command = app.frame_encoder.finish(&.{ .label = label });
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
command.release();
|
||||
app.state().frame_encoder.release();
|
||||
app.state().frame_render_pass.release();
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
app.frame_encoder.release();
|
||||
app.frame_render_pass.release();
|
||||
|
||||
// Every second, update the window title with the FPS
|
||||
if (app.state().fps_timer.read() >= 1.0) {
|
||||
try core.state().printTitle(
|
||||
core.state().main_window,
|
||||
if (app.fps_timer.read() >= 1.0) {
|
||||
try core.printTitle(
|
||||
core.main_window,
|
||||
"sprite [ FPS: {d} ] [ Sprites: {d} ]",
|
||||
.{ app.state().frame_count, app.state().sprites },
|
||||
.{ app.frame_count, app.sprites },
|
||||
);
|
||||
core.schedule(.update);
|
||||
app.state().fps_timer.reset();
|
||||
app.state().frame_count = 0;
|
||||
app.fps_timer.reset();
|
||||
app.frame_count = 0;
|
||||
}
|
||||
app.state().frame_count += 1;
|
||||
app.frame_count += 1;
|
||||
}
|
||||
|
||||
// TODO: move this helper into gfx module
|
||||
fn loadTexture(core: *mach.Core.Mod, allocator: std.mem.Allocator) !*gpu.Texture {
|
||||
const device = core.state().device;
|
||||
const queue = core.state().queue;
|
||||
fn loadTexture(core: *mach.Core, allocator: std.mem.Allocator) !*gpu.Texture {
|
||||
const device = core.device;
|
||||
const queue = core.queue;
|
||||
|
||||
// Load the image from memory
|
||||
var img = try zigimg.Image.fromMemory(allocator, assets.sprites_sheet_png);
|
||||
|
|
@ -266,7 +252,7 @@ fn loadTexture(core: *mach.Core.Mod, allocator: std.mem.Allocator) !*gpu.Texture
|
|||
const img_size = gpu.Extent3D{ .width = @as(u32, @intCast(img.width)), .height = @as(u32, @intCast(img.height)) };
|
||||
|
||||
// Create a GPU texture
|
||||
const label = @tagName(name) ++ ".loadTexture";
|
||||
const label = @tagName(mach_module) ++ ".loadTexture";
|
||||
const texture = device.createTexture(&.{
|
||||
.label = label,
|
||||
.size = img_size,
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
mach.gfx.sprite_modules,
|
||||
@import("App.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue