From 7d5b9c34b3640463a46e1877a3511207ed7a72fb Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Thu, 16 Apr 2026 15:33:58 +0800 Subject: [PATCH] feat: add texture hot-reload --- include/Cubed/app.hpp | 3 --- include/Cubed/input.hpp | 5 +++++ include/Cubed/texture_manager.hpp | 8 +++++++- include/Cubed/window.hpp | 4 ++++ src/app.cpp | 16 ++++++++++++---- src/map_table.cpp | 2 +- src/texture_manager.cpp | 16 ++++++++++++++++ src/window.cpp | 14 ++++++++++++++ 8 files changed, 59 insertions(+), 9 deletions(-) diff --git a/include/Cubed/app.hpp b/include/Cubed/app.hpp index adf70f1..0dae470 100644 --- a/include/Cubed/app.hpp +++ b/include/Cubed/app.hpp @@ -29,9 +29,6 @@ private: Window m_window{m_renderer}; - - GLuint m_texture_array = 0; - inline static double last_time = glfwGetTime(); inline static double current_time = glfwGetTime(); inline static double delta_time = 0.0f; diff --git a/include/Cubed/input.hpp b/include/Cubed/input.hpp index 7f42889..f2a2705 100644 --- a/include/Cubed/input.hpp +++ b/include/Cubed/input.hpp @@ -16,9 +16,14 @@ struct MouseState { bool right = false; }; +struct KeyState { + bool r = false; +}; + struct InputState { MoveState move_state; MouseState mouse_state; + KeyState key_state; }; namespace Input { diff --git a/include/Cubed/texture_manager.hpp b/include/Cubed/texture_manager.hpp index a8c5b69..14d1f51 100644 --- a/include/Cubed/texture_manager.hpp +++ b/include/Cubed/texture_manager.hpp @@ -6,20 +6,26 @@ class TextureManager { private: + bool m_need_reload = false; GLuint m_block_status_array; GLuint m_texture_array; GLuint m_ui_array; void load_block_status(unsigned status_id); void load_block_texture(unsigned block_id); void load_ui_texture(unsigned id); + public: TextureManager(); ~TextureManager(); - + void delet_texture(); GLuint get_block_status_array() const; GLuint get_texture_array() const; GLuint get_ui_array() const; // Must call after MapTable::init_map() and glfwMakeContextCurrent(window); void init_texture(); + void hot_reload(); + void need_reload(); + void update(); + }; \ No newline at end of file diff --git a/include/Cubed/window.hpp b/include/Cubed/window.hpp index ea05211..bae4ee4 100644 --- a/include/Cubed/window.hpp +++ b/include/Cubed/window.hpp @@ -6,12 +6,16 @@ public: Window(Renderer& renderer); ~Window(); + bool is_mouse_enable() const; const GLFWwindow* get_glfw_window() const; GLFWwindow* get_glfw_window(); void init(); void update_viewport(); void toggle_fullscreen(); + void toggle_mouse_able(); + private: + bool m_mouse_enable = false; float m_aspect; GLFWwindow* m_window; int m_width; diff --git a/src/app.cpp b/src/app.cpp index 67a252e..fb17fe1 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -19,7 +19,10 @@ App::~App() { void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos) { App* app = static_cast(glfwGetWindowUserPointer(window)); CUBED_ASSERT_MSG(app, "nullptr"); - app->m_camera.update_cursor_position_camera(xpos, ypos); + if (!app->m_window.is_mouse_enable()) { + app->m_camera.update_cursor_position_camera(xpos, ypos); + } + } void App::init() { m_window.init(); @@ -45,7 +48,6 @@ void App::init() { Logger::info("Texture Load Success"); m_world.init_world(); Logger::info("World Init Success"); - m_texture_array = m_texture_manager.get_texture_array(); 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) { App* app = static_cast(glfwGetWindowUserPointer(window)); CUBED_ASSERT_MSG(app, "nullptr"); - + auto& input = Input::get_input_state(); switch(key) { case GLFW_KEY_Q: if (action == GLFW_PRESS) { @@ -78,7 +80,12 @@ void App::key_callback(GLFWwindow* window, int key, int scancode, int action, in break; case GLFW_KEY_R: 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; @@ -159,6 +166,7 @@ void App::update() { frame_count = 0; fps_time_count = 0.0f; } + m_texture_manager.update(); m_world.update(delta_time); m_camera.update_move_camera(); const auto& player= m_world.get_player("TestPlayer"); diff --git a/src/map_table.cpp b/src/map_table.cpp index 66259aa..68c0a3e 100644 --- a/src/map_table.cpp +++ b/src/map_table.cpp @@ -31,7 +31,7 @@ void MapTable::init_map() { for (int i = 0; i < MAX_BLOCK_NUM; i++) { id_to_name_map[i] = BLOCK_REISTER[i]; - name_to_id_map[HASH::str(BLOCK_REISTER[i])] = i; + name_to_id_map[HASH::str(BLOCK_REISTER[i])] = i; } } diff --git a/src/texture_manager.cpp b/src/texture_manager.cpp index e12c3b7..2c1a19b 100644 --- a/src/texture_manager.cpp +++ b/src/texture_manager.cpp @@ -178,4 +178,20 @@ void TextureManager::init_texture() { glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST); +} + +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; } \ No newline at end of file diff --git a/src/window.cpp b/src/window.cpp index 55ca345..080fa91 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -19,6 +19,10 @@ Window::~Window() { glfwTerminate(); } +bool Window::is_mouse_enable() const { + return m_mouse_enable; +} + const GLFWwindow* Window::get_glfw_window() const { return m_window; } @@ -97,4 +101,14 @@ void Window::toggle_fullscreen() { ); } 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; + } } \ No newline at end of file