Skip to content

Commit

Permalink
core: various fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
emidoots committed Nov 18, 2024
1 parent d49a69d commit 8d9e252
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 49 deletions.
10 changes: 5 additions & 5 deletions src/Core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ pub fn init(core: *Core) !void {
.label = "main swap chain",
.usage = options.swap_chain_usage,
.format = .bgra8_unorm,
.width = @intCast(state.platform.size.width),
.height = @intCast(state.platform.size.height),
.present_mode = switch (state.platform.vsync_mode) {
.width = @intCast(core.platform.size.width),
.height = @intCast(core.platform.size.height),
.present_mode = switch (core.platform.vsync_mode) {
.none => .immediate,
.double => .fifo,
.triple => .mailbox,
Expand Down Expand Up @@ -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.
if (supports_non_blocking) {
while (core.state().state != .exited) {
while (core.state != .exited) {
core_mod.run(core.on_tick.?);
core_mod.call(.presentFrame);
}
Expand Down Expand Up @@ -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");

_ = try core.platform.update();
mach.sysgpu.Impl.deviceTick(state.device);
mach.sysgpu.Impl.deviceTick(core.device);
core.swap_chain.present();

// Update swapchain for the next frame
Expand Down
4 changes: 1 addition & 3 deletions src/core/Darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ size: Size,

// Internals
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 {
const Args = @TypeOf(args_tuple);
Expand Down Expand Up @@ -139,7 +138,7 @@ pub fn init(

darwin.* = .{
.allocator = options.allocator,
.core = @fieldParentPtr("platform", darwin),
.core = core,
.title = options.title,
.display_mode = options.display_mode,
.vsync_mode = .none,
Expand All @@ -151,7 +150,6 @@ pub fn init(
.size = options.size,
.surface_descriptor = surface_descriptor,
.window = window_opt,
.state = core.state(),
};
}

Expand Down
28 changes: 13 additions & 15 deletions src/core/Windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ surrogate: u16 = 0,
dinput: *w.IDirectInput8W,
saved_window_rect: w.RECT,
surface_descriptor_from_hwnd: gpu.Surface.DescriptorFromWindowsHWND,
state: *Core,

// ------------------------------
// Platform interface
Expand All @@ -54,9 +53,8 @@ pub fn init(
core: *Core,
options: InitOptions,
) !void {
self.state = core.state();
self.allocator = options.allocator;
self.core = @fieldParentPtr("platform", self);
self.core = core;
self.size = options.size;
self.saved_window_rect = .{ .top = 0, .left = 0, .right = 0, .bottom = 0 };

Expand Down Expand Up @@ -301,7 +299,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w

switch (msg) {
w.WM_CLOSE => {
self.state.pushEvent(.close);
self.core.pushEvent(.close);
return 0;
},
w.WM_SIZE => {
Expand All @@ -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 (msg == w.WM_SYSKEYDOWN and vkey == w.VK_F4) {
self.state.pushEvent(.close);
self.core.pushEvent(.close);
return 0;
}

Expand All @@ -347,20 +345,20 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
const key = keyFromScancode(scancode);
if (msg == w.WM_KEYDOWN or msg == w.WM_SYSKEYDOWN) {
if (flags & w.KF_REPEAT == 0)
self.state.pushEvent(.{
self.core.pushEvent(.{
.key_press = .{
.key = key,
.mods = mods,
},
})
else
self.state.pushEvent(.{
self.core.pushEvent(.{
.key_repeat = .{
.key = key,
.mods = mods,
},
});
} else self.state.pushEvent(.{
} else self.core.pushEvent(.{
.key_release = .{
.key = key,
.mods = mods,
Expand All @@ -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);
if (iter.nextCodepoint()) |codepoint| {
self.state.pushEvent(.{ .char_input = .{ .codepoint = codepoint.? } });
self.core.pushEvent(.{ .char_input = .{ .codepoint = codepoint.? } });
} else |err| {
err catch {};
}
Expand Down Expand Up @@ -414,14 +412,14 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
w.WM_MBUTTONDOWN,
w.WM_RBUTTONDOWN,
w.WM_XBUTTONDOWN,
=> self.state.pushEvent(.{
=> self.core.pushEvent(.{
.mouse_press = .{
.button = button,
.mods = mods,
.pos = .{ .x = x, .y = y },
},
}),
else => self.state.pushEvent(.{
else => self.core.pushEvent(.{
.mouse_release = .{
.button = button,
.mods = mods,
Expand All @@ -435,7 +433,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
w.WM_MOUSEMOVE => {
const x: f64 = @floatFromInt(@as(i16, @truncate(lParam & 0xFFFF)));
const y: f64 = @floatFromInt(@as(i16, @truncate((lParam >> 16) & 0xFFFF)));
self.state.pushEvent(.{
self.core.pushEvent(.{
.mouse_motion = .{
.pos = .{
.x = x,
Expand All @@ -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 delta_y: f32 = @as(f32, @floatFromInt(@as(i16, @bitCast(wheel_high_word)))) / WHEEL_DELTA;

self.state.pushEvent(.{
self.core.pushEvent(.{
.mouse_scroll = .{
.xoffset = 0,
.yoffset = delta_y,
Expand All @@ -459,11 +457,11 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
return 0;
},
w.WM_SETFOCUS => {
self.state.pushEvent(.{ .focus_gained = {} });
self.core.pushEvent(.{ .focus_gained = {} });
return 0;
},
w.WM_KILLFOCUS => {
self.state.pushEvent(.{ .focus_lost = {} });
self.core.pushEvent(.{ .focus_lost = {} });
return 0;
},
else => return w.DefWindowProcW(wnd, msg, wParam, lParam),
Expand Down
20 changes: 9 additions & 11 deletions src/core/linux/Wayland.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

state: *Core,
core: *Core,
surface_descriptor: *gpu.Surface.DescriptorFromWaylandSurface,
configured: bool = false,
Expand Down Expand Up @@ -77,8 +76,7 @@ pub fn init(
libwaylandclient_global = try LibWaylandClient.load();
linux.backend = .{
.wayland = Wayland{
.core = @fieldParentPtr("platform", linux),
.state = core.state(),
.core = core,
.libxkbcommon = try LibXkbCommon.load(),
.libwaylandclient = libwaylandclient_global,
.interfaces = Interfaces{},
Expand Down Expand Up @@ -462,7 +460,7 @@ const keyboard_listener = struct {
_ = keys;
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 {
Expand All @@ -471,7 +469,7 @@ const keyboard_listener = struct {
_ = surface;
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 {
Expand All @@ -485,7 +483,7 @@ const keyboard_listener = struct {
const key_event = KeyEvent{ .key = toMachKey(scancode), .mods = wl.modifiers };

if (pressed) {
wl.state.pushEvent(.{ .key_press = key_event });
wl.core.pushEvent(.{ .key_press = key_event });

var keysyms: ?[*]c.xkb_keysym_t = undefined;
//Get the keysym from the keycode (scancode + 8)
Expand All @@ -496,11 +494,11 @@ const keyboard_listener = struct {
//Try to convert that keysym to a unicode codepoint
const codepoint = wl.libxkbcommon.xkb_keysym_to_utf32(keysym);
if (codepoint != 0) {
wl.state.pushEvent(Core.Event{ .char_input = .{ .codepoint = @truncate(codepoint) } });
wl.core.pushEvent(Core.Event{ .char_input = .{ .codepoint = @truncate(codepoint) } });
}
}
} else {
wl.state.pushEvent(.{ .key_release = key_event });
wl.core.pushEvent(.{ .key_release = key_event });
}
}

Expand Down Expand Up @@ -629,7 +627,7 @@ const pointer_listener = struct {
const x = c.wl_fixed_to_double(fixed_x);
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 {
Expand All @@ -644,13 +642,13 @@ const pointer_listener = struct {
const y = wl.state.input_state.mouse_position.y;

if (pressed) {
wl.state.pushEvent(Core.Event{ .mouse_press = .{
wl.core.pushEvent(Core.Event{ .mouse_press = .{
.button = mouse_button,
.mods = wl.modifiers,
.pos = .{ .x = x, .y = y },
} });
} else {
wl.state.pushEvent(Core.Event{ .mouse_release = .{
wl.core.pushEvent(Core.Event{ .mouse_release = .{
.button = mouse_button,
.mods = wl.modifiers,
.pos = .{ .x = x, .y = y },
Expand Down
28 changes: 13 additions & 15 deletions src/core/linux/X11.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub const X11 = @This();

allocator: std.mem.Allocator,
core: *Core,
state: *Core,

libx11: LibX11,
libxrr: ?LibXRR,
Expand Down Expand Up @@ -141,8 +140,7 @@ pub fn init(
.window = @intCast(window),
};
linux.backend = .{ .x11 = X11{
.core = @fieldParentPtr("platform", linux),
.state = core.state(),
.core = core,
.allocator = options.allocator,
.display = display,
.libx11 = libx11,
Expand Down Expand Up @@ -501,15 +499,15 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {

switch (event.type) {
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));
if (codepoint != 0) {
x11.state.pushEvent(.{ .char_input = .{ .codepoint = @truncate(codepoint) } });
x11.core.pushEvent(.{ .char_input = .{ .codepoint = @truncate(codepoint) } });
}
},
c.KeyRelease => {
x11.state.pushEvent(.{ .key_release = key_event });
x11.core.pushEvent(.{ .key_release = key_event });
},
else => unreachable,
}
Expand All @@ -524,7 +522,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
7 => .{ -1.0, 0.0 },
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;
};
const cursor_pos = x11.getCursorPos();
Expand All @@ -534,7 +532,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state),
};

x11.state.pushEvent(.{ .mouse_press = mouse_button });
x11.core.pushEvent(.{ .mouse_press = mouse_button });
},
c.ButtonRelease => {
const button = toMachButton(event.xbutton.button) orelse return;
Expand All @@ -545,7 +543,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
.mods = toMachMods(event.xbutton.state),
};

x11.state.pushEvent(.{ .mouse_release = mouse_button });
x11.core.pushEvent(.{ .mouse_release = mouse_button });
},
c.ClientMessage => {
if (event.xclient.message_type == c.None) return;
Expand All @@ -555,7 +553,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
if (protocol == c.None) return;

if (protocol == x11.wm_delete_window) {
x11.state.pushEvent(.close);
x11.core.pushEvent(.close);
} else if (protocol == x11.net_wm_ping) {
// The window manager is pinging the application to ensure
// it's still responding to events
Expand All @@ -574,12 +572,12 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
c.EnterNotify => {
const x: f32 = @floatFromInt(event.xcrossing.x);
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 => {
const x: f32 = @floatFromInt(event.xmotion.x);
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 => {
if (event.xconfigure.width != linux.size.width or
Expand All @@ -588,7 +586,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
linux.size.width = @intCast(event.xconfigure.width);
linux.size.height = @intCast(event.xconfigure.height);
x11.core.swap_chain_update.set();
x11.state.pushEvent(.{
x11.core.pushEvent(.{
.framebuffer_resize = .{
.width = linux.size.width,
.height = linux.size.height,
Expand All @@ -605,7 +603,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
return;
}

x11.state.pushEvent(.focus_gained);
x11.core.pushEvent(.focus_gained);
},
c.FocusOut => {
if (event.xfocus.mode == c.NotifyGrab or
Expand All @@ -616,7 +614,7 @@ fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void {
return;
}

x11.state.pushEvent(.focus_lost);
x11.core.pushEvent(.focus_lost);
},
else => {},
}
Expand Down

0 comments on commit 8d9e252

Please sign in to comment.