]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Tweak and extend debug info
authorElias Fleckenstein <eliasfleckenstein@web.de>
Tue, 24 Aug 2021 19:22:32 +0000 (21:22 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Tue, 24 Aug 2021 19:22:32 +0000 (21:22 +0200)
12 files changed:
README.md
src/CMakeLists.txt
src/client/client_player.c
src/client/debug_menu.c [new file with mode: 0644]
src/client/debug_menu.h [new file with mode: 0644]
src/client/font.c
src/client/font.h
src/client/game.c
src/client/hud.c
src/client/hud.h
src/client/input.c
src/client/window.c

index 77e4abbe93b92f197a89fb15e13ae14038f7ec8b..11e8b5e566a896596648b41933ed044694ec501e 100644 (file)
--- a/README.md
+++ b/README.md
@@ -30,6 +30,7 @@ or alternatively:
 | F | Toggle flight |
 | C | Toggle collision |
 | F11 | Toggle fullscreen |
+| F3 | Toggle debug info |
 | ESC | Pause / unpause game |
 
 ## Dependencies
index b8e67f88cd42f2505c5a882c6cc227d40f443dc1..bcf2e34f9b0a3bc896eb23a4c9d68e56cb50f121 100644 (file)
@@ -50,6 +50,7 @@ add_executable(Dragonblocks
        client/client_node.c
        client/client_player.c
        client/cube.c
+       client/debug_menu.c
        client/facecache.c
        client/font.c
        client/game.c
@@ -89,6 +90,20 @@ target_link_libraries(DragonblocksServer
        sqlite3
 )
 
+execute_process(COMMAND git tag --points-at HEAD
+       OUTPUT_VARIABLE GIT_VERSION
+       OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+
+if ("${GIT_VERSION}" STREQUAL "")
+       execute_process(COMMAND git rev-parse --short HEAD
+               OUTPUT_VARIABLE GIT_VERSION
+               OUTPUT_STRIP_TRAILING_WHITESPACE
+       )
+endif()
+
+add_compile_definitions(DRAGONBLOCKS_VERSION="${GIT_VERSION}")
+
 if (CMAKE_BUILD_TYPE STREQUAL "Release")
        add_compile_definitions(RELEASE)
 endif()
index 3171b7fc77ab77508f6c95bb03eeb3f5fd0f8563..67e49fea49ee35e44fc29d017549ee33b25e3fd6 100644 (file)
@@ -5,6 +5,7 @@
 #include "client/client_map.h"
 #include "client/client_player.h"
 #include "client/cube.h"
+#include "client/debug_menu.h"
 #include "client/texture.h"
 
 struct ClientPlayer client_player;
@@ -19,7 +20,9 @@ static void update_pos()
        client_player.obj->pos = (v3f32) {client_player.pos.x, client_player.pos.y, client_player.pos.z};
        object_transform(client_player.obj);
 
-       client_player_update_info();
+       debug_menu_update_pos();
+       debug_menu_update_humidity();
+       debug_menu_update_temperature();
 }
 
 // get absolute player bounding box
@@ -107,21 +110,12 @@ void client_player_add_to_scene()
                }
        }
 
-       client_player.info_hud = hud_add((HUDElementDefinition) {
-               .type = HUD_TEXT,
-               .pos = {-1.0f, -1.0f, 0.0f},
-               .offset = {2, 2 + 16 + 2 + 16 + 2},
-               .type_def = {
-                       .text = {
-                               .text = "",
-                               .color = {1.0f, 1.0f, 1.0f},
-                       },
-               },
-       });
-
        pthread_rwlock_rdlock(&client_player.rwlock);
        update_pos();
        pthread_rwlock_unlock(&client_player.rwlock);
+
+       debug_menu_update_yaw();
+       debug_menu_update_pitch();
 }
 
 // jump if possible
@@ -208,14 +202,3 @@ void client_player_tick(f64 dtime)
 
        pthread_rwlock_unlock(&client_player.rwlock);
 }
