core: various fixes

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-11-17 21:06:51 -07:00 committed by Emi Gutekanst
parent 714f200bc1
commit fc13b371bf
5 changed files with 41 additions and 49 deletions

View file

@ -208,9 +208,9 @@ pub fn init(core: *Core) !void {
.label = "main swap chain", .label = "main swap chain",
.usage = options.swap_chain_usage, .usage = options.swap_chain_usage,
.format = .bgra8_unorm, .format = .bgra8_unorm,
.width = @intCast(state.platform.size.width), .width = @intCast(core.platform.size.width),
.height = @intCast(state.platform.size.height), .height = @intCast(core.platform.size.height),
.present_mode = switch (state.platform.vsync_mode) { .present_mode = switch (core.platform.vsync_mode) {
.none => .immediate, .none => .immediate,
.double => .fifo, .double => .fifo,
.triple => .mailbox, .triple => .mailbox,
@ -258,7 +258,7 @@ pub fn main(core: *Core, core_mod: mach.Mod(Core)) !void {
// The user wants mach.Core to take control of the main loop. // The user wants mach.Core to take control of the main loop.
if (supports_non_blocking) { if (supports_non_blocking) {
while (core.state().state != .exited) { while (core.state != .exited) {
core_mod.run(core.on_tick.?); core_mod.run(core.on_tick.?);
core_mod.call(.presentFrame); core_mod.call(.presentFrame);
} }
@ -580,7 +580,7 @@ pub fn presentFrame(core: *Core, core_mod: mach.Mod(Core)) !void {
// if (num_windows > 1) @panic("mach: Core currently only supports a single window"); // if (num_windows > 1) @panic("mach: Core currently only supports a single window");
_ = try core.platform.update(); _ = try core.platform.update();
mach.sysgpu.Impl.deviceTick(state.device); mach.sysgpu.Impl.deviceTick(core.device);
core.swap_chain.present(); core.swap_chain.present();
// Update swapchain for the next frame // Update swapchain for the next frame

View file

@ -41,7 +41,6 @@ size: Size,
// Internals // Internals
window: ?*objc.app_kit.Window, window: ?*objc.app_kit.Window,
state: *Core,
pub fn run(comptime on_each_update_fn: anytype, args_tuple: std.meta.ArgsTuple(@TypeOf(on_each_update_fn))) noreturn { pub fn run(comptime on_each_update_fn: anytype, args_tuple: std.meta.ArgsTuple(@TypeOf(on_each_update_fn))) noreturn {
const Args = @TypeOf(args_tuple); const Args = @TypeOf(args_tuple);
@ -139,7 +138,7 @@ pub fn init(
darwin.* = .{ darwin.* = .{
.allocator = options.allocator, .allocator = options.allocator,
.core = @fieldParentPtr("platform", darwin), .core = core,
.title = options.title, .title = options.title,
.display_mode = options.display_mode, .display_mode = options.display_mode,
.vsync_mode = .none, .vsync_mode = .none,
@ -151,7 +150,6 @@ pub fn init(
.size = options.size, .size = options.size,
.surface_descriptor = surface_descriptor, .surface_descriptor = surface_descriptor,
.window = window_opt, .window = window_opt,
.state = core.state(),
}; };
} }

View file

@ -44,7 +44,6 @@ surrogate: u16 = 0,
dinput: *w.IDirectInput8W, dinput: *w.IDirectInput8W,
saved_window_rect: w.RECT, saved_window_rect: w.RECT,
surface_descriptor_from_hwnd: gpu.Surface.DescriptorFromWindowsHWND, surface_descriptor_from_hwnd: gpu.Surface.DescriptorFromWindowsHWND,
state: *Core,
// ------------------------------ // ------------------------------
// Platform interface // Platform interface
@ -54,9 +53,8 @@ pub fn init(
core: *Core, core: *Core,
options: InitOptions, options: InitOptions,
) !void { ) !void {
self.state = core.state();
self.allocator = options.allocator; self.allocator = options.allocator;
self.core = @fieldParentPtr("platform", self); self.core = core;
self.size = options.size; self.size = options.size;
self.saved_window_rect = .{ .top = 0, .left = 0, .right = 0, .bottom = 0 }; self.saved_window_rect = .{ .top = 0, .left = 0, .right = 0, .bottom = 0 };
@ -301,7 +299,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
switch (msg) { switch (msg) {
w.WM_CLOSE => { w.WM_CLOSE => {
self.state.pushEvent(.close); self.core.pushEvent(.close);
return 0; return 0;
}, },
w.WM_SIZE => { w.WM_SIZE => {
@ -323,7 +321,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
if (vkey == w.VK_PROCESSKEY) return 0; if (vkey == w.VK_PROCESSKEY) return 0;
if (msg == w.WM_SYSKEYDOWN and vkey == w.VK_F4) { if (msg == w.WM_SYSKEYDOWN and vkey == w.VK_F4) {
self.state.pushEvent(.close); self.core.pushEvent(.close);
return 0; return 0;
} }
@ -347,20 +345,20 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
const key = keyFromScancode(scancode); const key = keyFromScancode(scancode);
if (msg == w.WM_KEYDOWN or msg == w.WM_SYSKEYDOWN) { if (msg == w.WM_KEYDOWN or msg == w.WM_SYSKEYDOWN) {
if (flags & w.KF_REPEAT == 0) if (flags & w.KF_REPEAT == 0)
self.state.pushEvent(.{ self.core.pushEvent(.{
.key_press = .{ .key_press = .{
.key = key, .key = key,
.mods = mods, .mods = mods,
}, },
}) })
else else
self.state.pushEvent(.{ self.core.pushEvent(.{
.key_repeat = .{ .key_repeat = .{
.key = key, .key = key,
.mods = mods, .mods = mods,
}, },
}); });
} else self.state.pushEvent(.{ } else self.core.pushEvent(.{
.key_release = .{ .key_release = .{
.key = key, .key = key,
.mods = mods, .mods = mods,
@ -383,7 +381,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
} }
var iter = std.unicode.Utf16LeIterator.init(chars); var iter = std.unicode.Utf16LeIterator.init(chars);
if (iter.nextCodepoint()) |codepoint| { if (iter.nextCodepoint()) |codepoint| {
self.state.pushEvent(.{ .char_input = .{ .codepoint = codepoint.? } }); self.core.pushEvent(.{ .char_input = .{ .codepoint = codepoint.? } });
} else |err| { } else |err| {
err catch {}; err catch {};
} }
@ -414,14 +412,14 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
w.WM_MBUTTONDOWN, w.WM_MBUTTONDOWN,
w.WM_RBUTTONDOWN, w.WM_RBUTTONDOWN,
w.WM_XBUTTONDOWN, w.WM_XBUTTONDOWN,
=> self.state.pushEvent(.{ => self.core.pushEvent(.{
.mouse_press = .{ .mouse_press = .{
.button = button, .button = button,
.mods = mods, .mods = mods,
.pos = .{ .x = x, .y = y }, .pos = .{ .x = x, .y = y },
}, },
}), }),
else => self.state.pushEvent(.{ else => self.core.pushEvent(.{
.mouse_release = .{ .mouse_release = .{
.button = button, .button = button,
.mods = mods, .mods = mods,
@ -435,7 +433,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
w.WM_MOUSEMOVE => { w.WM_MOUSEMOVE => {
const x: f64 = @floatFromInt(@as(i16, @truncate(lParam & 0xFFFF))); const x: f64 = @floatFromInt(@as(i16, @truncate(lParam & 0xFFFF)));
const y: f64 = @floatFromInt(@as(i16, @truncate((lParam >> 16) & 0xFFFF))); const y: f64 = @floatFromInt(@as(i16, @truncate((lParam >> 16) & 0xFFFF)));
self.state.pushEvent(.{ self.core.pushEvent(.{
.mouse_motion = .{ .mouse_motion = .{
.pos = .{ .pos = .{
.x = x, .x = x,
@ -450,7 +448,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
const wheel_high_word: u16 = @truncate((wParam >> 16) & 0xffff); const wheel_high_word: u16 = @truncate((wParam >> 16) & 0xffff);
const delta_y: f32 = @as(f32, @floatFromInt(@as(i16, @bitCast(wheel_high_word)))) / WHEEL_DELTA; const delta_y: f32 = @as(f32, @floatFromInt(@as(i16, @bitCast(wheel_high_word)))) / WHEEL_DELTA;
self.state.pushEvent(.{ self.core.pushEvent(.{
.mouse_scroll = .{ .mouse_scroll = .{
.xoffset = 0, .xoffset = 0,
.yoffset = delta_y, .yoffset = delta_y,
@ -459,11 +457,11 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
return 0; return 0;
}, },
w.WM_SETFOCUS => { w.WM_SETFOCUS => {
self.state.pushEvent(.{ .focus_gained = {} }); self.core.pushEvent(.{ .focus_gained = {} });
return 0; return 0;
}, },
w.WM_KILLFOCUS => { w.WM_KILLFOCUS => {
self.state.pushEvent(.{ .focus_lost = {} }); self.core.pushEvent(.{ .focus_lost = {} });
return 0; return 0;
}, },
else => return w.DefWindowProcW(wnd, msg, wParam, lParam), else => return w.DefWindowProcW(wnd, msg, wParam, lParam),

View file

@ -46,7 +46,6 @@ export fn wl_proxy_destroy(proxy: ?*c.struct_wl_proxy) void {
return @call(.always_tail, libwaylandclient_global.wl_proxy_destroy, .{proxy}); return @call(.always_tail, libwaylandclient_global.wl_proxy_destroy, .{proxy});
} }
state: *Core,
core: *Core, core: *Core,
surface_descriptor: *gpu.Surface.DescriptorFromWaylandSurface, surface_descriptor: *gpu.Surface.DescriptorFromWaylandSurface,
configured: bool = false, configured: bool = false,
@ -77,8 +76,7 @@ pub fn init(
libwaylandclient_global = try LibWaylandClient.load(); libwaylandclient_global = try LibWaylandClient.load();
linux.backend = .{ linux.backend = .{
.wayland = Wayland{ .wayland = Wayland{
.core = @fieldParentPtr("platform", linux), .core = core,
.state = core.state(),
.libxkbcommon = try LibXkbCommon.load(), .libxkbcommon = try LibXkbCommon.load(),
.libwaylandclient = libwaylandclient_global, .libwaylandclient = libwaylandclient_global,
.interfaces = Interfaces{}, .interfaces = Interfaces{},
@ -462,7 +460,7 @@ const keyboard_listener = struct {
_ = keys; _ = keys;
const wl = &linux.backend.wayland; const wl = &linux.backend.wayland;
wl.state.pushEvent(.focus_gained); wl.core.pushEvent(.focus_gained);
} }
fn keyboardHandleLeave(linux: *Linux, keyboard: ?*c.struct_wl_keyboard, serial: u32, surface: ?*c.struct_wl_surface) callconv(.C) void { fn keyboardHandleLeave(linux: *Linux, keyboard: ?*c.struct_wl_keyboard, serial: u32, surface: ?*c.struct_wl_surface) callconv(.C) void {
@ -471,7 +469,7 @@ const keyboard_listener = struct {
_ = surface; _ = surface;
const wl = &linux.backend.wayland; const wl = &linux.backend.wayland;
wl.state.pushEvent(.focus_lost); wl.core.pushEvent(.focus_lost);
} }
fn keyboardHandleKey(linux: *Linux, keyboard: ?*c.struct_wl_keyboard, serial: u32, time: u32, scancode: u32, state: u32) callconv(.C) void { fn keyboardHandleKey(linux: *Linux, keyboard: ?*c.struct_wl_keyboard, serial: u32, time: u32, scancode: u32, state: u32) callconv(.C) void {
@ -485,7 +483,7 @@ const keyboard_listener = struct {
const key_event = KeyEvent{ .key = toMachKey(scancode), .mods = wl.modifiers }; const key_event = KeyEvent{ .key = toMachKey(scancode), .mods = wl.modifiers };
if (pressed) { if (pressed) {
wl.state.pushEvent(.{ .key_press = key_event }); wl.core.pushEvent(.{ .key_press = key_event });
var keysyms: ?[*]c.xkb_keysym_t = undefined; var keysyms: ?[*]c.xkb_keysym_t = undefined;
//Get the keysym from the keycode (scancode + 8) //Get the keysym from the keycode (scancode + 8)
@ -496,11 +494,11 @@ const keyboard_listener = struct {
//Try to convert that keysym to a unicode codepoint //Try to convert that keysym to a unicode codepoint
const codepoint = wl.libxkbcommon.xkb_keysym_to_utf32(keysym); const codepoint = wl.libxkbcommon.xkb_keysym_to_utf32(keysym);
if (codepoint != 0) { if (codepoint != 0) {
wl.state.pushEvent(Core.Event{ .char_input = .{ .codepoint = @truncate(codepoint) } }); wl.core.pushEvent(Core.Event{ .char_input = .{ .codepoint = @truncate(codepoint) } });
} }
} }
} else { } else {
wl.state.pushEvent(.{ .key_release = key_event }); wl.core.pushEvent(.{ .key_release = key_event });
} }
} }
@ -629,7 +627,7 @@ const pointer_listener = struct {
const x = c.wl_fixed_to_double(fixed_x); const x = c.wl_fixed_to_double(fixed_x);
const y = c.wl_fixed_to_double(fixed_y); const y = c.wl_fixed_to_double(fixed_y);
wl.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } }); wl.core.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
} }
fn handlePointerButton(linux: *Linux, pointer: ?*c.struct_wl_pointer, serial: u32, time: u32, button: u32, state: u32) callconv(.C) void { fn handlePointerButton(linux: *Linux, pointer: ?*c.struct_wl_pointer, serial: u32, time: u32, button: u32, state: u32) callconv(.C) void {
@ -644,13 +642,13 @@ const pointer_listener = struct {
const y = wl.state.input_state.mouse_position.y; const y = wl.state.input_state.mouse_position.y;
if (pressed) { if (pressed) {
wl.state.pushEvent(Core.Event{ .mouse_press = .{ wl.core.pushEvent(Core.Event{ .mouse_press = .{
.button = mouse_button, .button = mouse_button,
.mods = wl.modifiers, .mods = wl.modifiers,
.pos = .{ .x = x, .y = y }, .pos = .{ .x = x, .y = y },
} }); } });
} else { } else {
wl.state.pushEvent(Core.Event{ .mouse_release = .{ wl.core.pushEvent(Core.Event{ .mouse_release = .{
.button = mouse_button, .button = mouse_button,
.mods = wl.modifiers, .mods = wl.modifiers,
.pos = .{ .x = x, .y = y }, .pos = .{ .x = x, .y = y },

View file

@ -34,7 +34,6 @@ pub const X11 = @This();
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
core: *Core, core: *Core,
state: *Core,
libx11: LibX11, libx11: LibX11,
libxrr: ?LibXRR, libxrr: ?LibXRR,
@ -141,8 +140,7 @@ pub fn init(
.window = @intCast(window), .window = @intCast(window),
}; };
linux.backend = .{ .x11 = X11{ linux.backend = .{ .x11 = X11{
.core = @fieldParentPtr("platform", linux), .core = core,
.state = core.state(),
.allocator = options.allocator, .allocator = options.allocator,
.display = display, .display = display,
.libx11 = libx11, .libx11 = libx11,
@ -501,15 +499,15 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
switch (event.type) { switch (event.type) {
c.KeyPress => { c.KeyPress => {
x11.state.pushEvent(.{ .key_press = key_event }); x11.core.pushEvent(.{ .key_press = key_event });
const codepoint = x11.libxkbcommon.xkb_keysym_to_utf32(@truncate(keysym)); const codepoint = x11.libxkbcommon.xkb_keysym_to_utf32(@truncate(keysym));
if (codepoint != 0) { if (codepoint != 0) {
x11.state.pushEvent(.{ .char_input = .{ .codepoint = @truncate(codepoint) } }); x11.core.pushEvent(.{ .char_input = .{ .codepoint = @truncate(codepoint) } });
} }
}, },
c.KeyRelease => { c.KeyRelease => {
x11.state.pushEvent(.{ .key_release = key_event }); x11.core.pushEvent(.{ .key_release = key_event });
}, },
else => unreachable, else => unreachable,
} }
@ -524,7 +522,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
7 => .{ -1.0, 0.0 }, 7 => .{ -1.0, 0.0 },
else => unreachable, else => unreachable,
}; };
x11.state.pushEvent(.{ .mouse_scroll = .{ .xoffset = scroll[0], .yoffset = scroll[1] } }); x11.core.pushEvent(.{ .mouse_scroll = .{ .xoffset = scroll[0], .yoffset = scroll[1] } });
return; return;
}; };
const cursor_pos = x11.getCursorPos(); const cursor_pos = x11.getCursorPos();
@ -534,7 +532,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state), .mods = toMachMods(event.xbutton.state),
}; };
x11.state.pushEvent(.{ .mouse_press = mouse_button }); x11.core.pushEvent(.{ .mouse_press = mouse_button });
}, },
c.ButtonRelease => { c.ButtonRelease => {
const button = toMachButton(event.xbutton.button) orelse return; const button = toMachButton(event.xbutton.button) orelse return;
@ -545,7 +543,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state), .mods = toMachMods(event.xbutton.state),
}; };
x11.state.pushEvent(.{ .mouse_release = mouse_button }); x11.core.pushEvent(.{ .mouse_release = mouse_button });
}, },
c.ClientMessage => { c.ClientMessage => {
if (event.xclient.message_type == c.None) return; if (event.xclient.message_type == c.None) return;
@ -555,7 +553,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
if (protocol == c.None) return; if (protocol == c.None) return;
if (protocol == x11.wm_delete_window) { if (protocol == x11.wm_delete_window) {
x11.state.pushEvent(.close); x11.core.pushEvent(.close);
} else if (protocol == x11.net_wm_ping) { } else if (protocol == x11.net_wm_ping) {
// The window manager is pinging the application to ensure // The window manager is pinging the application to ensure
// it's still responding to events // it's still responding to events
@ -574,12 +572,12 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
c.EnterNotify => { c.EnterNotify => {
const x: f32 = @floatFromInt(event.xcrossing.x); const x: f32 = @floatFromInt(event.xcrossing.x);
const y: f32 = @floatFromInt(event.xcrossing.y); const y: f32 = @floatFromInt(event.xcrossing.y);
x11.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } }); x11.core.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
}, },
c.MotionNotify => { c.MotionNotify => {
const x: f32 = @floatFromInt(event.xmotion.x); const x: f32 = @floatFromInt(event.xmotion.x);
const y: f32 = @floatFromInt(event.xmotion.y); const y: f32 = @floatFromInt(event.xmotion.y);
x11.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } }); x11.core.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
}, },
c.ConfigureNotify => { c.ConfigureNotify => {
if (event.xconfigure.width != linux.size.width or if (event.xconfigure.width != linux.size.width or
@ -588,7 +586,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
linux.size.width = @intCast(event.xconfigure.width); linux.size.width = @intCast(event.xconfigure.width);
linux.size.height = @intCast(event.xconfigure.height); linux.size.height = @intCast(event.xconfigure.height);
x11.core.swap_chain_update.set(); x11.core.swap_chain_update.set();
x11.state.pushEvent(.{ x11.core.pushEvent(.{
.framebuffer_resize = .{ .framebuffer_resize = .{
.width = linux.size.width, .width = linux.size.width,
.height = linux.size.height, .height = linux.size.height,
@ -605,7 +603,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
return; return;
} }
x11.state.pushEvent(.focus_gained); x11.core.pushEvent(.focus_gained);
}, },
c.FocusOut => { c.FocusOut => {
if (event.xfocus.mode == c.NotifyGrab or if (event.xfocus.mode == c.NotifyGrab or
@ -616,7 +614,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
return; return;
} }
x11.state.pushEvent(.focus_lost); x11.core.pushEvent(.focus_lost);
}, },
else => {}, else => {},
} }