From eccd744f8bdbad015a90a038d6a348a9320b685e Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sat, 7 Mar 2026 14:44:08 +0800 Subject: [PATCH] feat: add map table to save block index --- CMakeLists.txt | 1 + include/Cubed/config.hpp | 3 +- include/Cubed/map_table.hpp | 14 ++++++ include/Cubed/texture_manager.hpp | 18 ++++--- include/Cubed/tools/cubed_hash.hpp | 8 ++++ include/Cubed/tools/shader_tools.hpp | 15 +++--- src/main.cpp | 25 +++++----- src/map_table.cpp | 27 +++++++++++ src/texture_manager.cpp | 72 ++++++++++++++++++---------- 9 files changed, 128 insertions(+), 55 deletions(-) create mode 100644 include/Cubed/map_table.hpp create mode 100644 include/Cubed/tools/cubed_hash.hpp create mode 100644 src/map_table.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e9e0d1..a36bebc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ add_executable(${PROJECT_NAME} src/main.cpp src/camera.cpp src/gameplay/player.cpp + src/map_table.cpp src/texture_manager.cpp src/tools/shader_tools.cpp src/tools/log.cpp diff --git a/include/Cubed/config.hpp b/include/Cubed/config.hpp index 70e4efc..578a580 100644 --- a/include/Cubed/config.hpp +++ b/include/Cubed/config.hpp @@ -1,3 +1,4 @@ #pragma once constexpr int WORLD_SIZE_X = 32; -constexpr int WORLD_SIZE_Z = 32; \ No newline at end of file +constexpr int WORLD_SIZE_Z = 32; +constexpr int MAX_BLOCK_NUM = 1; \ No newline at end of file diff --git a/include/Cubed/map_table.hpp b/include/Cubed/map_table.hpp new file mode 100644 index 0000000..439d8a5 --- /dev/null +++ b/include/Cubed/map_table.hpp @@ -0,0 +1,14 @@ +#pragma once +#include +#include +class MapTable { +private: + static std::unordered_map id_to_name_map; + static std::unordered_map name_to_id_map; +public: + // please using reference + static const std::string& get_name_from_id(unsigned id); + static const unsigned get_id_from_name(const std::string& name); + static void init_map(); + +}; diff --git a/include/Cubed/texture_manager.hpp b/include/Cubed/texture_manager.hpp index bceb631..0a0adca 100644 --- a/include/Cubed/texture_manager.hpp +++ b/include/Cubed/texture_manager.hpp @@ -1,26 +1,24 @@ #pragma once #include #include -#include #include - +#include #include -struct BlockTexture { - std::string name; - std::vector texture; -}; class TextureManager { private: - static std::size_t make_hash(std::string); - std::unordered_map m_block_textures; + std::vector m_block_textures; public: TextureManager(); ~TextureManager(); - const BlockTexture& get_block_texture(std::string name); + const BlockTexture& get_block_texture(const std::string& block_name); + const BlockTexture& get_block_texture(unsigned block_id); void delet_texture(); - void load_block_texture(std::string block_name); + void load_block_texture(const std::string& block_name); + void load_block_texture(unsigned block_id); + // Must call after MapTable::init_map() and glfwMakeContextCurrent(window); + void init_texture(); }; \ No newline at end of file diff --git a/include/Cubed/tools/cubed_hash.hpp b/include/Cubed/tools/cubed_hash.hpp new file mode 100644 index 0000000..2ecda98 --- /dev/null +++ b/include/Cubed/tools/cubed_hash.hpp @@ -0,0 +1,8 @@ +#pragma once +#include +namespace HASH { + inline std::size_t str(std::string value) { + return std::hash{}(value); + } + +} \ No newline at end of file diff --git a/include/Cubed/tools/shader_tools.hpp b/include/Cubed/tools/shader_tools.hpp index 1cf693c..3db813b 100644 --- a/include/Cubed/tools/shader_tools.hpp +++ b/include/Cubed/tools/shader_tools.hpp @@ -4,10 +4,11 @@ #include #include - -void print_shader_log(GLuint shader); -void print_program_info(int prog); -bool check_opengl_error(); -std::string read_shader_source(const char* file_path); -GLuint load_texture(const std::string& tex_image_path); -GLuint load_texture(const char* tex_image_path); \ No newline at end of file +namespace Shader { + void print_shader_log(GLuint shader); + void print_program_info(int prog); + bool check_opengl_error(); + std::string read_shader_source(const char* file_path); + GLuint load_texture(const std::string& tex_image_path); + GLuint load_texture(const char* tex_image_path); +} diff --git a/src/main.cpp b/src/main.cpp index a6f755c..50a2d3c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -30,7 +31,7 @@ float inc = 0.01f; float tf = 0.0f; double last_time = 0.0f; double delta_time = 0.0f; -std::vector grass_block_texture(6); +std::vector grass_block_texture; Player player; Camera camera; TextureManager texture_manager; @@ -110,8 +111,8 @@ void setup_vertices(void) { GLuint create_shader_program() { - std::string v_shader_str = read_shader_source("shaders/vShader.glsl"); - std::string f_shader_str = read_shader_source("shaders/fShader.glsl"); + std::string v_shader_str = Shader::read_shader_source("shaders/vShader.glsl"); + std::string f_shader_str = Shader::read_shader_source("shaders/fShader.glsl"); const char *v_shader_source = v_shader_str.c_str(); const char *f_shader_source = f_shader_str.c_str(); @@ -122,18 +123,18 @@ GLuint create_shader_program() { glShaderSource(v_shader, 1, &v_shader_source, NULL); glShaderSource(f_shader, 1, &f_shader_source, NULL); glCompileShader(v_shader); - check_opengl_error(); + Shader::check_opengl_error(); glGetShaderiv(v_shader, GL_COMPILE_STATUS, &vc); if (vc != 1) { LOG::error("vertex compilation failed"); - print_shader_log(v_shader); + Shader::print_shader_log(v_shader); } glCompileShader(f_shader); - check_opengl_error(); + Shader::check_opengl_error(); glGetShaderiv(f_shader, GL_COMPILE_STATUS, &fc); if (fc != 1) { LOG::error("vertex compilation failed"); - print_shader_log(f_shader); + Shader::print_shader_log(f_shader); } GLuint vf_program = glCreateProgram(); glAttachShader(vf_program, v_shader); @@ -141,11 +142,11 @@ GLuint create_shader_program() { glLinkProgram(vf_program); GLint linked; - check_opengl_error(); + Shader::check_opengl_error(); glGetProgramiv(vf_program, GL_LINK_STATUS, &linked); if (linked != 1) { LOG::error("linking failed"); - print_program_info(vf_program); + Shader::print_program_info(vf_program); } return vf_program; @@ -163,7 +164,7 @@ void init(GLFWwindow* window) { aspect = (float)width / (float)height; glViewport(0, 0, width, height); p_mat = glm::perspective(glm::radians(60.0f), aspect, 0.1f, 1000.0f); - + // Must call after texture_manager.init_texture(); grass_block_texture = texture_manager.get_block_texture("grass_block").texture; for (int i = 0; i < 6; i++) { @@ -311,8 +312,10 @@ int main() { glfwSetWindowSizeCallback(window, window_reshape_callback); glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, cursor_position_callback); + MapTable::init_map(); + texture_manager.init_texture(); init(window); - + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); while(!glfwWindowShouldClose(window)) { diff --git a/src/map_table.cpp b/src/map_table.cpp new file mode 100644 index 0000000..cef7fa6 --- /dev/null +++ b/src/map_table.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +std::unordered_map MapTable::id_to_name_map; +std::unordered_map MapTable::name_to_id_map; + + +const std::string& MapTable::get_name_from_id(unsigned id) { + auto it = id_to_name_map.find(id); + CUBED_ASSERT_MSG(it != id_to_name_map.end(), "Id: " + std::to_string(id) + " is not exist"); + return it->second; +} +const unsigned MapTable::get_id_from_name(const std::string& name) { + auto it = name_to_id_map.find(HASH::str(name)); + CUBED_ASSERT_MSG(it != name_to_id_map.end(), "Name " + name + " is not exist"); + return it->second; +} +void MapTable::init_map() { + id_to_name_map.reserve(MAX_BLOCK_NUM); + name_to_id_map.reserve(MAX_BLOCK_NUM); + id_to_name_map[0] = "grass_block"; + name_to_id_map[HASH::str("grass_block")] = 0; +} + + diff --git a/src/texture_manager.cpp b/src/texture_manager.cpp index 75e52d5..267db11 100644 --- a/src/texture_manager.cpp +++ b/src/texture_manager.cpp @@ -1,46 +1,66 @@ +#include +#include #include - +#include +#include TextureManager::TextureManager() { - + } TextureManager::~TextureManager() { delet_texture(); } -std::size_t TextureManager::make_hash(std::string name) { - std::size_t h1 = std::hash{}(name); - return h1; +const BlockTexture& TextureManager::get_block_texture(const std::string& name) { + + + load_block_texture(name); + return m_block_textures[MapTable::get_id_from_name(name)]; + } -const BlockTexture& TextureManager::get_block_texture(std::string name) { - auto it = m_block_textures.find(make_hash(name)); - if (it != m_block_textures.end()) { - return it->second; - } - load_block_texture(name); - return m_block_textures[make_hash(name)]; +const BlockTexture& TextureManager::get_block_texture(unsigned id) { + + + load_block_texture(id); + return m_block_textures[id]; } void TextureManager::delet_texture() { - for (const auto& texture : m_block_textures) { - auto [key, block_texture] = texture; - for (const GLuint& texture_id : block_texture.texture) { - glDeleteTextures(1, &texture_id); - } + for (const auto& block_texture : m_block_textures) { + for (const auto& id : block_texture.texture) { + glDeleteTextures(1, &id); + } } + LOG::info("Successfully delete all texture"); } -void TextureManager::load_block_texture(std::string block_name) { - BlockTexture block_texture; +void TextureManager::load_block_texture(const std::string& block_name) { + + auto id = MapTable::get_id_from_name(block_name); + std::string block_texture_path = "assets/texture/block/" + block_name; - block_texture.texture.emplace_back(load_texture(block_texture_path + "/front.png")); - block_texture.texture.emplace_back(load_texture(block_texture_path + "/right.png")); - block_texture.texture.emplace_back(load_texture(block_texture_path + "/back.png")); - block_texture.texture.emplace_back(load_texture(block_texture_path + "/left.png")); - block_texture.texture.emplace_back(load_texture(block_texture_path + "/top.png")); - block_texture.texture.emplace_back(load_texture(block_texture_path + "/base.png")); - m_block_textures[make_hash(block_name)] = block_texture; + m_block_textures[id].texture.emplace_back(Shader::load_texture(block_texture_path + "/front.png")); + m_block_textures[id].texture.emplace_back(Shader::load_texture(block_texture_path + "/right.png")); + m_block_textures[id].texture.emplace_back(Shader::load_texture(block_texture_path + "/back.png")); + m_block_textures[id].texture.emplace_back(Shader::load_texture(block_texture_path + "/left.png")); + m_block_textures[id].texture.emplace_back(Shader::load_texture(block_texture_path + "/top.png")); + m_block_textures[id].texture.emplace_back(Shader::load_texture(block_texture_path + "/base.png")); +} + +void TextureManager::load_block_texture(unsigned block_id) { + CUBED_ASSERT_MSG(block_id < MAX_BLOCK_NUM, "Exceed the max block sum limit"); + load_block_texture(MapTable::get_name_from_id(block_id)); +} + +void TextureManager::init_texture() { + MapTable::init_map(); + m_block_textures.resize(MAX_BLOCK_NUM); + + for (int i = 0; i < MAX_BLOCK_NUM; i++) { + load_block_texture(i); + } + } \ No newline at end of file