mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
feat: add TextureManager class
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
26
include/Cubed/texture_manager.hpp
Normal file
26
include/Cubed/texture_manager.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
struct BlockTexture {
|
||||
std::string name;
|
||||
std::vector<GLuint> texture;
|
||||
|
||||
};
|
||||
|
||||
class TextureManager {
|
||||
private:
|
||||
static std::size_t make_hash(std::string);
|
||||
std::unordered_map<std::size_t, BlockTexture> 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);
|
||||
};
|
||||
@@ -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);
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <Cubed/camera.hpp>
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/gameplay/player.hpp>
|
||||
#include <Cubed/texture_manager.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
@@ -32,6 +33,7 @@ double delta_time = 0.0f;
|
||||
std::vector<GLuint> 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]);
|
||||
|
||||
46
src/texture_manager.cpp
Normal file
46
src/texture_manager.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <Cubed/texture_manager.hpp>
|
||||
|
||||
TextureManager::TextureManager() {
|
||||
|
||||
}
|
||||
|
||||
TextureManager::~TextureManager() {
|
||||
delet_texture();
|
||||
}
|
||||
|
||||
std::size_t TextureManager::make_hash(std::string name) {
|
||||
std::size_t h1 = std::hash<std::string>{}(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;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <fstream>
|
||||
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user