From 2ad9deff3e5d97771f223a3f25e66de99255ccd7 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sat, 7 Mar 2026 11:46:36 +0800 Subject: [PATCH] feat: add TextureManager class --- CMakeLists.txt | 1 + include/Cubed/texture_manager.hpp | 26 ++++++++++++++++ include/Cubed/tools/shader_tools.hpp | 1 + src/main.cpp | 9 ++---- src/texture_manager.cpp | 46 ++++++++++++++++++++++++++++ src/tools/shader_tools.cpp | 12 +++++--- 6 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 include/Cubed/texture_manager.hpp create mode 100644 src/texture_manager.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 74fca09..0e9e0d1 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/texture_manager.cpp src/tools/shader_tools.cpp src/tools/log.cpp ) diff --git a/include/Cubed/texture_manager.hpp b/include/Cubed/texture_manager.hpp new file mode 100644 index 0000000..bceb631 --- /dev/null +++ b/include/Cubed/texture_manager.hpp @@ -0,0 +1,26 @@ +#pragma once +#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; + +public: + TextureManager(); + ~TextureManager(); + const BlockTexture& get_block_texture(std::string name); + void delet_texture(); + + void load_block_texture(std::string block_name); +}; \ No newline at end of file diff --git a/include/Cubed/tools/shader_tools.hpp b/include/Cubed/tools/shader_tools.hpp index 004bb73..1cf693c 100644 --- a/include/Cubed/tools/shader_tools.hpp +++ b/include/Cubed/tools/shader_tools.hpp @@ -9,4 +9,5 @@ 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 diff --git a/src/main.cpp b/src/main.cpp index 24bc526..a6f755c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,7 @@ double delta_time = 0.0f; std::vector grass_block_texture(6); Player player; Camera camera; +TextureManager texture_manager; void setup_vertices(void) { float vertices_pos[108] = { // ===== front (z = +1) ===== @@ -162,12 +164,7 @@ void init(GLFWwindow* window) { glViewport(0, 0, width, height); p_mat = glm::perspective(glm::radians(60.0f), aspect, 0.1f, 1000.0f); - grass_block_texture[0] = load_texture("assets/texture/block/grass_block/front.png"); - grass_block_texture[1] = load_texture("assets/texture/block/grass_block/right.png"); - grass_block_texture[2] = load_texture("assets/texture/block/grass_block/back.png"); - grass_block_texture[3] = load_texture("assets/texture/block/grass_block/left.png"); - grass_block_texture[4] = load_texture("assets/texture/block/grass_block/top.png"); - grass_block_texture[5] = load_texture("assets/texture/block/grass_block/base.png"); + grass_block_texture = texture_manager.get_block_texture("grass_block").texture; for (int i = 0; i < 6; i++) { glBindTexture(GL_TEXTURE_2D, grass_block_texture[i]); diff --git a/src/texture_manager.cpp b/src/texture_manager.cpp new file mode 100644 index 0000000..75e52d5 --- /dev/null +++ b/src/texture_manager.cpp @@ -0,0 +1,46 @@ +#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(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)]; + +} + + +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); + } + } +} + +void TextureManager::load_block_texture(std::string block_name) { + BlockTexture block_texture; + 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; +} \ No newline at end of file diff --git a/src/tools/shader_tools.cpp b/src/tools/shader_tools.cpp index b8f760e..52fecba 100644 --- a/src/tools/shader_tools.cpp +++ b/src/tools/shader_tools.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -51,7 +52,7 @@ std::string read_shader_source(const char* file_path) { std::ifstream file_stream(file_path, std::ios::in); if (!file_stream.is_open()) { - LOG::error("file not exist"); + LOG::error("{} not exist", file_path); } std::string line = ""; @@ -64,11 +65,14 @@ std::string read_shader_source(const char* file_path) { return content; } +GLuint load_texture(const std::string& tex_image_path) { + return load_texture(tex_image_path.c_str()); +} + GLuint load_texture(const char* tex_image_path) { GLuint texture_id; texture_id = SOIL_load_OGL_texture(tex_image_path, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y); - if (texture_id == 0) { - LOG::error("could not find texture file"); - } + std::string error_info = std::string("Could not load texture") + tex_image_path; + CUBED_ASSERT_MSG(texture_id, error_info.c_str()); return texture_id; } \ No newline at end of file