summaryrefslogtreecommitdiff
path: root/src/renderer
diff options
context:
space:
mode:
authorLorenzo Torres <torres@sideros.org>2025-03-29 14:51:31 +0100
committerLorenzo Torres <torres@sideros.org>2025-03-29 14:51:31 +0100
commitfd7973173f163e068deb0ae8f9d6ff0fc31fc71b (patch)
tree3d4af1de3a1d1eab487da6368b32fb3dca9e5f69 /src/renderer
parent4a43e564d7a137097a8403f807b63a2998e0c090 (diff)
Added cursor input management
Diffstat (limited to 'src/renderer')
-rw-r--r--src/renderer/Camera.zig26
-rw-r--r--src/renderer/Window.zig23
2 files changed, 48 insertions, 1 deletions
diff --git a/src/renderer/Camera.zig b/src/renderer/Camera.zig
index 9794da3..9ab305c 100644
--- a/src/renderer/Camera.zig
+++ b/src/renderer/Camera.zig
@@ -15,7 +15,31 @@ position: @Vector(3, f32),
target: @Vector(3, f32),
direction: @Vector(3, f32),
right: @Vector(3, f32),
+front: @Vector(3, f32),
up: @Vector(3, f32),
+speed: f32 = 2.5,
-fn input(pool: *ecs.Pool) void {
+fn getProjection(width: usize, height: usize) math.Matrix {
+ return math.Matrix.perspective(math.rad(45.0), (@as(f32, @floatFromInt(width)) / @as(f32, @floatFromInt(height))), 0.1, 10.0);
+}
+
+fn getView(self: Camera) math.Matrix {
+ math.lookAt(self.position, self.position + self.front, self.up);
+}
+
+fn moveCamera(pool: *ecs.Pool) void {
+ const input = pool.resources.input;
+ const camera = pool.resources.camera;
+ if (input.isKeyDown(.w)) {
+ camera.position += (camera.front * (camera.speed * pool.resources.delta_time));
+ }
+ if (input.isKeyDown(.s)) {
+ camera.position -= (camera.front * (camera.speed * pool.resources.delta_time));
+ }
+ if (input.isKeyDown(.a)) {
+ camera.position -= math.normalize(math.cross(camera.front, camera.up)) * (camera.speed * pool.resources.delta_time);
+ }
+ if (input.isKeyDown(.d)) {
+ camera.position += math.normalize(math.cross(camera.front, camera.up)) * (camera.speed * pool.resources.delta_time);
+ }
}
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;
+ }
+}