-
-// update HUD info text
-void client_player_update_info()
-{
-       v3s32 node_pos = {client_player.pos.x, client_player.pos.y, client_player.pos.z};
-
-       char info_text[BUFSIZ];
-       sprintf(info_text, "(%.1f %.1f %.1f) humidity: %.2f temperature: %.2f flight: %s collision: %s", client_player.pos.x, client_player.pos.y, client_player.pos.z, get_humidity(node_pos), get_temperature(node_pos), client_player.fly ? "enabled" : "disabled", client_player.collision ? "enabled" : "disabled");
-
-       hud_change_text(client_player.info_hud, info_text);
-}
diff --git a/src/client/debug_menu.c b/src/client/debug_menu.c
new file mode 100644 (file)
index 0000000..d9ecd14
--- /dev/null
@@ -0,0 +1,148 @@
+#include <stdio.h>
+#include <GL/glew.h>
+#include <GL/gl.h>
+#include "environment.h"
+#include "client/client_player.h"
+#include "client/debug_menu.h"
+#include "client/hud.h"
+#include "client/window.h"
+
+typedef enum
+{
+       DME_VERSION,
+       DME_FPS,
+       DME_POS,
+       DME_YAW,
+       DME_PITCH,
+       DME_HUMIDITY,
+       DME_TEMPERATURE,
+       DME_FLIGHT,
+       DME_COLLISION,
+       DME_FULLSCREEN,
+       DME_OPENGL,
+       DME_GPU,
+       DME_GLSL,
+       DME_COUNT,
+} DebugMenuEntry;
+
+static HUDElement *huds[DME_COUNT] = {NULL};
+
+static bool debug_menu_enabled = true;
+static DebugMenuEntry last_always_visible = DME_POS;
+
+void debug_menu_init()
+{
+       s32 offset = -16;
+
+       for (DebugMenuEntry i = 0; i < DME_COUNT; i++) {
+               huds[i] = hud_add((HUDElementDefinition) {
+                       .type = HUD_TEXT,
+                       .pos = {-1.0f, -1.0f, 0.0f},
+                       .offset = {2, offset += 18},
+                       .type_def = {
+                               .text = {
+                                       .text = "",
+                                       .color = {1.0f, 1.0f, 1.0f},
+                               },
+                       },
+               });
+       }
+}
+
+void debug_menu_toggle()
+{
+       debug_menu_enabled = ! debug_menu_enabled;
+
+       for (DebugMenuEntry i = 0; i < DME_COUNT; i++)
+               huds[i]->visible = debug_menu_enabled || i <= last_always_visible;
+}
+
+void debug_menu_update_version()
+{
+       hud_change_text(huds[DME_VERSION], "Dragonblocks Alpha " DRAGONBLOCKS_VERSION);
+}
+
+void debug_menu_update_fps(int fps)
+{
+       char text[BUFSIZ];
+       sprintf(text, "%d FPS", fps);
+       hud_change_text(huds[DME_FPS], text);
+}
+
+void debug_menu_update_pos()
+{
+       char text[BUFSIZ];
+       sprintf(text, "(%.1f %.1f %.1f)", client_player.pos.x, client_player.pos.y, client_player.pos.z);
+       hud_change_text(huds[DME_POS], text);
+}
+
+void debug_menu_update_yaw()
+{
+       char text[BUFSIZ];
+       sprintf(text, "yaw = %.1f", client_player.yaw / M_PI * 180.0);
+       hud_change_text(huds[DME_YAW], text);
+}
+
+void debug_menu_update_pitch()
+{
+       char text[BUFSIZ];
+       sprintf(text, "pitch = %.1f", client_player.pitch / M_PI * 180.0);
+       hud_change_text(huds[DME_PITCH], text);
+}
+
+void debug_menu_update_humidity()
+{
+       char text[BUFSIZ];
+       sprintf(text, "humidity = %.2f", get_humidity((v3s32) {client_player.pos.x, client_player.pos.y, client_player.pos.z}));
+       hud_change_text(huds[DME_HUMIDITY], text);
+}
+
+void debug_menu_update_temperature()
+{
+       char text[BUFSIZ];
+       sprintf(text, "temperature = %.2f", get_temperature((v3s32) {client_player.pos.x, client_player.pos.y, client_player.pos.z}));
+       hud_change_text(huds[DME_TEMPERATURE], text);
+}
+
+void debug_menu_update_flight()
+{
+       char text[BUFSIZ];
+       sprintf(text, "flight: %s", client_player.fly ? "enabled" : "disabled");
+       hud_change_text(huds[DME_FLIGHT], text);
+}
+
+void debug_menu_update_collision()
+{
+       char text[BUFSIZ];
+       sprintf(text, "collision: %s", client_player.collision ? "enabled" : "disabled");
+       hud_change_text(huds[DME_COLLISION], text);
+}
+
+void debug_menu_update_fullscreen()
+{
+       char text[BUFSIZ];
+       sprintf(text, "fullscreen: %s", window.fullscreen ? "enabled" : "disabled");
+       hud_change_text(huds[DME_FULLSCREEN], text);
+}
+
+void debug_menu_update_opengl()
+{
+       char text[BUFSIZ];
+       sprintf(text, "OpenGL %s", glGetString(GL_VERSION));
+       hud_change_text(huds[DME_OPENGL], text);
+}
+
+void debug_menu_update_gpu()
+{
+       char text[BUFSIZ];
+       sprintf(text, "%s", glGetString(GL_RENDERER));
+       hud_change_text(huds[DME_GPU], text);
+}
+
+void debug_menu_update_glsl()
+{
+       char text[BUFSIZ];
+       sprintf(text, "GLSL %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
+       hud_change_text(huds[DME_GLSL], text);
+}
+
diff --git a/src/client/debug_menu.h b/src/client/debug_menu.h
new file mode 100644 (file)
index 0000000..4a8ab58
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef _DEBUG_MENU_H_
+#define _DEBUG_MENU_H_
+
+#include <stdbool.h>
+
+void debug_menu_init();
+void debug_menu_toggle();
+
+void debug_menu_update_version();
+void debug_menu_update_fps(int fps);
+void debug_menu_update_pos();
+void debug_menu_update_yaw();
+void debug_menu_update_pitch();
+void debug_menu_update_humidity();
+void debug_menu_update_temperature();
+void debug_menu_update_flight();
+void debug_menu_update_collision();
+void debug_menu_update_fullscreen();
+void debug_menu_update_opengl();
+void debug_menu_update_gpu();
+void debug_menu_update_glsl();
+
+#endif
index ac3c251cbc9df1d38b664c0b75c3b3c2e302a313..01d9c77dc49250b51f4a093adfe5aa30c7b819bc 100644 (file)
@@ -109,7 +109,7 @@ void font_deinit()
        }
 }
 
