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

@ -7,6 +7,7 @@ pub const mach_module = .app;
pub const mach_systems = .{ .main, .init, .deinit, .tick };
window: mach.ObjectID,
title_timer: mach.time.Timer,
pipeline: *gpu.RenderPipeline,
@ -28,10 +29,25 @@ 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-custom-entrypoint" });
// 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,
};
// TODO(object): window-title
// try updateWindowTitle(core);
}
fn setupPipeline(core: *mach.Core, app: *App, window_id: mach.ObjectID) !void {
_ = window_id; // autofix
const window = core.windows.getValue(app.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
@ -39,7 +55,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,
};
@ -60,36 +76,31 @@ 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,
};
// TODO(object): window-title
// try updateWindowTitle(core);
app.pipeline = window.device.createRenderPipeline(&pipeline_descriptor);
}
pub fn tick(core: *mach.Core, app: *App) !void {
while (core.nextEvent()) |event| {
switch (event) {
.window_open => |ev| {
try setupPipeline(core, app, ev.window_id);
},
.close => core.exit(),
else => {},
}
}
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
@ -116,7 +127,7 @@ pub fn tick(core: *mach.Core, app: *App) !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) {

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) {

View file

@ -28,6 +28,7 @@ pub const deinit = mach.schedule(.{
.{ mach.Audio, .deinit },
});
window: mach.ObjectID,
/// Tag object we set as a child of mach.Audio objects to indicate they are background music.
// TODO(object): consider adding a better object 'tagging' system?
bgm: mach.Objects(.{}, struct {}),
@ -46,6 +47,10 @@ pub fn init(
core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit;
const window = try core.windows.new(.{
.title = "play-opus",
});
// Configure the audio module to send our app's .audio_state_change event when an entity's sound
// finishes playing.
audio.on_state_change = app_mod.id.audioStateChange;
@ -61,7 +66,7 @@ pub fn init(
const sfx = try mach.Audio.Opus.decodeStream(allocator, sfx_sound_stream);
// Initialize module state
app.* = .{ .sfx = sfx, .bgm = app.bgm };
app.* = .{ .sfx = sfx, .bgm = app.bgm, .window = window };
const bgm_buffer = blk: {
audio.buffers.lock();
@ -138,16 +143,16 @@ pub fn tick(
}
}
var main_window = core.windows.getValue(core.main_window);
var 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
@ -172,5 +177,5 @@ pub fn tick(
// 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});
}