]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/input.c
Optimize MapBlock meshes
[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->yaw += (float) delta_x * M_PI / 180.0f / 8.0f;
22         input.client->pitch -= (float) delta_y * M_PI / 180.0f / 8.0f;
23
24         input.client->pitch = fmax(fmin(input.client->pitch, 89.0f), -89.0f);
25
26         set_camera_angle(input.client->yaw, input.client->pitch);
27 }
28
29 static bool move(int forward, int backward, vec3 speed)
30 {
31         float sign;
32
33         if (glfwGetKey(input.window, forward) == GLFW_PRESS)
34                 sign = +1.0f;
35         else if (glfwGetKey(input.window, backward) == GLFW_PRESS)
36                 sign = -1.0f;
37         else
38                 return false;
39
40         input.client->pos.x += speed[0] * sign;
41         input.client->pos.y += speed[1] * sign;
42         input.client->pos.z += speed[2] * sign;
43
44         return true;
45 }
46
47 void process_input()
48 {
49         bool moved_forward = move(GLFW_KEY_W, GLFW_KEY_S, movement_dirs.front);
50         bool moved_up = move(GLFW_KEY_SPACE, GLFW_KEY_LEFT_SHIFT, movement_dirs.up);
51         bool moved_right = move(GLFW_KEY_D, GLFW_KEY_A, movement_dirs.right);
52
53         if (moved_forward || moved_up || moved_right) {
54                 set_camera_position(input.client->pos);
55
56                 pthread_mutex_lock(&input.client->mtx);
57                 (void) (write_u32(input.client->fd, SC_POS) && write_v3f32(input.client->fd, input.client->pos));
58                 pthread_mutex_unlock(&input.client->mtx);
59         }
60 }
61
62 void init_input(Client *client, GLFWwindow *window)
63 {
64         input.client = client;
65         input.window = window;
66
67         glfwSetCursorPosCallback(input.window, &cursor_pos_callback);
68 }
69
70