Skip to content

Commit e15cbe6

Browse files
foxnneemidoots
authored andcommitted
windows: implement tick, call core.initWindow, various fixes
1 parent 0ec182c commit e15cbe6

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/Core.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub fn init(core: *Core) !void {
161161

162162
pub fn initWindow(core: *Core, window_id: mach.ObjectID) !void {
163163
var core_window = core.windows.getValue(window_id);
164+
defer core.windows.setValueRaw(window_id, core_window);
164165

165166
core_window.instance = gpu.createInstance(null) orelse {
166167
log.err("failed to create GPU instance", .{});
@@ -228,8 +229,6 @@ pub fn initWindow(core: *Core, window_id: mach.ObjectID) !void {
228229
core_window.framebuffer_format = core_window.swap_chain_descriptor.format;
229230
core_window.framebuffer_width = core_window.swap_chain_descriptor.width;
230231
core_window.framebuffer_height = core_window.swap_chain_descriptor.height;
231-
232-
core.windows.setValueRaw(window_id, core_window);
233232
}
234233

235234
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 {
266265
// The user wants mach.Core to take control of the main loop.
267266
if (supports_non_blocking) {
268267
while (core.state != .exited) {
268+
try Platform.tick(core);
269269
core_mod.run(core.on_tick.?);
270270
core_mod.call(.presentFrame);
271271
}

src/core/Windows.zig

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,26 @@ pub const Context = struct {
152152
// }
153153

154154
pub fn tick(core: *Core) !void {
155-
_ = core; // autofix
155+
var windows = core.windows.slice();
156+
while (windows.next()) |window_id| {
157+
const native_opt: ?Native = core.windows.get(window_id, .native);
156158

159+
if (native_opt) |native| {
160+
_ = native; // autofix
161+
162+
// Handle resizing the window when the user changes width or height
163+
if (core.windows.updated(window_id, .width) or core.windows.updated(window_id, .height)) {}
164+
} else {
165+
try initWindow(core, window_id);
166+
}
167+
}
157168
}
158169

159170
fn initWindow(
160171
core: *Core,
161172
window_id: mach.ObjectID,
162173
) !void {
163-
const core_window = core.windows.getValue(window_id);
174+
var core_window = core.windows.getValue(window_id);
164175

165176
const hInstance = w.GetModuleHandleW(null);
166177
const class_name = w.L("mach");
@@ -227,11 +238,19 @@ fn initWindow(
227238

228239
_ = w.SetWindowLongPtrW(native_window, w.GWLP_USERDATA, @bitCast(@intFromPtr(&context)));
229240

241+
_ = w.ShowWindow(native_window, w.SW_SHOW);
242+
243+
restoreWindowPosition(core, window_id);
244+
230245
const size = getClientRect(core, window_id);
231246
core_window.width = size.width;
232247
core_window.height = size.height;
233248

234249
_ = w.GetWindowRect(native.window, &native.saved_window_rect);
250+
251+
core_window.native = native;
252+
core.windows.setValueRaw(window_id, core_window);
253+
try core.initWindow(window_id);
235254
}
236255

237256
// 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
406425
const core = context.core;
407426
const window_id = context.window_id;
408427

409-
const window = core.windows.getValue(window_id);
428+
var window = core.windows.getValue(window_id);
410429
defer core.windows.setValueRaw(window_id, window);
411430

412431
switch (msg) {
413432
w.WM_CLOSE => {
414-
core.pushEvent(.close);
433+
core.pushEvent(.{ .close = .{ .window_id = window_id } });
415434
return 0;
416435
},
417436
w.WM_SIZE => {
@@ -437,7 +456,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
437456
if (vkey == w.VK_PROCESSKEY) return 0;
438457

439458
if (msg == w.WM_SYSKEYDOWN and vkey == w.VK_F4) {
440-
core.pushEvent(.close);
459+
core.pushEvent(.{ .close = .{ .window_id = window_id } });
441460
return 0;
442461
}
443462

@@ -489,7 +508,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
489508
return 0;
490509
},
491510
w.WM_CHAR => {
492-
if (window.native) |native| {
511+
if (window.native) |*native| {
493512
const char: u16 = @truncate(wParam);
494513
var chars: []const u16 = undefined;
495514
if (native.surrogate != 0) {
@@ -596,6 +615,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
596615
},
597616
else => return w.DefWindowProcW(wnd, msg, wParam, lParam),
598617
}
618+
return w.DefWindowProcW(wnd, msg, wParam, lParam);
599619
}
600620

601621
fn keyFromScancode(scancode: u9) Key {

0 commit comments

Comments
 (0)