-Font *font_create(char *text)
+Font *font_create(const char *text)
 {
        Font *fnt = malloc(sizeof(fnt));
 
index e8d42e6020b4c5a8671631bd33561762f744563e..6e3bb619d402521a6bf4d64ad029deb1256ff7f3 100644 (file)
@@ -13,7 +13,7 @@ typedef struct
 
 bool font_init();
 void font_deinit();
-Font *font_create(char *text);
+Font *font_create(const char *text);
 void font_delete(Font *fnt);
 void font_render(Font *fnt);
 
index 48f80fde82af0a0cf64bc98e20ed0461e9e777df..4368b122d00e7938f466f1128bf6d73e46a07a29 100644 (file)
@@ -8,26 +8,31 @@
 #include "client/client_map.h"
 #include "client/client_node.h"
 #include "client/client_player.h"
+#include "client/debug_menu.h"
 #include "client/hud.h"
 #include "client/input.h"
 #include "client/font.h"
 #include "client/window.h"
 #include "signal_handlers.h"
 
-static void game_loop(Client *client)
+static void crosshair_init()
 {
-       HUDElement *fps_hud = hud_add((HUDElementDefinition) {
-               .type = HUD_TEXT,
-               .pos = {-1.0f, -1.0f, 0.0f},
-               .offset = {2, 2 + 16 + 2},
+       hud_add((HUDElementDefinition) {
+               .type = HUD_IMAGE,
+               .pos = {0.0f, 0.0f, 0.0f},
+               .offset = {0, 0},
                .type_def = {
-                       .text = {
-                               .text = "",
-                               .color = {1.0f, 1.0f, 1.0f},
+                       .image = {
+                               .texture = texture_get(RESSOURCEPATH "textures/crosshair.png"),
+                               .scale = {1.0f, 1.0f},
+                               .scale_type = HUD_SCALE_TEXTURE,
                        },
                },
        });
+}
 
+static void game_loop(Client *client)
+{
        f64 fps_update_timer = 1.0f;
        int frames = 0;
 
@@ -40,9 +45,7 @@ static void game_loop(Client *client)
                ts_old = ts;
 
                if ((fps_update_timer -= dtime) <= 0.0) {
-                       char fps[((int) log10(frames) + 1) + 1 + 3 + 1];
-                       sprintf(fps, "%d FPS", frames);
-                       hud_change_text(fps_hud, fps);
+                       debug_menu_update_fps(frames);
                        fps_update_timer += 1.0;
                        frames = 0;
                }
@@ -97,30 +100,18 @@ bool game(Client *client)
 
        hud_on_resize(width, height);
 
-       hud_add((HUDElementDefinition) {
-               .type = HUD_IMAGE,
-               .pos = {0.0f, 0.0f, 0.0f},
-               .offset = {0, 0},
-               .type_def = {
-                       .image = {
-                               .texture = texture_get(RESSOURCEPATH "textures/crosshair.png"),
-                               .scale = {1.0f, 1.0f},
-                               .scale_type = HUD_SCALE_TEXTURE,
-                       },
-               },
-       });
-
-       hud_add((HUDElementDefinition) {
-               .type = HUD_TEXT,
-               .pos = {-1.0f, -1.0f, 0.0f},
-               .offset = {2, 2},
-               .type_def = {
-                       .text = {
-                               .text = "Dragonblocks Alpha",   // ToDo: add version
-                               .color = {1.0f, 1.0f, 1.0f},
-                       },
-               },
-       });
+       debug_menu_init();
+       debug_menu_toggle();
+       debug_menu_update_fps(0);
+       debug_menu_update_version();
+       debug_menu_update_flight();
+       debug_menu_update_collision();
+       debug_menu_update_fullscreen();
+       debug_menu_update_opengl();
+       debug_menu_update_gpu();
+       debug_menu_update_glsl();
+
+       crosshair_init();
 
        input_init();
 
index 071a45eb28a8621ff87f9a3274dc53617671c3e8..a903164f51b85e298111a15a9f1ade3caca34eba 100644 (file)
@@ -225,7 +225,7 @@ HUDElement *hud_add(HUDElementDefinition def)
        return element;
 }
 
-void hud_change_text(HUDElement *element, char *text)
+void hud_change_text(HUDElement *element, const char *text)
 {
        if (strcmp(element->def.type_def.text.text, text)) {
                element->def.type_def.text.text = strdup(text);
index d58af37c5aba13cb1e6c019d559c266ae9db8dde..70d4e78466d0d94c85d384d22d7b2e0bc2f4aa1a 100644 (file)
@@ -56,6 +56,6 @@ void hud_deinit();
 void hud_on_resize(int width, int height);
 void hud_render();
 HUDElement *hud_add(HUDElementDefinition def);
-void hud_change_text(HUDElement *element, char *text);
+void hud_change_text(HUDElement *element, const char *text);
 
 #endif
index c2cd869219f35a3ec8dcf709c821497084855d1e..61efa7af98eacc6afcd121be94a2967a2b0f50a8 100644 (file)
@@ -2,6 +2,7 @@
 #include "client/camera.h"
 #include "client/client.h"
 #include "client/client_player.h"
+#include "client/debug_menu.h"
 #include "client/hud.h"
 #include "client/input.h"
 #include "client/window.h"
@@ -21,6 +22,7 @@ static struct
        KeyListener fullscreen_listener;
        KeyListener fly_listener;
        KeyListener collision_listener;
+       KeyListener debug_menu_listener;
 } input;
 
 void input_on_cursor_pos(double current_x, double current_y)
@@ -38,9 +40,13 @@ void input_on_cursor_pos(double current_x, double current_y)
        client_player.yaw += (f32) delta_x * M_PI / 180.0f / 8.0f;
        client_player.pitch -= (f32) delta_y * M_PI / 180.0f / 8.0f;
 
+       client_player.yaw = fmod(client_player.yaw + M_PI * 2.0f, M_PI * 2.0f);
        client_player.pitch = fmax(fmin(client_player.pitch, M_PI / 2.0f - 0.01f), -M_PI / 2.0f + 0.01f);
 
        camera_set_angle(client_player.yaw, client_player.pitch);
+
+       debug_menu_update_yaw();
+       debug_menu_update_pitch();
 }
 
 static bool move(int forward, int backward, vec3 dir)
@@ -88,8 +94,6 @@ void input_tick()
 {
        do_key_listener(&input.pause_listener);
        do_key_listener(&input.fullscreen_listener);
-       do_key_listener(&input.fly_listener);
-       do_key_listener(&input.collision_listener);
 
        if (input.pause_listener.fired) {
                input.paused = ! input.paused;
@@ -109,14 +113,23 @@ void input_tick()
                        window_enter_fullscreen();
        }
 
-       if (input.fly_listener.fired) {
-               client_player.fly = ! client_player.fly;
-               client_player_update_info();
-       }
+       if (! input.paused) {
+               do_key_listener(&input.fly_listener);
+               do_key_listener(&input.collision_listener);
+               do_key_listener(&input.debug_menu_listener);
+
+               if (input.fly_listener.fired) {
+                       client_player.fly = ! client_player.fly;
+                       debug_menu_update_flight();
+               }
+
+               if (input.collision_listener.fired) {
+                       client_player.collision = ! client_player.collision;
+                       debug_menu_update_collision();
+               }
 
-       if (input.collision_listener.fired) {
-               client_player.collision = ! client_player.collision;
-               client_player_update_info();
+               if (input.debug_menu_listener.fired)
+                       debug_menu_toggle();
        }
 
        client_player.velocity.x = 0.0f;
@@ -144,6 +157,7 @@ void input_init()
        input.fullscreen_listener = create_key_listener(GLFW_KEY_F11);
        input.fly_listener = create_key_listener(GLFW_KEY_F);
        input.collision_listener = create_key_listener(GLFW_KEY_C);
+       input.debug_menu_listener = create_key_listener(GLFW_KEY_F3);
 
        input.pause_menu_hud = hud_add((HUDElementDefinition) {
                .type = HUD_IMAGE,
index 9f71403c78a509e09daf5552c2a08cf082a52a9b..511ee4e4d990be7cac2cbf71e09b8ee089777846 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <GL/glew.h>
 #include <GL/gl.h>
+#include "client/debug_menu.h"
 #include "client/hud.h"
 #include "client/input.h"
 #include "client/scene.h"
@@ -41,12 +42,16 @@ void window_enter_fullscreen()
        GLFWmonitor *monitor = glfwGetPrimaryMonitor();
        const GLFWvidmode *vidmode = glfwGetVideoMode(monitor);
        glfwSetWindowMonitor(window.handle, monitor, 0, 0, vidmode->width, vidmode->height, 0);
+
+       debug_menu_update_fullscreen();
 }
 
 void window_exit_fullscreen()
 {
        window.fullscreen = false;
        glfwSetWindowMonitor(window.handle, NULL, window.small_bounds.x, window.small_bounds.y, window.small_bounds.width, window.small_bounds.height, 0);
+
+       debug_menu_update_fullscreen();
 }
 
 bool window_init(int width, int height)