mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
feat: add map table to save block index
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
constexpr int WORLD_SIZE_X = 32;
|
||||
constexpr int WORLD_SIZE_Z = 32;
|
||||
constexpr int WORLD_SIZE_Z = 32;
|
||||
constexpr int MAX_BLOCK_NUM = 1;
|
||||
14
include/Cubed/map_table.hpp
Normal file
14
include/Cubed/map_table.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
class MapTable {
|
||||
private:
|
||||
static std::unordered_map<unsigned, std::string> id_to_name_map;
|
||||
static std::unordered_map<size_t, unsigned> 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();
|
||||
|
||||
};
|
||||
@@ -1,26 +1,24 @@
|
||||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <Cubed/gameplay/block.hpp>
|
||||
#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;
|
||||
std::vector<BlockTexture> 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();
|
||||
};
|
||||
8
include/Cubed/tools/cubed_hash.hpp
Normal file
8
include/Cubed/tools/cubed_hash.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
namespace HASH {
|
||||
inline std::size_t str(std::string value) {
|
||||
return std::hash<std::string>{}(value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,10 +4,11 @@
|
||||
#include <SOIL2.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
25
src/main.cpp
25
src/main.cpp
@@ -11,6 +11,7 @@
|
||||
#include <Cubed/camera.hpp>
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/gameplay/player.hpp>
|
||||
#include <Cubed/map_table.hpp>
|
||||
#include <Cubed/texture_manager.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
@@ -30,7 +31,7 @@ float inc = 0.01f;
|
||||
float tf = 0.0f;
|
||||
double last_time = 0.0f;
|
||||
double delta_time = 0.0f;
|
||||
std::vector<GLuint> grass_block_texture(6);
|
||||
std::vector<GLuint> 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)) {
|
||||
|
||||
27
src/map_table.cpp
Normal file
27
src/map_table.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/map_table.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/cubed_hash.hpp>
|
||||
|
||||
std::unordered_map<unsigned, std::string> MapTable::id_to_name_map;
|
||||
std::unordered_map<size_t, unsigned> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,46 +1,66 @@
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/map_table.hpp>
|
||||
#include <Cubed/texture_manager.hpp>
|
||||
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.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(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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user