diff options
author | Lorenzo Torres <torres@sideros.org> | 2025-03-29 14:51:31 +0100 |
---|---|---|
committer | Lorenzo Torres <torres@sideros.org> | 2025-03-29 14:51:31 +0100 |
commit | fd7973173f163e068deb0ae8f9d6ff0fc31fc71b (patch) | |
tree | 3d4af1de3a1d1eab487da6368b32fb3dca9e5f69 /src/renderer/Window.zig | |
parent | 4a43e564d7a137097a8403f807b63a2998e0c090 (diff) |
Added cursor input management
Diffstat (limited to 'src/renderer/Window.zig')
-rw-r--r-- | src/renderer/Window.zig | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/renderer/Window.zig b/src/renderer/Window.zig index c05391b..bc8f83b 100644 --- a/src/renderer/Window.zig +++ b/src/renderer/Window.zig @@ -38,6 +38,7 @@ pub fn create(width: usize, height: usize, title: []const u8) !Window { const raw = c.glfwCreateWindow(@intCast(width), @intCast(height), title.ptr, null, null); c.glfwShowWindow(raw); _ = c.glfwSetKeyCallback(raw, keyCallback); + _ = c.glfwSetCursorPosCallback(raw, cursorCallback); return Window{ .title = title, @@ -73,6 +74,10 @@ pub fn destroy(self: Window) void { c.glfwTerminate(); } +pub fn getTime() f32 { + return c.glfwGetTime(); +} + pub fn keyCallback(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.c) void { _ = scancode; _ = mods; @@ -85,3 +90,21 @@ pub fn keyCallback(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: } } } + +pub fn cursorCallback(window: ?*c.GLFWwindow, x: f64, y: f64) callconv(.c) void { + if (c.glfwGetWindowUserPointer(window)) |r| { + const resources: *ecs.Resources = @alignCast(@ptrCast(r)); + var input = resources.input; + + if (input.mouse_first) { + input.mouse_x = x; + input.mouse_y = y; + input.mouse_first = false; + } + + input.mouse_delta_x = (x - input.mouse_x); + input.mouse_delta_y = (y - input.mouse_y); + input.mouse_x = x; + input.mouse_y = y; + } +} |