Added cursor input management
This commit is contained in:
parent
4a43e564d7
commit
fd7973173f
3 changed files with 63 additions and 2 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue