Skip to content

Commit

Permalink
all: update to global vs. local ECS change
Browse files Browse the repository at this point in the history
See hexops-graveyard/mach-ecs@ef06fb6

Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
emidoots committed Dec 17, 2023
1 parent 11aed4d commit 8ff30c9
Show file tree
Hide file tree
Showing 4 changed files with 669 additions and 662 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
},
.dependencies = .{
.mach_ecs = .{
.url = "https://pkg.machengine.org/mach-ecs/46fbc9175a70a8cc983b88f911aa076b625e0fb2.tar.gz",
.hash = "1220acdc7bdb09425ccdd92bb906c4e30e3391c95ab15049c946c98cc6643a7ad250",
.url = "https://pkg.machengine.org/mach-ecs/ef06fb647353356eff080ad3ec337e028b492d41.tar.gz",
.hash = "1220014851adb37d191430ac371cc467e0e0eb633b84b856c7a37e41a3149dea7ce8",
},
.mach_core = .{
.url = "https://pkg.machengine.org/mach-core/cce02fb96ca787378289c5855b381f0ca9f3e090.tar.gz",
Expand Down
157 changes: 80 additions & 77 deletions src/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,96 +16,99 @@ pub const Engine = struct {

pub const name = .engine;

pub fn engineInit(world: *World) !void {
core.allocator = allocator;
try core.init(.{});
const state = &world.mod.engine.state;
state.device = core.device;
state.queue = core.device.getQueue();
state.exit = false;
state.encoder = state.device.createCommandEncoder(&gpu.CommandEncoder.Descriptor{
.label = "engine.state.encoder",
});

try world.send(.init, .{});
}

pub fn engineDeinit(
world: *World,
engine: *World.Mod(.engine),
) !void {
// TODO: this triggers a device loss error, which we should handle correctly
// engine.state.device.release();
engine.state.queue.release();
try world.send(.deinit, .{});
core.deinit();
world.deinit();
_ = gpa.deinit();
}

pub fn engineExit(world: *World) !void {
try world.send(.exit, .{});
world.mod.engine.state.exit = true;
}

pub fn engineBeginPass(
engine: *World.Mod(.engine),
clear_color: gpu.Color,
) !void {
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();

// TODO: expose options
const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.clear_value = clear_color,
.load_op = .clear,
.store_op = .store,
};
const pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{color_attachment},
});

engine.state.pass = engine.state.encoder.beginRenderPass(&pass_info);
}

pub fn engineEndPass(
engine: *World.Mod(.engine),
) !void {
// End this pass
engine.state.pass.end();
engine.state.pass.release();

var command = engine.state.encoder.finish(null);
defer command.release();
engine.state.encoder.release();
engine.state.queue.submit(&[_]*gpu.CommandBuffer{command});

// Prepare for next pass
engine.state.encoder = engine.state.device.createCommandEncoder(&gpu.CommandEncoder.Descriptor{
.label = "engine.state.encoder",
});
}

pub fn enginePresent() !void {
core.swap_chain.present();
}
pub const local = struct {
pub fn init(world: *World) !void {
core.allocator = allocator;
try core.init(.{});
const state = &world.mod.engine.state;
state.device = core.device;
state.queue = core.device.getQueue();
state.exit = false;
state.encoder = state.device.createCommandEncoder(&gpu.CommandEncoder.Descriptor{
.label = "engine.state.encoder",
});

try world.send(null, .init, .{});
}

pub fn deinit(
world: *World,
engine: *World.Mod(.engine),
) !void {
// TODO: this triggers a device loss error, which we should handle correctly
// engine.state.device.release();
engine.state.queue.release();
try world.send(null, .deinit, .{});
core.deinit();
world.deinit();
_ = gpa.deinit();
}

// Engine module's exit handler
pub fn exit(world: *World) !void {
try world.send(null, .exit, .{});
world.mod.engine.state.exit = true;
}

pub fn beginPass(
engine: *World.Mod(.engine),
clear_color: gpu.Color,
) !void {
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();

// TODO: expose options
const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.clear_value = clear_color,
.load_op = .clear,
.store_op = .store,
};
const pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{color_attachment},
});

engine.state.pass = engine.state.encoder.beginRenderPass(&pass_info);
}

pub fn endPass(
engine: *World.Mod(.engine),
) !void {
// End this pass
engine.state.pass.end();
engine.state.pass.release();

var command = engine.state.encoder.finish(null);
defer command.release();
engine.state.encoder.release();
engine.state.queue.submit(&[_]*gpu.CommandBuffer{command});

// Prepare for next pass
engine.state.encoder = engine.state.device.createCommandEncoder(&gpu.CommandEncoder.Descriptor{
.label = "engine.state.encoder",
});
}

pub fn present() !void {
core.swap_chain.present();
}
};
};

pub const App = struct {
world: World,

pub fn init(app: *@This()) !void {
app.* = .{ .world = try World.init(allocator) };
try app.world.send(.engineInit, .{});
try app.world.send(.engine, .init, .{});
}

pub fn deinit(app: *@This()) void {
try app.world.send(.engineDeinit, .{});
try app.world.send(.engine, .deinit, .{});
}

pub fn update(app: *@This()) !bool {
try app.world.send(.tick, .{});
try app.world.send(null, .tick, .{});
return app.world.mod.engine.state.exit;
}
};
Expand Down
Loading

0 comments on commit 8ff30c9

Please sign in to comment.