diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f12f37..e8ba705 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/Cubed/debug_collector.hpp b/include/Cubed/debug_collector.hpp new file mode 100644 index 0000000..45d68d9 --- /dev/null +++ b/include/Cubed/debug_collector.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include + +class DebugCollector { +public: + static DebugCollector& get(); + DebugCollector(); + + std::unordered_map& 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 m_texts; +}; \ No newline at end of file diff --git a/include/Cubed/renderer.hpp b/include/Cubed/renderer.hpp index 6e82522..1c36173 100644 --- a/include/Cubed/renderer.hpp +++ b/include/Cubed/renderer.hpp @@ -47,10 +47,6 @@ private: std::vector m_vao; std::vector m_ui; - Text m_version_text; - Text m_fps_text; - Text m_player_pos_text; - void init_text(); void render_outline(); diff --git a/include/Cubed/tools/cubed_hash.hpp b/include/Cubed/tools/cubed_hash.hpp index 4f63064..d44c8d3 100644 --- a/include/Cubed/tools/cubed_hash.hpp +++ b/include/Cubed/tools/cubed_hash.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include namespace HASH { - inline std::size_t str(const std::string& value) { - return std::hash{}(value); + inline std::size_t str(std::string_view value) { + return std::hash{}(value); } } \ No newline at end of file diff --git a/include/Cubed/ui/text.hpp b/include/Cubed/ui/text.hpp index f340e61..e65a293 100644 --- a/include/Cubed/ui/text.hpp +++ b/include/Cubed/ui/text.hpp @@ -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; diff --git a/src/app.cpp b/src/app.cpp index ff9aef4..67a252e 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -153,6 +154,7 @@ void App::update() { if (fps_time_count >= 1.0f) { fps = static_cast(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; diff --git a/src/debug_collector.cpp b/src/debug_collector.cpp new file mode 100644 index 0000000..a2f6c13 --- /dev/null +++ b/src/debug_collector.cpp @@ -0,0 +1,51 @@ +#include + +#include + +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& 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); +} \ No newline at end of file diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 1bc5d0a..5a35583 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -1,4 +1,6 @@ #include + +#include #include #include #include @@ -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) { diff --git a/src/renderer.cpp b/src/renderer.cpp index f49024b..1d0f863 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -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(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); } diff --git a/src/ui/text.cpp b/src/ui/text.cpp index 9df9aaa..20287bc 100644 --- a/src/ui/text.cpp +++ b/src/ui/text.cpp @@ -1,15 +1,22 @@ #include #include +#include #include #include #include -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(); } \ No newline at end of file