5 #include "client/camera.h"
6 #include "client/client.h"
7 #include "client/client_config.h"
8 #include "client/client_inventory.h"
9 #include "client/client_player.h"
10 #include "client/debug_menu.h"
11 #include "client/gui.h"
12 #include "client/interact.h"
13 #include "client/input.h"
14 #include "client/screenshot.h"
15 #include "client/window.h"
16 #include "common/day.h"
18 #define SET_STATUS_MESSAGE(args...) { \
19 char *msg; asprintf(&msg, args); \
20 gui_text(status_message, msg); free(msg); \
21 status_message->def.text_color.w = 1.01f; }
28 static bool paused = false;
30 static GUIElement *pause_menu;
31 static GUIElement *status_message;
33 static KeyListener listener_pause = {GLFW_KEY_ESCAPE, false};
34 static KeyListener listener_fullscreen = {GLFW_KEY_F11, false};
35 static KeyListener listener_fly = {GLFW_KEY_F, false};
36 static KeyListener listener_collision = {GLFW_KEY_C, false};
37 static KeyListener listener_timelapse = {GLFW_KEY_T, false};
38 static KeyListener listener_debug_menu = {GLFW_KEY_F3, false};
39 static KeyListener listener_screenshot = {GLFW_KEY_F2, false};
41 static double cursor_last_x = 0.0;
42 static double cursor_last_y = 0.0;
44 // movement mutex needs to be locked
45 static bool move(int forward, int backward, vec3 dir)
50 if (glfwGetKey(window.handle, forward) == GLFW_PRESS)
52 else if (glfwGetKey(window.handle, backward) == GLFW_PRESS)
57 client_player.velocity.x += dir[0] * client_player.movement.speed * sign;
58 client_player.velocity.y += dir[1] * client_player.movement.speed * sign;
59 client_player.velocity.z += dir[2] * client_player.movement.speed * sign;
64 static void enter_game()
66 glfwSetInputMode(window.handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
67 pause_menu->visible = false;
70 static bool key_listener(KeyListener *listener)
72 bool was = listener->state;
73 return !(listener->state = (glfwGetKey(window.handle, listener->key) == GLFW_PRESS)) && was;
78 pause_menu = gui_add(NULL, (GUIElementDef) {
83 .align = {0.0f, 0.0f},
84 .scale = {1.0f, 1.0f},
85 .scale_type = SCALE_PARENT,
86 .affect_parent_scale = false,
89 .text_color = {0.0f, 0.0f, 0.0f, 0.0f},
90 .bg_color = {0.0f, 0.0f, 0.0f, 0.4f},
93 status_message = gui_add(NULL, (GUIElementDef) {
98 .align = {0.5f, 0.5f},
99 .scale = {1.0f, 1.0f},
100 .scale_type = SCALE_TEXT,
101 .affect_parent_scale = false,
104 .text_color = {1.0f, 0.91f, 0.13f, 0.0f},
105 .bg_color = {0.0f, 0.0f, 0.0f, 0.0f},
108 glfwSetInputMode(window.handle, GLFW_STICKY_KEYS, GL_TRUE);
112 void input_tick(f64 dtime)
114 if (status_message->def.text_color.w > 1.0f)
115 status_message->def.text_color.w = 1.0f;
116 else if (status_message->def.text_color.w > 0.0f)
117 status_message->def.text_color.w -= dtime * 1.0f;
119 if (key_listener(&listener_pause)) {
123 glfwSetInputMode(window.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
124 pause_menu->visible = true;
130 if (key_listener(&listener_fullscreen)) {
131 if (window.fullscreen)
132 window_exit_fullscreen();
134 window_enter_fullscreen();
138 if (key_listener(&listener_fly)) {
139 pthread_rwlock_wrlock(&client_player.lock_movement);
140 client_player.movement.flight = !client_player.movement.flight;
142 SET_STATUS_MESSAGE("Flight %s", client_player.movement.flight ? "Enabled" : "Disabled")
143 debug_menu_changed(ENTRY_FLIGHT);
145 pthread_rwlock_unlock(&client_player.lock_movement);
148 if (key_listener(&listener_collision)) {
149 pthread_rwlock_wrlock(&client_player.lock_movement);
150 client_player.movement.collision = !client_player.movement.collision;
152 SET_STATUS_MESSAGE("Collision %s", client_player.movement.collision ? "Enabled" : "Disabled")
153 debug_menu_changed(ENTRY_COLLISION);
155 pthread_rwlock_unlock(&client_player.lock_movement);
158 if (key_listener(&listener_timelapse)) {
159 f64 current_time = get_time_of_day();
160 timelapse = !timelapse;
161 set_time_of_day(current_time);
163 SET_STATUS_MESSAGE("Timelapse %s", timelapse ? "Enabled" : "Disabled")
164 debug_menu_changed(ENTRY_TIMELAPSE);
167 if (key_listener(&listener_debug_menu))
170 if (key_listener(&listener_screenshot)) {
171 char *screenshot_filename = screenshot();
172 SET_STATUS_MESSAGE("Screenshot saved to %s", screenshot_filename)
173 free(screenshot_filename);
177 pthread_rwlock_rdlock(&client_player.lock_movement);
179 client_player.velocity.x = 0.0f;
180 client_player.velocity.z = 0.0f;
182 if (client_player.movement.flight)
183 client_player.velocity.y = 0.0f;
186 move(GLFW_KEY_W, GLFW_KEY_S, camera.movement_dirs.front);
187 move(GLFW_KEY_D, GLFW_KEY_A, camera.movement_dirs.right);
189 if (client_player.movement.flight)
190 move(GLFW_KEY_SPACE, GLFW_KEY_LEFT_SHIFT, camera.movement_dirs.up);
191 else if (glfwGetKey(window.handle, GLFW_KEY_SPACE) == GLFW_PRESS)
192 client_player_jump();
195 pthread_rwlock_unlock(&client_player.lock_movement);
198 void input_cursor(double current_x, double current_y)
203 double delta_x = current_x - cursor_last_x;
204 double delta_y = current_y - cursor_last_y;
205 cursor_last_x = current_x;
206 cursor_last_y = current_y;
208 ClientEntity *entity = client_player_entity_local();
212 pthread_rwlock_wrlock(&entity->lock_pos_rot);
214 entity->data.rot.y -= (f32) delta_x * M_PI / 180.0f / 8.0f;
215 entity->data.rot.x += (f32) delta_y * M_PI / 180.0f / 8.0f;
217 entity->data.rot.y = fmod(entity->data.rot.y + M_PI * 2.0f, M_PI * 2.0f);
218 entity->data.rot.x = f32_clamp(entity->data.rot.x, -M_PI / 2.0f + 0.01f, M_PI / 2.0f - 0.01f);
220 client_player_update_rot(entity);
221 pthread_rwlock_unlock(&entity->lock_pos_rot);
222 refcount_drp(&entity->rc);
225 void input_click(bool left)
227 if (client_config.swap_mouse_buttons)