feat: add texture hot-reload

This commit is contained in:
2026-04-16 15:33:58 +08:00
parent cf9aaa62a7
commit 7d5b9c34b3
8 changed files with 59 additions and 9 deletions

View File

@@ -29,9 +29,6 @@ private:
Window m_window{m_renderer}; Window m_window{m_renderer};
GLuint m_texture_array = 0;
inline static double last_time = glfwGetTime(); inline static double last_time = glfwGetTime();
inline static double current_time = glfwGetTime(); inline static double current_time = glfwGetTime();
inline static double delta_time = 0.0f; inline static double delta_time = 0.0f;

View File

@@ -16,9 +16,14 @@ struct MouseState {
bool right = false; bool right = false;
}; };
struct KeyState {
bool r = false;
};
struct InputState { struct InputState {
MoveState move_state; MoveState move_state;
MouseState mouse_state; MouseState mouse_state;
KeyState key_state;
}; };
namespace Input { namespace Input {

View File

@@ -6,12 +6,14 @@
class TextureManager { class TextureManager {
private: private:
bool m_need_reload = false;
GLuint m_block_status_array; GLuint m_block_status_array;
GLuint m_texture_array; GLuint m_texture_array;
GLuint m_ui_array; GLuint m_ui_array;
void load_block_status(unsigned status_id); void load_block_status(unsigned status_id);
void load_block_texture(unsigned block_id); void load_block_texture(unsigned block_id);
void load_ui_texture(unsigned id); void load_ui_texture(unsigned id);
public: public:
TextureManager(); TextureManager();
~TextureManager(); ~TextureManager();
@@ -22,4 +24,8 @@ public:
GLuint get_ui_array() const; GLuint get_ui_array() const;
// Must call after MapTable::init_map() and glfwMakeContextCurrent(window); // Must call after MapTable::init_map() and glfwMakeContextCurrent(window);
void init_texture(); void init_texture();
void hot_reload();
void need_reload();
void update();
}; };

View File

@@ -6,12 +6,16 @@ public:
Window(Renderer& renderer); Window(Renderer& renderer);
~Window(); ~Window();
bool is_mouse_enable() const;
const GLFWwindow* get_glfw_window() const; const GLFWwindow* get_glfw_window() const;
GLFWwindow* get_glfw_window(); GLFWwindow* get_glfw_window();
void init(); void init();
void update_viewport(); void update_viewport();
void toggle_fullscreen(); void toggle_fullscreen();
void toggle_mouse_able();
private: private:
bool m_mouse_enable = false;
float m_aspect; float m_aspect;
GLFWwindow* m_window; GLFWwindow* m_window;
int m_width; int m_width;

View File

@@ -19,8 +19,11 @@ App::~App() {
void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos) { void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window)); App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr"); CUBED_ASSERT_MSG(app, "nullptr");
if (!app->m_window.is_mouse_enable()) {
app->m_camera.update_cursor_position_camera(xpos, ypos); app->m_camera.update_cursor_position_camera(xpos, ypos);
} }
}
void App::init() { void App::init() {
m_window.init(); m_window.init();
Logger::info("Window Init Success"); Logger::info("Window Init Success");
@@ -45,7 +48,6 @@ void App::init() {
Logger::info("Texture Load Success"); Logger::info("Texture Load Success");
m_world.init_world(); m_world.init_world();
Logger::info("World Init Success"); Logger::info("World Init Success");
m_texture_array = m_texture_manager.get_texture_array();
m_camera.camera_init(&m_world.get_player("TestPlayer")); m_camera.camera_init(&m_world.get_player("TestPlayer"));
@@ -54,7 +56,7 @@ void App::init() {
void App::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { void App::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window)); App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr"); CUBED_ASSERT_MSG(app, "nullptr");
auto& input = Input::get_input_state();
switch(key) { switch(key) {
case GLFW_KEY_Q: case GLFW_KEY_Q:
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
@@ -78,7 +80,12 @@ void App::key_callback(GLFWwindow* window, int key, int scancode, int action, in
break; break;
case GLFW_KEY_R: case GLFW_KEY_R:
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
app->m_world.need_gen(); app->m_texture_manager.need_reload();
}
break;
case GLFW_KEY_P:
if (action == GLFW_PRESS) {
app->m_window.toggle_mouse_able();
} }
break; break;
@@ -159,6 +166,7 @@ void App::update() {
frame_count = 0; frame_count = 0;
fps_time_count = 0.0f; fps_time_count = 0.0f;
} }
m_texture_manager.update();
m_world.update(delta_time); m_world.update(delta_time);
m_camera.update_move_camera(); m_camera.update_move_camera();
const auto& player= m_world.get_player("TestPlayer"); const auto& player= m_world.get_player("TestPlayer");

View File

@@ -179,3 +179,19 @@ void TextureManager::init_texture() {
} }
void TextureManager::update() {
if (m_need_reload) {
hot_reload();
}
}
void TextureManager::need_reload() {
m_need_reload = true;
}
void TextureManager::hot_reload() {
delet_texture();
init_texture();
m_need_reload = false;
}

View File

@@ -19,6 +19,10 @@ Window::~Window() {
glfwTerminate(); glfwTerminate();
} }
bool Window::is_mouse_enable() const {
return m_mouse_enable;
}
const GLFWwindow* Window::get_glfw_window() const { const GLFWwindow* Window::get_glfw_window() const {
return m_window; return m_window;
} }
@@ -98,3 +102,13 @@ void Window::toggle_fullscreen() {
} }
update_viewport(); update_viewport();
} }
void Window::toggle_mouse_able() {
if (m_mouse_enable) {
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
m_mouse_enable = false;
} else {
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
m_mouse_enable = true;
}
}