Skip to content

windows: implement tick, call core.initWindow, various fixes #1311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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", .{});
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is probably the fix you needed for Linux @joshua-holmes to get the Linux.zig implementation to have a place to poll for events

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging this PR now because of this change.

core_mod.run(core.on_tick.?);
core_mod.call(.presentFrame);
}
Expand Down
32 changes: 26 additions & 6 deletions src/core/Windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
Loading