feat: add DebugCollector

This commit is contained in:
2026-04-16 11:55:25 +08:00
parent 311c675852
commit 1d04fedb0f
10 changed files with 136 additions and 38 deletions

View File

@@ -80,6 +80,7 @@ set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
add_executable(${PROJECT_NAME}
src/main.cpp
src/app.cpp
src/debug_collector.cpp
src/camera.cpp
src/gameplay/chunk.cpp
src/gameplay/player.cpp

View File

@@ -0,0 +1,20 @@
#pragma once
#include <Cubed/ui/text.hpp>
#include <unordered_map>
class DebugCollector {
public:
static DebugCollector& get();
DebugCollector();
std::unordered_map<std::size_t, Text>& all_texts();
Text& text(std::string_view name);
void report(std::string_view name, std::string_view content);
void init_text();
private:
std::unordered_map<std::size_t, Text> m_texts;
};

View File

@@ -47,10 +47,6 @@ private:
std::vector<GLuint> m_vao;
std::vector<Vertex2D> m_ui;
Text m_version_text;
Text m_fps_text;
Text m_player_pos_text;
void init_text();
void render_outline();

View File

@@ -1,8 +1,8 @@
#pragma once
#include <string>
#include <string_view>
namespace HASH {
inline std::size_t str(const std::string& value) {
return std::hash<std::string>{}(value);
inline std::size_t str(std::string_view value) {
return std::hash<std::string_view>{}(value);
}
}

View File

@@ -12,25 +12,32 @@ class Shader;
class Text {
public:
Text();
Text(std::string_view str, glm::vec2 pos = glm::vec2{0.0f, 0.0f}, Color color = Color::BLACK);
explicit Text(std::string_view name);
Text(std::string_view name, std::string_view str, glm::vec2 pos = glm::vec2{0.0f, 0.0f}, Color color = Color::BLACK);
~Text();
Text(const Text&) = delete;
Text(Text&&) noexcept;
Text& operator=(const Text&) = delete;
Text& operator=(Text&&) noexcept = delete;
Text& color(Color color);
//Text& color(const glm::vec4& color, int pos);
Text& name(std::string_view name);
Text& position(float x, float y);
Text& scale(float s);
static void set_loc(const Shader& shader);
Text& text(std::string_view str);
std::size_t uuid() const;
static void set_loc(const Shader& shader);
void render();
bool operator==(const Text& other) const;
private:
float m_scale = 1.0f;
glm::vec2 m_pos{0.0f, 0.0f};
std::string m_name;
const std::string NAME;
const std::size_t UUID;
std::string m_text;
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
glm::mat4 m_model_matrix;

View File

@@ -1,4 +1,5 @@
#include <Cubed/app.hpp>
#include <Cubed/debug_collector.hpp>
#include <Cubed/gameplay/player.hpp>
#include <Cubed/map_table.hpp>
#include <Cubed/tools/cubed_assert.hpp>
@@ -153,6 +154,7 @@ void App::update() {
if (fps_time_count >= 1.0f) {
fps = static_cast<int>(frame_count / fps_time_count);
std::string title = "Cubed FPS: " + std::to_string(fps);
DebugCollector::get().report("fps", std::string{"FPS: " + std::to_string(fps)});
glfwSetWindowTitle(m_window.get_glfw_window(), title.c_str());
frame_count = 0;
fps_time_count = 0.0f;

51
src/debug_collector.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include <Cubed/debug_collector.hpp>
#include <Cubed/tools/cubed_hash.hpp>
DebugCollector::DebugCollector() {
}
DebugCollector& DebugCollector::get() {
static DebugCollector instance;
return instance;
}
void DebugCollector::init_text() {
Text version_text("version");
Text fps_text("fps");
Text player_pos_text("player_pos");
version_text
.position(0.0f, 100.0f)
.scale(0.8f)
.color(Color::WHITE)
.text("Version: v0.0.1-Debug");
fps_text
.position(0.0f, 50.0f)
.text("FPS: 0");
player_pos_text
.position(0.0f, 150.0f)
.scale(0.8f)
.text("x: 0.00 y: 0.00 z: 0.00");
m_texts.insert({version_text.uuid(), std::move(version_text)});
m_texts.insert({fps_text.uuid(), std::move(fps_text)});
m_texts.insert({player_pos_text.uuid(), std::move(player_pos_text)});
}
std::unordered_map<std::size_t, Text>& DebugCollector::all_texts() {
return m_texts;
}
Text& DebugCollector::text(std::string_view name) {
std::size_t id = HASH::str(name);
auto it = m_texts.find(id);
CUBED_ASSERT_MSG(it != m_texts.end(), "Can't Find Text");
return it->second;
}
void DebugCollector::report(std::string_view name, std::string_view content) {
auto& t = text(name);
t.text(content);
}

View File

@@ -1,4 +1,6 @@
#include <Cubed/gameplay/player.hpp>
#include <Cubed/debug_collector.hpp>
#include <Cubed/gameplay/world.hpp>
#include <Cubed/map_table.hpp>
#include <Cubed/tools/cubed_assert.hpp>
@@ -138,6 +140,10 @@ void Player::update(float delta_time) {
update_move(delta_time);
update_lookup_block();
check_player_chunk_transition();
std::string player_pos = std::format("x: {:.2f} y: {:.2f} z: {:.2f}", m_player_pos.x, m_player_pos.y, m_player_pos.z);
DebugCollector::get().report("player_pos", player_pos);
}
void Player::update_player_move_state(int key, int action) {

View File

@@ -1,6 +1,7 @@
#include <Cubed/app.hpp>
#include <Cubed/camera.hpp>
#include <Cubed/config.hpp>
#include <Cubed/debug_collector.hpp>
#include <Cubed/gameplay/player.hpp>
#include <Cubed/gameplay/world.hpp>
#include <Cubed/texture_manager.hpp>
@@ -117,18 +118,7 @@ const Shader& Renderer::get_shader(const std::string& name) const {
void Renderer::init_text() {
const auto& shader = get_shader("text");
Text::set_loc(shader);
m_version_text
.position(0.0f, 100.0f)
.scale(0.8f)
.color(Color::WHITE)
.text("Version: v0.0.1-Debug");
m_fps_text
.position(0.0f, 50.0f)
.text("FPS: 0");
m_player_pos_text
.position(0.0f, 150.0f)
.scale(0.8f)
.text("x: 0.00 y: 0.00 z: 0.00");
DebugCollector::get().init_text();
}
void Renderer::render() {
@@ -211,15 +201,11 @@ void Renderer::render_text() {
m_proj_loc = shader.loc("projection");
glUniformMatrix4fv(m_proj_loc, 1, GL_FALSE, glm::value_ptr(m_ui_proj));
m_fps_text.text(std::string{"FPS: " + std::to_string(static_cast<int>(App::get_fps()))});
const auto& player = m_world.get_player("TestPlayer");
const auto& pos = player.get_player_pos();
std::string player_pos = std::format("x: {:.2f} y: {:.2f} z: {:.2f}", pos.x, pos.y, pos.z);
m_player_pos_text.text(player_pos);
m_fps_text.render();
m_player_pos_text.render();
m_version_text.render();
auto& texts = DebugCollector::get().all_texts();
for (auto& t : texts) {
t.second.render();
}
glEnable(GL_DEPTH_TEST);
}

View File

@@ -1,15 +1,22 @@
#include <Cubed/ui/text.hpp>
#include <Cubed/shader.hpp>
#include <Cubed/tools/cubed_hash.hpp>
#include <Cubed/tools/font.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
Text::Text() {
Text::Text(std::string_view name) :
NAME(name),
UUID(HASH::str(name))
{
}
Text::Text(std::string_view str, glm::vec2 pos, Color color) {
Text::Text(std::string_view name, std::string_view str, glm::vec2 pos, Color color) :
NAME(name),
UUID(HASH::str(name))
{
m_text.assign(str);
m_pos = pos;
m_color = color_value(color);
@@ -22,6 +29,20 @@ Text::~Text() {
}
}
Text::Text(Text&& other) noexcept :
UUID(other.UUID),
NAME(other.NAME),
m_scale(other.m_scale),
m_vbo(other.m_vbo),
m_color(other.m_color),
m_model_matrix(other.m_model_matrix),
m_pos(other.m_pos),
m_text(std::move(other.m_text)),
m_vertices(std::move(other.m_vertices))
{
other.m_vbo = 0;
}
Text& Text::color(Color color) {
m_color = color_value(color);
return *this;
@@ -37,6 +58,10 @@ Text& Text::scale(float s) {
return *this;
}
std::size_t Text::uuid() const {
return UUID;
}
void Text::set_loc(const Shader& shader) {
m_color_loc = shader.loc("textColor");
m_mv_loc = shader.loc("mv_matrix");
@@ -49,7 +74,7 @@ Text& Text::text(std::string_view str) {
}
void Text::render() {
CUBED_ASSERT_MSG(m_vbo != 0,"VBO not initialized!");
CUBED_ASSERT_MSG(!m_vertices.empty(), "Text String Not Set");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, Font::text_texture());
@@ -88,3 +113,7 @@ void Text::upload_to_gpu() {
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(Vertex2D), m_vertices.data(), GL_DYNAMIC_DRAW);
}
bool Text::operator==(const Text& other) const {
return UUID == other.uuid();
}