mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-17 16:17:02 +08:00
feat: add DebugCollector
This commit is contained in:
@@ -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
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/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) {
|
||||
|
||||
@@ -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,16 +201,12 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
@@ -87,4 +112,8 @@ void Text::upload_to_gpu() {
|
||||
CUBED_ASSERT_MSG(m_vbo, "Vbo Is Not Gen");
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user