Made ECS a separate module and implemented basic input handling.

This commit is contained in:
Lorenzo Torres 2025-03-28 19:53:56 +01:00
parent 64c9d32905
commit 536c927613
6 changed files with 172 additions and 7 deletions

View file

@ -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;
}
}
}