From 536c927613948f884ca441b9045afe89c45e0440 Mon Sep 17 00:00:00 2001 From: Lorenzo Torres Date: Fri, 28 Mar 2025 19:53:56 +0100 Subject: Made ECS a separate module and implemented basic input handling. --- src/renderer/Window.zig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/renderer') diff --git a/src/renderer/Window.zig b/src/renderer/Window.zig index a80f277..9ef389d 100644 --- a/src/renderer/Window.zig +++ b/src/renderer/Window.zig @@ -1,4 +1,5 @@ const c = @import("c.zig"); +const ecs = @import("ecs"); const std = @import("std"); const Window = @This(); @@ -36,6 +37,7 @@ pub fn create(width: usize, height: usize, title: []const u8) !Window { c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API); const raw = c.glfwCreateWindow(@intCast(width), @intCast(height), title.ptr, null, null); c.glfwShowWindow(raw); + _ = c.glfwSetKeyCallback(raw, keyCallback); return Window{ .title = title, @@ -45,6 +47,10 @@ pub fn create(width: usize, height: usize, title: []const u8) !Window { }; } +pub fn setResources(self: *Window, resources: *ecs.Resources) void { + c.glfwSetWindowUserPointer(self.raw, resources); +} + pub fn pollEvents() void { c.glfwPollEvents(); } @@ -66,3 +72,17 @@ pub fn destroy(self: Window) void { c.glfwDestroyWindow(self.raw); c.glfwTerminate(); } + +pub fn keyCallback(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.c) void { + _ = scancode; + _ = mods; + std.debug.print("test {d}\n", .{key}); + if (c.glfwGetWindowUserPointer(window)) |r| { + const resources: *ecs.Resources = @alignCast(@ptrCast(r)); + if (action == c.GLFW_PRESS) { + resources.input.key_pressed[@intCast(key)] = true; + } else if (action == c.GLFW_RELEASE) { + resources.input.key_pressed[@intCast(key)] = false; + } + } +} -- cgit v1.2.3