1 #include <asprintf/asprintf.h>
5 #include "client/camera.h"
6 #include "client/client.h"
7 #include "client/client_player.h"
8 #include "client/debug_menu.h"
9 #include "client/gui.h"
10 #include "client/input.h"
11 #include "client/screenshot.h"
12 #include "client/window.h"
15 #define SET_STATUS_MESSAGE(args...) { \
16 char *msg; asprintf(&msg, args); \
17 gui_text(status_message, msg); free(msg); \
18 status_message->def.text_color.w = 1.01f; }
25 static bool paused = false;
27 static GUIElement *pause_menu;
28 static GUIElement *status_message;
30 static KeyListener listener_pause = {GLFW_KEY_ESCAPE, false};
31 static KeyListener listener_fullscreen = {GLFW_KEY_F11, false};
32 static KeyListener listener_fly = {GLFW_KEY_F, false};
33 static KeyListener listener_collision = {GLFW_KEY_C, false};
34 static KeyListener listener_timelapse = {GLFW_KEY_T, false};
35 static KeyListener listener_debug_menu = {GLFW_KEY_F3, false};
36 static KeyListener listener_screenshot = {GLFW_KEY_F2, false};
38 static double cursor_last_x = 0.0;
39 static double cursor_last_y = 0.0;
41 // movement mutex needs to be locked
42 static bool move(int forward, int backward, vec3 dir)
47 if (glfwGetKey(window.handle, forward) == GLFW_PRESS)
49 else if (glfwGetKey(window.handle, backward) == GLFW_PRESS)
54 client_player.velocity.x += dir[0] * client_player.movement.speed * sign;
55 client_player.velocity.y += dir[1] * client_player.movement.speed * sign;
56 client_player.velocity.z += dir[2] * client_player.movement.speed * sign;
61 static void enter_game()
63 glfwSetInputMode(window.handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
64 pause_menu->visible = false;
67 static bool key_listener(KeyListener *listener)
69 bool was = listener->state;
70 return !(listener->state = (glfwGetKey(window.handle, listener->key) == GLFW_PRESS)) && was;
75 pause_menu = gui_add(NULL, (GUIElementDef) {
80 .align = {0.0f, 0.0f},
81 .scale = {1.0f, 1.0f},
82 .scale_type = SCALE_PARENT,
83 .affect_parent_scale = false,
86 .text_color = {0.0f, 0.0f, 0.0f, 0.0f},
87 .bg_color = {0.0f, 0.0f, 0.0f, 0.4f},
90 status_message = gui_add(NULL, (GUIElementDef) {
95 .align = {0.5f, 0.5f},
96 .scale = {1.0f, 1.0f},
97 .scale_type = SCALE_TEXT,
98 .affect_parent_scale = false,
101 .text_color = {1.0f, 0.91f, 0.13f, 0.0f},
102 .bg_color = {0.0f, 0.0f, 0.0f, 0.0f},
105 glfwSetInputMode(window.handle, GLFW_STICKY_KEYS, GL_TRUE);
109 void input_tick(f64 dtime)
111 if (status_message->def.text_color.w > 1.0f)
112 status_message->def.text_color.w = 1.0f;
113 else if (status_message->def.text_color.w > 0.0f)
114 status_message->def.text_color.w -= dtime * 1.0f;
116 if (key_listener(&listener_pause)) {
120 glfwSetInputMode(window.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
121 pause_menu->visible = true;
127 if (key_listener(&listener_fullscreen)) {
128 if (window.fullscreen)
129 window_exit_fullscreen();
131 window_enter_fullscreen();
135 if (key_listener(&listener_fly)) {
136 pthread_rwlock_wrlock(&client_player.lock_movement);
137 client_player.movement.flight = !client_player.movement.flight;
139 SET_STATUS_MESSAGE("Flight %s", client_player.movement.flight ? "Enabled" : "Disabled")
140 debug_menu_changed(ENTRY_FLIGHT);
142 pthread_rwlock_unlock(&client_player.lock_movement);
145 if (key_listener(&listener_collision)) {
146 pthread_rwlock_wrlock(&client_player.lock_movement);
147 client_player.movement.collision = !client_player.movement.collision;
149 SET_STATUS_MESSAGE("Collision %s", client_player.movement.collision ? "Enabled" : "Disabled")
150 debug_menu_changed(ENTRY_COLLISION);
152 pthread_rwlock_unlock(&client_player.lock_movement);
155 if (key_listener(&listener_timelapse)) {
156 f64 current_time = get_time_of_day();
157 timelapse = !timelapse;
158 set_time_of_day(current_time);
160 SET_STATUS_MESSAGE("Timelapse %s", timelapse ? "Enabled" : "Disabled")
161 debug_menu_changed(ENTRY_TIMELAPSE);
164 if (key_listener(&listener_debug_menu))
167 if (key_listener(&listener_screenshot)) {
168 char *screenshot_filename = screenshot();
169 SET_STATUS_MESSAGE("Screenshot saved to %s", screenshot_filename)
170 free(screenshot_filename);
174 pthread_rwlock_rdlock(&client_player.lock_movement);
176 client_player.velocity.x = 0.0f;
177 client_player.velocity.z = 0.0f;
179 if (client_player.movement.flight)
180 client_player.velocity.y = 0.0f;
183 move(GLFW_KEY_W, GLFW_KEY_S, camera.movement_dirs.front);
184 move(GLFW_KEY_D, GLFW_KEY_A, camera.movement_dirs.right);
186 if (client_player.movement.flight)
187 move(GLFW_KEY_SPACE, GLFW_KEY_LEFT_SHIFT, camera.movement_dirs.up);
188 else if (glfwGetKey(window.handle, GLFW_KEY_SPACE) == GLFW_PRESS)
189 client_player_jump();
192 pthread_rwlock_unlock(&client_player.lock_movement);
195 void input_cursor(double current_x, double current_y)
200 double delta_x = current_x - cursor_last_x;
201 double delta_y = current_y - cursor_last_y;
202 cursor_last_x = current_x;
203 cursor_last_y = current_y;
205 ClientEntity *entity = client_player_entity_local();
209 pthread_rwlock_wrlock(&entity->lock_pos_rot);
211 entity->data.rot.y -= (f32) delta_x * M_PI / 180.0f / 8.0f;
212 entity->data.rot.x += (f32) delta_y * M_PI / 180.0f / 8.0f;
214 entity->data.rot.y = fmod(entity->data.rot.y + M_PI * 2.0f, M_PI * 2.0f);
215 entity->data.rot.x = f32_clamp(entity->data.rot.x, -M_PI / 2.0f + 0.01f, M_PI / 2.0f - 0.01f);
217 client_player_update_rot(entity);
218 pthread_rwlock_unlock(&entity->lock_pos_rot);
219 refcount_drp(&entity->rc);