From 3445df5315a9e762e8073b6780e5491e97baefa8 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 10 Jan 2022 10:21:37 -0700 Subject: [PATCH] Added setMouseMoveCallback --- lib/minifb/src/minifb.zig | 5 +++++ src/main.zig | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/minifb/src/minifb.zig b/lib/minifb/src/minifb.zig index 97f743f..3dbd8a7 100644 --- a/lib/minifb/src/minifb.zig +++ b/lib/minifb/src/minifb.zig @@ -143,6 +143,11 @@ pub fn Window(comptime TUserData: type) type { c.mfb_set_mouse_button_callback(self.cwin, @ptrCast(c.mfb_mouse_button_func, callback)); } + pub const MouseMoveFunc = fn (win: Window(TUserData), x: i32, y: i32) callconv(.C) void; + pub fn setMouseMoveCallback(self: Window(TUserData), callback: MouseMoveFunc) void { + c.mfb_set_mouse_move_callback(self.cwin, @ptrCast(c.mfb_mouse_move_func, callback)); + } + pub fn setViewport(self: Window(TUserData), offset_x: u32, offset_y: u32, width: u32, height: u32) bool { return c.mfb_set_viewport(self.cwin, offset_x, offset_y, width, height); } diff --git a/src/main.zig b/src/main.zig index a5a015f..648c01e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -6,6 +6,7 @@ const max = std.math.max; const Width = 800; const Height = 600; const AspectRatio = @intToFloat(comptime_float, Width) / @intToFloat(comptime_float, Height); +const CursorWidth = 25; fn grey(value: u8) mfb.Rgb { return mfb.Rgb{ .r = value, .g = value, .b = value }; @@ -21,6 +22,7 @@ const State = struct { timer: *mfb.Timer, active: bool = true, alloc: std.mem.Allocator, + cursor: Coords = undefined, pub fn init(alloc: std.mem.Allocator, period: f64) !State { var timer = try alloc.create(mfb.Timer); @@ -48,6 +50,13 @@ const State = struct { buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = Width, .y = Height }, grey(@floatToInt(u8, bgValue))); buf.drawRectangle(.{ .x = Width / 3, .y = Height / 3 }, .{ .x = 2 * Width / 3, .y = 2 * Height / 3 }, rectColor); buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = 10, .y = 10 }, mfb.Rgb{ .r = 0, .g = 0, .b = 255 }); + + // Draw cursor + buf.drawRectangle( + .{ .x = self.cursor.x - CursorWidth / 2, .y = self.cursor.y - CursorWidth / 2 }, + .{ .x = self.cursor.x + CursorWidth / 2, .y = self.cursor.y + CursorWidth / 2 }, + mfb.Rgb{ .r = 61, .g = 97, .b = 170 }, + ); } }; @@ -137,6 +146,12 @@ fn handleMouseButton(_: mfb.Window(State), mouse_button: mfb.MouseButton, key_mo std.log.info("{any} mouse{d} {s}", .{ key_mod, @enumToInt(mouse_button), up_down }); } +fn handleMouseMove(win: mfb.Window(State), x: i32, y: i32) callconv(.C) void { + var state = win.getUserData().?; + std.log.debug("cursor={any}", .{state.*.cursor}); + state.*.cursor = .{ .x = x, .y = y }; +} + pub fn main() !void { var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = gp_allocator.allocator(); @@ -153,6 +168,7 @@ pub fn main() !void { win.setActiveCallback(handleActive); win.setResizeCallback(handleResize); win.setMouseButtonCallback(handleMouseButton); + win.setMouseMoveCallback(handleMouseMove); var buf = try Buffer.init(alloc, Width, Height); defer buf.deinit();