editor: update to latest mach-ecs API
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
2b9ec3ba4c
commit
35742d1591
1 changed files with 33 additions and 35 deletions
|
|
@ -4,7 +4,7 @@ const core = mach.core;
|
||||||
const gpu = mach.gpu;
|
const gpu = mach.gpu;
|
||||||
|
|
||||||
pub const name = .editor;
|
pub const name = .editor;
|
||||||
pub const modules = .{ mach.Module, @This() };
|
pub const modules = .{ mach.Engine, @This() };
|
||||||
pub const App = mach.App;
|
pub const App = mach.App;
|
||||||
|
|
||||||
const UniformBufferObject = struct {
|
const UniformBufferObject = struct {
|
||||||
|
|
@ -25,9 +25,7 @@ fragment_shader_file: std.fs.File,
|
||||||
fragment_shader_code: [:0]const u8,
|
fragment_shader_code: [:0]const u8,
|
||||||
last_mtime: i128,
|
last_mtime: i128,
|
||||||
|
|
||||||
pub fn init(eng: *mach.Engine) !void {
|
pub fn init(editor: *mach.Mod(.editor)) !void {
|
||||||
const editor = &eng.mod.editor.state;
|
|
||||||
|
|
||||||
core.setTitle("Mach editor");
|
core.setTitle("Mach editor");
|
||||||
|
|
||||||
var fragment_file: std.fs.File = undefined;
|
var fragment_file: std.fs.File = undefined;
|
||||||
|
|
@ -69,55 +67,55 @@ pub fn init(eng: *mach.Engine) !void {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
editor.timer = try mach.Timer.start();
|
editor.state.timer = try mach.Timer.start();
|
||||||
|
|
||||||
editor.pipeline = pipeline;
|
editor.state.pipeline = pipeline;
|
||||||
editor.queue = queue;
|
editor.state.queue = queue;
|
||||||
editor.uniform_buffer = uniform_buffer;
|
editor.state.uniform_buffer = uniform_buffer;
|
||||||
editor.bind_group = bind_group;
|
editor.state.bind_group = bind_group;
|
||||||
|
|
||||||
editor.fragment_shader_file = fragment_file;
|
editor.state.fragment_shader_file = fragment_file;
|
||||||
editor.fragment_shader_code = code;
|
editor.state.fragment_shader_code = code;
|
||||||
editor.last_mtime = last_mtime;
|
editor.state.last_mtime = last_mtime;
|
||||||
|
|
||||||
bgl.release();
|
bgl.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(eng: *mach.Engine) !void {
|
pub fn deinit(editor: *mach.Mod(.editor)) !void {
|
||||||
const editor = &eng.mod.editor.state;
|
|
||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
|
|
||||||
editor.fragment_shader_file.close();
|
editor.state.fragment_shader_file.close();
|
||||||
allocator.free(editor.fragment_shader_code);
|
allocator.free(editor.state.fragment_shader_code);
|
||||||
|
|
||||||
editor.uniform_buffer.release();
|
editor.state.uniform_buffer.release();
|
||||||
editor.bind_group.release();
|
editor.state.bind_group.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tick(eng: *mach.Engine) !void {
|
pub fn tick(
|
||||||
const editor = &eng.mod.editor.state;
|
engine: *mach.Mod(.engine),
|
||||||
|
editor: *mach.Mod(.editor),
|
||||||
|
) !void {
|
||||||
var iter = core.pollEvents();
|
var iter = core.pollEvents();
|
||||||
while (iter.next()) |event| {
|
while (iter.next()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
if (ev.key == .space) return eng.send(.machExit);
|
if (ev.key == .space) return engine.send(.exit, .{});
|
||||||
},
|
},
|
||||||
.close => return eng.send(.machExit),
|
.close => return engine.send(.exit, .{}),
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (editor.fragment_shader_file.stat()) |stat| {
|
if (editor.state.fragment_shader_file.stat()) |stat| {
|
||||||
if (editor.last_mtime < stat.mtime) {
|
if (editor.state.last_mtime < stat.mtime) {
|
||||||
std.log.info("The fragment shader has been changed", .{});
|
std.log.info("The fragment shader has been changed", .{});
|
||||||
editor.last_mtime = stat.mtime;
|
editor.state.last_mtime = stat.mtime;
|
||||||
editor.fragment_shader_file.seekTo(0) catch unreachable;
|
editor.state.fragment_shader_file.seekTo(0) catch unreachable;
|
||||||
editor.fragment_shader_code = editor.fragment_shader_file.readToEndAllocOptions(allocator, std.math.maxInt(u32), null, 1, 0) catch |err| {
|
editor.state.fragment_shader_code = editor.state.fragment_shader_file.readToEndAllocOptions(allocator, std.math.maxInt(u32), null, 1, 0) catch |err| {
|
||||||
std.log.err("Err: {}", .{err});
|
std.log.err("Err: {}", .{err});
|
||||||
return eng.send(.machExit);
|
return engine.send(.exit, .{});
|
||||||
};
|
};
|
||||||
editor.pipeline = recreatePipeline(editor.fragment_shader_code, null);
|
editor.state.pipeline = recreatePipeline(editor.state.fragment_shader_code, null);
|
||||||
}
|
}
|
||||||
} else |err| {
|
} else |err| {
|
||||||
std.log.err("Something went wrong when attempting to stat file: {}\n", .{err});
|
std.log.err("Something went wrong when attempting to stat file: {}\n", .{err});
|
||||||
|
|
@ -136,16 +134,16 @@ pub fn tick(eng: *mach.Engine) !void {
|
||||||
.color_attachments = &.{color_attachment},
|
.color_attachments = &.{color_attachment},
|
||||||
});
|
});
|
||||||
|
|
||||||
const time = editor.timer.read() / @as(f32, std.time.ns_per_s);
|
const time = editor.state.timer.read() / @as(f32, std.time.ns_per_s);
|
||||||
const ubo = UniformBufferObject{
|
const ubo = UniformBufferObject{
|
||||||
.resolution = .{ @as(f32, @floatFromInt(core.descriptor.width)), @as(f32, @floatFromInt(core.descriptor.height)) },
|
.resolution = .{ @as(f32, @floatFromInt(core.descriptor.width)), @as(f32, @floatFromInt(core.descriptor.height)) },
|
||||||
.time = time,
|
.time = time,
|
||||||
};
|
};
|
||||||
encoder.writeBuffer(editor.uniform_buffer, 0, &[_]UniformBufferObject{ubo});
|
encoder.writeBuffer(editor.state.uniform_buffer, 0, &[_]UniformBufferObject{ubo});
|
||||||
|
|
||||||
const pass = encoder.beginRenderPass(&render_pass_info);
|
const pass = encoder.beginRenderPass(&render_pass_info);
|
||||||
pass.setPipeline(editor.pipeline);
|
pass.setPipeline(editor.state.pipeline);
|
||||||
pass.setBindGroup(0, editor.bind_group, &.{0});
|
pass.setBindGroup(0, editor.state.bind_group, &.{0});
|
||||||
pass.draw(3, 1, 0, 0);
|
pass.draw(3, 1, 0, 0);
|
||||||
pass.end();
|
pass.end();
|
||||||
pass.release();
|
pass.release();
|
||||||
|
|
@ -153,7 +151,7 @@ pub fn tick(eng: *mach.Engine) !void {
|
||||||
var command = encoder.finish(null);
|
var command = encoder.finish(null);
|
||||||
encoder.release();
|
encoder.release();
|
||||||
|
|
||||||
editor.queue.submit(&[_]*gpu.CommandBuffer{command});
|
editor.state.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||||
command.release();
|
command.release();
|
||||||
core.swap_chain.present();
|
core.swap_chain.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue