mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
feat: add DebugCollector
This commit is contained in:
@@ -80,6 +80,7 @@ set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
|
|||||||
add_executable(${PROJECT_NAME}
|
add_executable(${PROJECT_NAME}
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/app.cpp
|
src/app.cpp
|
||||||
|
src/debug_collector.cpp
|
||||||
src/camera.cpp
|
src/camera.cpp
|
||||||
src/gameplay/chunk.cpp
|
src/gameplay/chunk.cpp
|
||||||
src/gameplay/player.cpp
|
src/gameplay/player.cpp
|
||||||
|
|||||||
20
include/Cubed/debug_collector.hpp
Normal file
20
include/Cubed/debug_collector.hpp
Normal 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;
|
||||||
|
};
|
||||||
@@ -47,10 +47,6 @@ private:
|
|||||||
std::vector<GLuint> m_vao;
|
std::vector<GLuint> m_vao;
|
||||||
std::vector<Vertex2D> m_ui;
|
std::vector<Vertex2D> m_ui;
|
||||||
|
|
||||||
Text m_version_text;
|
|
||||||
Text m_fps_text;
|
|
||||||
Text m_player_pos_text;
|
|
||||||
|
|
||||||
void init_text();
|
void init_text();
|
||||||
|
|
||||||
void render_outline();
|
void render_outline();
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string_view>
|
||||||
namespace HASH {
|
namespace HASH {
|
||||||
inline std::size_t str(const std::string& value) {
|
inline std::size_t str(std::string_view value) {
|
||||||
return std::hash<std::string>{}(value);
|
return std::hash<std::string_view>{}(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,25 +12,32 @@ class Shader;
|
|||||||
|
|
||||||
class Text {
|
class Text {
|
||||||
public:
|
public:
|
||||||
Text();
|
explicit Text(std::string_view name);
|
||||||
Text(std::string_view str, glm::vec2 pos = glm::vec2{0.0f, 0.0f}, Color color = Color::BLACK);
|
Text(std::string_view name, std::string_view str, glm::vec2 pos = glm::vec2{0.0f, 0.0f}, Color color = Color::BLACK);
|
||||||
~Text();
|
~Text();
|
||||||
|
Text(const Text&) = delete;
|
||||||
|
Text(Text&&) noexcept;
|
||||||
|
Text& operator=(const Text&) = delete;
|
||||||
|
Text& operator=(Text&&) noexcept = delete;
|
||||||
Text& color(Color color);
|
Text& color(Color color);
|
||||||
//Text& color(const glm::vec4& color, int pos);
|
//Text& color(const glm::vec4& color, int pos);
|
||||||
|
Text& name(std::string_view name);
|
||||||
Text& position(float x, float y);
|
Text& position(float x, float y);
|
||||||
Text& scale(float s);
|
Text& scale(float s);
|
||||||
static void set_loc(const Shader& shader);
|
|
||||||
Text& text(std::string_view str);
|
Text& text(std::string_view str);
|
||||||
|
|
||||||
|
std::size_t uuid() const;
|
||||||
|
static void set_loc(const Shader& shader);
|
||||||
void render();
|
void render();
|
||||||
|
|
||||||
|
bool operator==(const Text& other) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float m_scale = 1.0f;
|
float m_scale = 1.0f;
|
||||||
glm::vec2 m_pos{0.0f, 0.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;
|
std::string m_text;
|
||||||
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
|
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
|
||||||
glm::mat4 m_model_matrix;
|
glm::mat4 m_model_matrix;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <Cubed/app.hpp>
|
#include <Cubed/app.hpp>
|
||||||
|
#include <Cubed/debug_collector.hpp>
|
||||||
#include <Cubed/gameplay/player.hpp>
|
#include <Cubed/gameplay/player.hpp>
|
||||||
#include <Cubed/map_table.hpp>
|
#include <Cubed/map_table.hpp>
|
||||||
#include <Cubed/tools/cubed_assert.hpp>
|
#include <Cubed/tools/cubed_assert.hpp>
|
||||||
@@ -153,6 +154,7 @@ void App::update() {
|
|||||||
if (fps_time_count >= 1.0f) {
|
if (fps_time_count >= 1.0f) {
|
||||||
fps = static_cast<int>(frame_count / fps_time_count);
|
fps = static_cast<int>(frame_count / fps_time_count);
|
||||||
std::string title = "Cubed FPS: " + std::to_string(fps);
|
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());
|
glfwSetWindowTitle(m_window.get_glfw_window(), title.c_str());
|
||||||
frame_count = 0;
|
frame_count = 0;
|
||||||
fps_time_count = 0.0f;
|
fps_time_count = 0.0f;
|
||||||
|
|||||||
51
src/debug_collector.cpp
Normal file
51
src/debug_collector.cpp
Normal 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);
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <Cubed/gameplay/player.hpp>
|
#include <Cubed/gameplay/player.hpp>
|
||||||
|
|
||||||
|
#include <Cubed/debug_collector.hpp>
|
||||||
#include <Cubed/gameplay/world.hpp>
|
#include <Cubed/gameplay/world.hpp>
|
||||||
#include <Cubed/map_table.hpp>
|
#include <Cubed/map_table.hpp>
|
||||||
#include <Cubed/tools/cubed_assert.hpp>
|
#include <Cubed/tools/cubed_assert.hpp>
|
||||||
@@ -138,6 +140,10 @@ void Player::update(float delta_time) {
|
|||||||
update_move(delta_time);
|
update_move(delta_time);
|
||||||
update_lookup_block();
|
update_lookup_block();
|
||||||
check_player_chunk_transition();
|
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) {
|
void Player::update_player_move_state(int key, int action) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <Cubed/app.hpp>
|
#include <Cubed/app.hpp>
|
||||||
#include <Cubed/camera.hpp>
|
#include <Cubed/camera.hpp>
|
||||||
#include <Cubed/config.hpp>
|
#include <Cubed/config.hpp>
|
||||||
|
#include <Cubed/debug_collector.hpp>
|
||||||
#include <Cubed/gameplay/player.hpp>
|
#include <Cubed/gameplay/player.hpp>
|
||||||
#include <Cubed/gameplay/world.hpp>
|
#include <Cubed/gameplay/world.hpp>
|
||||||
#include <Cubed/texture_manager.hpp>
|
#include <Cubed/texture_manager.hpp>
|
||||||
@@ -117,18 +118,7 @@ const Shader& Renderer::get_shader(const std::string& name) const {
|
|||||||
void Renderer::init_text() {
|
void Renderer::init_text() {
|
||||||
const auto& shader = get_shader("text");
|
const auto& shader = get_shader("text");
|
||||||
Text::set_loc(shader);
|
Text::set_loc(shader);
|
||||||
m_version_text
|
DebugCollector::get().init_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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::render() {
|
void Renderer::render() {
|
||||||
@@ -211,16 +201,12 @@ void Renderer::render_text() {
|
|||||||
m_proj_loc = shader.loc("projection");
|
m_proj_loc = shader.loc("projection");
|
||||||
|
|
||||||
glUniformMatrix4fv(m_proj_loc, 1, GL_FALSE, glm::value_ptr(m_ui_proj));
|
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();
|
auto& texts = DebugCollector::get().all_texts();
|
||||||
m_player_pos_text.render();
|
for (auto& t : texts) {
|
||||||
m_version_text.render();
|
t.second.render();
|
||||||
|
}
|
||||||
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
#include <Cubed/ui/text.hpp>
|
#include <Cubed/ui/text.hpp>
|
||||||
|
|
||||||
#include <Cubed/shader.hpp>
|
#include <Cubed/shader.hpp>
|
||||||
|
#include <Cubed/tools/cubed_hash.hpp>
|
||||||
#include <Cubed/tools/font.hpp>
|
#include <Cubed/tools/font.hpp>
|
||||||
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/type_ptr.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_text.assign(str);
|
||||||
m_pos = pos;
|
m_pos = pos;
|
||||||
m_color = color_value(color);
|
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) {
|
Text& Text::color(Color color) {
|
||||||
m_color = color_value(color);
|
m_color = color_value(color);
|
||||||
return *this;
|
return *this;
|
||||||
@@ -37,6 +58,10 @@ Text& Text::scale(float s) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::size_t Text::uuid() const {
|
||||||
|
return UUID;
|
||||||
|
}
|
||||||
|
|
||||||
void Text::set_loc(const Shader& shader) {
|
void Text::set_loc(const Shader& shader) {
|
||||||
m_color_loc = shader.loc("textColor");
|
m_color_loc = shader.loc("textColor");
|
||||||
m_mv_loc = shader.loc("mv_matrix");
|
m_mv_loc = shader.loc("mv_matrix");
|
||||||
@@ -49,7 +74,7 @@ Text& Text::text(std::string_view str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Text::render() {
|
void Text::render() {
|
||||||
|
CUBED_ASSERT_MSG(m_vbo != 0,"VBO not initialized!");
|
||||||
CUBED_ASSERT_MSG(!m_vertices.empty(), "Text String Not Set");
|
CUBED_ASSERT_MSG(!m_vertices.empty(), "Text String Not Set");
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D_ARRAY, Font::text_texture());
|
glBindTexture(GL_TEXTURE_2D_ARRAY, Font::text_texture());
|
||||||
@@ -87,4 +112,8 @@ void Text::upload_to_gpu() {
|
|||||||
CUBED_ASSERT_MSG(m_vbo, "Vbo Is Not Gen");
|
CUBED_ASSERT_MSG(m_vbo, "Vbo Is Not Gen");
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||||
glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(Vertex2D), m_vertices.data(), GL_DYNAMIC_DRAW);
|
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();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user