From e15cbe63dea05949adc5e6a1d8818bb32e2a3ab3 Mon Sep 17 00:00:00 2001 From: foxnne Date: Sat, 30 Nov 2024 18:46:54 -0600 Subject: [PATCH] windows: implement `tick`, call `core.initWindow`, various fixes --- src/Core.zig | 4 ++-- src/core/Windows.zig | 32 ++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Core.zig b/src/Core.zig index 05f5430a3f..d143c0fe2d 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -161,6 +161,7 @@ pub fn init(core: *Core) !void { pub fn initWindow(core: *Core, window_id: mach.ObjectID) !void { var core_window = core.windows.getValue(window_id); + defer core.windows.setValueRaw(window_id, core_window); core_window.instance = gpu.createInstance(null) orelse { log.err("failed to create GPU instance", .{}); @@ -228,8 +229,6 @@ 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.windows.setValueRaw(window_id, core_window); } pub fn tick(core: *Core, core_mod: mach.Mod(Core)) !void { @@ -266,6 +265,7 @@ pub fn main(core: *Core, core_mod: mach.Mod(Core)) !void { // The user wants mach.Core to take control of the main loop. if (supports_non_blocking) { while (core.state != .exited) { + try Platform.tick(core); core_mod.run(core.on_tick.?); core_mod.call(.presentFrame); } diff --git a/src/core/Windows.zig b/src/core/Windows.zig index d6e2aa452c..5e82d24d2a 100644 --- a/src/core/Windows.zig +++ b/src/core/Windows.zig @@ -152,15 +152,26 @@ pub const Context = struct { // } pub fn tick(core: *Core) !void { - _ = core; // autofix + var windows = core.windows.slice(); + while (windows.next()) |window_id| { + const native_opt: ?Native = core.windows.get(window_id, .native); + if (native_opt) |native| { + _ = native; // autofix + + // Handle resizing the window when the user changes width or height + if (core.windows.updated(window_id, .width) or core.windows.updated(window_id, .height)) {} + } else { + try initWindow(core, window_id); + } + } } fn initWindow( core: *Core, window_id: mach.ObjectID, ) !void { - const core_window = core.windows.getValue(window_id); + var core_window = core.windows.getValue(window_id); const hInstance = w.GetModuleHandleW(null); const class_name = w.L("mach"); @@ -227,11 +238,19 @@ fn initWindow( _ = w.SetWindowLongPtrW(native_window, w.GWLP_USERDATA, @bitCast(@intFromPtr(&context))); + _ = w.ShowWindow(native_window, w.SW_SHOW); + + restoreWindowPosition(core, window_id); + const size = getClientRect(core, window_id); core_window.width = size.width; core_window.height = size.height; _ = w.GetWindowRect(native.window, &native.saved_window_rect); + + core_window.native = native; + core.windows.setValueRaw(window_id, core_window); + try core.initWindow(window_id); } // pub fn update(self: *Win32) !void { @@ -406,12 +425,12 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w const core = context.core; const window_id = context.window_id; - const window = core.windows.getValue(window_id); + var window = core.windows.getValue(window_id); defer core.windows.setValueRaw(window_id, window); switch (msg) { w.WM_CLOSE => { - core.pushEvent(.close); + core.pushEvent(.{ .close = .{ .window_id = window_id } }); return 0; }, w.WM_SIZE => { @@ -437,7 +456,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w if (vkey == w.VK_PROCESSKEY) return 0; if (msg == w.WM_SYSKEYDOWN and vkey == w.VK_F4) { - core.pushEvent(.close); + core.pushEvent(.{ .close = .{ .window_id = window_id } }); return 0; } @@ -489,7 +508,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w return 0; }, w.WM_CHAR => { - if (window.native) |native| { + if (window.native) |*native| { const char: u16 = @truncate(wParam); var chars: []const u16 = undefined; if (native.surrogate != 0) { @@ -596,6 +615,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w }, else => return w.DefWindowProcW(wnd, msg, wParam, lParam), } + return w.DefWindowProcW(wnd, msg, wParam, lParam); } fn keyFromScancode(scancode: u9) Key {