summaryrefslogtreecommitdiffstats
path: root/src/game/client/gui/metrics.cc
blob: bf466490c8d2bfe485a842944fe61dec30868a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "client/pch.hh"

#include "client/gui/metrics.hh"

#include "core/version.hh"

#include "shared/entity/grounded.hh"
#include "shared/entity/head.hh"
#include "shared/entity/transform.hh"
#include "shared/entity/velocity.hh"

#include "shared/world/dimension.hh"

#include "shared/coord.hh"

#include "client/entity/camera.hh"

#include "client/gui/imdraw_ext.hh"

#include "client/game.hh"
#include "client/globals.hh"
#include "client/session.hh"

constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs
    | ImGuiWindowFlags_NoNav;

static std::basic_string<GLubyte> r_version;
static std::basic_string<GLubyte> r_renderer;

void gui::metrics::init(void)
{
    r_version = std::basic_string<GLubyte>(glGetString(GL_VERSION));
    r_renderer = std::basic_string<GLubyte>(glGetString(GL_RENDERER));
}

void gui::metrics::layout(void)
{
    if(!session::is_ingame()) {
        // Sanity check; we are checking this
        // in client_game before calling layout
        // on HUD-ish GUI systems but still
        return;
    }

    auto draw_list = ImGui::GetForegroundDrawList();

    // FIXME: maybe use style colors instead of hardcoding?
    auto text_color = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
    auto shadow_color = ImGui::GetColorU32(ImVec4(0.1f, 0.1f, 0.1f, 1.0f));

    auto font_size = 4.0f;
    auto position = ImVec2(8.0f, 8.0f);
    auto y_step = 1.5f * globals::gui_scale * font_size;

    // Draw version
    auto version_line = std::format("Voxelius {}", version::full);
    gui::imdraw_ext::text_shadow(version_line, position, text_color, shadow_color, globals::font_unscii8, draw_list, font_size);
    position.y += 1.5f * y_step;

    // Draw client-side window framerate metrics
    auto window_framerate = 1.0f / globals::window_frametime_avg;
    auto window_frametime = 1000.0f * globals::window_frametime_avg;
    auto window_fps_line = std::format("{:.02f} FPS [{:.02f} ms]", window_framerate, window_frametime);
    gui::imdraw_ext::text_shadow(window_fps_line, position, text_color, shadow_color, globals::font_unscii8, draw_list, font_size);
    position.y += y_step;

    // Draw world rendering metrics
    auto drawcall_line = std::format("World: {} DC / {} TRI", globals::num_drawcalls, globals::num_triangles);
    gui::imdraw_ext::text_shadow(drawcall_line, position, text_color, shadow_color, globals::font_unscii8, draw_list, font_size);
    position.y += y_step;

    // Draw OpenGL version string
    auto r_version_line = std::format("GL_VERSION: {}", reinterpret_cast<const char*>(r_version.c_str()));
    gui::imdraw_ext::text_shadow(r_version_line, position, text_color, shadow_color, globals::font_unscii8, draw_list, font_size);
    position.y += y_step;

    // Draw OpenGL renderer string
    auto r_renderer_line = std::format("GL_RENDERER: {}", reinterpret_cast<const char*>(r_renderer.c_str()));
    gui::imdraw_ext::text_shadow(r_renderer_line, position, text_color, shadow_color, globals::font_unscii8, draw_list, font_size);
    position.y += 1.5f * y_step;

    const auto& head = globals::dimension->entities.get<entity::Head>(globals::player);
    const auto& transform = globals::dimension->entities.get<entity::Transform>(globals::player);
    const auto& velocity = globals::dimension->entities.get<entity::Velocity>(globals::player);

    // Draw player voxel position
    auto voxel_position = coord::to_voxel(transform.chunk, transform.local);
    auto voxel_line = std::format("voxel: [{} {} {}]", voxel_position.x, voxel_position.y, voxel_position.z);
    gui::imdraw_ext::text_shadow(voxel_line, position, text_color, shadow_color, globals::font_unscii8, draw_list, font_size);
    position.y += y_step;

    // Draw player world position
    auto world_line = std::format("world: [{} {} {}] [{:.03f} {:.03f} {:.03f}]", transform.chunk.x, transform.chunk.y, transform.chunk.z,
        transform.local.x, transform.local.y, transform.local.z);
    gui::imdraw_ext::text_shadow(world_line, position, text_color, shadow_color, globals::font_unscii8, draw_list, font_size);
    position.y += y_step;

    // Draw player look angles
    auto angles = glm::degrees(transform.angles + head.angles);
    auto angle_line = std::format("angle: [{: .03f} {: .03f} {: .03f}]", angles[0], angles[1], angles[2]);
    gui::imdraw_ext::text_shadow(angle_line, position, text_color, shadow_color, globals::font_unscii8, draw_list, font_size);
    position.y += y_step;
}