core: Remove main_window in favor of users creating their own windows. Added event window_open which is called when the platform has finished initializing the window

This commit is contained in:
foxnne 2024-12-01 18:10:02 -06:00 committed by Emi Gutekanst
parent b33f85576e
commit ad5700cf48
4 changed files with 72 additions and 50 deletions

View file

@ -14,6 +14,7 @@ pub const main = mach.schedule(.{
.{ mach.Core, .main },
});
window: mach.ObjectID,
title_timer: mach.time.Timer,
pipeline: *gpu.RenderPipeline,
@ -25,10 +26,24 @@ pub fn init(
core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit;
const main_window = core.windows.getValue(core.main_window);
const window = try core.windows.new(.{
.title = "core-triangle",
});
// Store our render pipeline in our module's state, so we can access it later on.
app.* = .{
.window = window,
.title_timer = try mach.time.Timer.start(),
.pipeline = undefined,
};
}
fn setupPipeline(core: *mach.Core, app: *App, window_id: mach.ObjectID) !void {
var window = core.windows.getValue(window_id);
defer core.windows.setValueRaw(window_id, window);
// Create our shader module
const shader_module = main_window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
const shader_module = window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
defer shader_module.release();
// Blend state describes how rendered colors get blended
@ -36,7 +51,7 @@ pub fn init(
// Color target describes e.g. the pixel format of the window we are rendering to.
const color_target = gpu.ColorTargetState{
.format = main_window.framebuffer_format,
.format = window.framebuffer_format,
.blend = &blend,
};
@ -57,13 +72,7 @@ pub fn init(
.entry_point = "vertex_main",
},
};
const pipeline = main_window.device.createRenderPipeline(&pipeline_descriptor);
// Store our render pipeline in our module's state, so we can access it later on.
app.* = .{
.title_timer = try mach.time.Timer.start(),
.pipeline = pipeline,
};
app.pipeline = window.device.createRenderPipeline(&pipeline_descriptor);
}
// TODO(object): window-title
@ -72,19 +81,22 @@ pub fn init(
pub fn tick(app: *App, core: *mach.Core) void {
while (core.nextEvent()) |event| {
switch (event) {
.window_open => |ev| {
try setupPipeline(core, app, ev.window_id);
},
.key_press => |ev| {
switch (ev.key) {
.right => {
core.windows.set(core.main_window, .width, core.windows.get(core.main_window, .width) + 10);
core.windows.set(app.window, .width, core.windows.get(app.window, .width) + 10);
},
.left => {
core.windows.set(core.main_window, .width, core.windows.get(core.main_window, .width) - 10);
core.windows.set(app.window, .width, core.windows.get(app.window, .width) - 10);
},
.up => {
core.windows.set(core.main_window, .height, core.windows.get(core.main_window, .height) + 10);
core.windows.set(app.window, .height, core.windows.get(app.window, .height) + 10);
},
.down => {
core.windows.set(core.main_window, .height, core.windows.get(core.main_window, .height) - 10);
core.windows.set(app.window, .height, core.windows.get(app.window, .height) - 10);
},
else => {},
}
@ -94,17 +106,17 @@ pub fn tick(app: *App, core: *mach.Core) void {
}
}
const main_window = core.windows.getValue(core.main_window);
const window = core.windows.getValue(app.window);
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = main_window.swap_chain.getCurrentTextureView().?;
const back_buffer_view = window.swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Create a command encoder
const label = @tagName(mach_module) ++ ".tick";
const encoder = main_window.device.createCommandEncoder(&.{ .label = label });
const encoder = window.device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
// Begin render pass
@ -131,7 +143,7 @@ pub fn tick(app: *App, core: *mach.Core) void {
// Submit our commands to the queue
var command = encoder.finish(&.{ .label = label });
defer command.release();
main_window.queue.submit(&[_]*gpu.CommandBuffer{command});
window.queue.submit(&[_]*gpu.CommandBuffer{command});
// update the window title every second
// if (app.title_timer.read() >= 1.0) {