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});
}

View file

@ -106,9 +106,6 @@ on_tick: ?mach.FunctionID = null,
/// Callback system invoked when application is exiting
on_exit: ?mach.FunctionID = null,
/// Main window of the application
main_window: mach.ObjectID,
/// Current state of the application
state: enum {
running,
@ -132,8 +129,6 @@ pub fn init(core: *Core) !void {
// TODO: fix all leaks and use options.allocator
try mach.sysgpu.Impl.init(allocator, .{});
const main_window = try core.windows.new(.{});
var events = EventQueue.init(allocator);
try events.ensureTotalCapacity(8192);
@ -142,7 +137,6 @@ pub fn init(core: *Core) !void {
.windows = core.windows,
.allocator = allocator,
.main_window = main_window,
.events = events,
.input_state = .{},
@ -150,11 +144,6 @@ pub fn init(core: *Core) !void {
.frame = .{ .target = 1 },
};
// Tick the platform so that the platform can grab the newly created window
// and perform initialization
// TODO: consider removing `main_window` and then this wont be necessary
try Platform.tick(core);
try core.frame.start();
try core.input.start();
}
@ -229,6 +218,8 @@ pub fn initWindow(core: *Core, window_id: mach.ObjectID) !void {
core_window.framebuffer_format = core_window.swap_chain_descriptor.format;
core_window.framebuffer_width = core_window.swap_chain_descriptor.width;
core_window.framebuffer_height = core_window.swap_chain_descriptor.height;
core.pushEvent(.{ .window_open = .{ .window_id = window_id } });
}
pub fn tick(core: *Core, core_mod: mach.Mod(Core)) !void {
@ -686,6 +677,9 @@ pub const Event = union(enum) {
yoffset: f32,
},
window_resize: ResizeEvent,
window_open: struct {
window_id: mach.ObjectID,
},
focus_gained: struct {
window_id: mach.ObjectID,
},