]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/input.c
Add falling
[dragonblocks_alpha.git] / src / input.c
1 #include <math.h>
2 #include "camera.h"
3 #include "client.h"
4 #include "input.h"
5
6 static struct
7 {
8         GLFWwindow *window;
9         Client *client;
10 } input;
11
12 static void cursor_pos_callback(__attribute__((unused)) GLFWwindow* window, double current_x, double current_y)
13 {
14         static double last_x, last_y = 0.0;
15
16         double delta_x = current_x - last_x;
17         double delta_y = current_y - last_y;
18         last_x = current_x;
19         last_y = current_y;
20
21         input.client->player.yaw += (f32) delta_x * M_PI / 180.0f / 8.0f;
22         input.client->player.pitch -= (f32) delta_y * M_PI / 180.0f / 8.0f;
23
24         input.client->player.pitch = fmax(fmin(input.client->player.pitch, 89.0f), -89.0f);
25
26         set_camera_angle(input.client->player.yaw, input.client->player.pitch);
27 }
28
29 static bool move(int forward, int backward, vec3 dir)
30 {
31         f32 sign;
32         f32 speed = 10.0f;
33
34         if (glfwGetKey(input.window, forward) == GLFW_PRESS)
35                 sign = +1.0f;
36         else if (glfwGetKey(input.window, backward) == GLFW_PRESS)
37                 sign = -1.0f;
38         else
39                 return false;
40
41         input.client->player.velocity.x += dir[0] * speed * sign;
42         // input.client->player.velocity.y += dir[1] * speed * sign;
43         input.client->player.velocity.z += dir[2] * speed * sign;
44
45         return true;
46 }
47
48 void process_input()
49 {
50         input.client->player.velocity.x = 0.0f;
51         // input.client->player.velocity.y = 0.0f;
52         input.client->player.velocity.z = 0.0f;
53
54         move(GLFW_KEY_W, GLFW_KEY_S, movement_dirs.front);
55         move(GLFW_KEY_SPACE, GLFW_KEY_LEFT_SHIFT, movement_dirs.up);
56         move(GLFW_KEY_D, GLFW_KEY_A, movement_dirs.right);
57 }
58
59 void init_input(Client *client, GLFWwindow *window)
60 {
61         input.client = client;
62         input.window = window;
63
64         glfwSetCursorPosCallback(input.window, &cursor_pos_callback);
65 }
66
67