diff --git a/src/gpu_resource/allocator.cpp b/src/gpu_resource/allocator.cpp index 2e265f7..9283d3d 100644 --- a/src/gpu_resource/allocator.cpp +++ b/src/gpu_resource/allocator.cpp @@ -126,13 +126,6 @@ namespace mirai { return flags; } - vma_allocator::~vma_allocator() { - if (vma_allocator_) { - vma_allocator_.destroy(); - vma_allocator_ = nullptr; - } - } - result_t vma_allocator::alloc_buffer(const buffer_create_info& info) { buffer_allocation alloc; @@ -160,4 +153,13 @@ namespace mirai { } return {}; } + + void vma_allocator::on_destroying() { + object::on_destroying(); + MIRAI_LOG_INFO("VMA 分配器正在销毁"); + if (vma_allocator_) { + vma_allocator_.destroy(); + vma_allocator_ = nullptr; + } + } } diff --git a/src/gpu_resource/allocator.h b/src/gpu_resource/allocator.h index 922b2a6..19900a7 100644 --- a/src/gpu_resource/allocator.h +++ b/src/gpu_resource/allocator.h @@ -42,7 +42,6 @@ namespace mirai { } explicit vma_allocator() = default; - ~vma_allocator() override; void setup(const allocator_config& config); @@ -50,6 +49,8 @@ namespace mirai { vma::AllocationCreateFlags to_vma_allocation_flags(memory_usage usage, bool persistent_mapped) noexcept; result_t alloc_buffer(const buffer_create_info& info); + protected: + void on_destroying() override; private: vk::Instance instance_{}; vk::PhysicalDevice physical_device_; diff --git a/src/render/vulkan_device.cpp b/src/render/vulkan_device.cpp index c2c793c..5fec5ac 100644 --- a/src/render/vulkan_device.cpp +++ b/src/render/vulkan_device.cpp @@ -116,7 +116,7 @@ mirai::vulkan_device::vulkan_device(const vulkan_device_config& config) { void mirai::vulkan_device::on_destroying() { object::on_destroying(); - MIRAI_LOG_INFO("Vulkan 设备正在销毁"); + MIRAI_LOG_INFO("Vulkan 设备 {} 正在销毁", get_device_name()); // 销毁逻辑设备 if (device_) { diff --git a/src/render/vulkan_device.h b/src/render/vulkan_device.h index 9d4dfea..39144b6 100644 --- a/src/render/vulkan_device.h +++ b/src/render/vulkan_device.h @@ -34,7 +34,7 @@ namespace mirai { [[nodiscard]] const auto& get_device() const noexcept { return device_; } [[nodiscard]] auto& get_dispatch_loader() noexcept { return disp_; } [[nodiscard]] const auto& get_dispatch_loader() const noexcept { return disp_; } - + [[nodiscard]] std::string get_device_name() const noexcept { return physical_device_.getProperties().deviceName.data(); } protected: void on_destroying() override; diff --git a/src/render/vulkan_instance.cpp b/src/render/vulkan_instance.cpp index 7e233ba..cc6e85c 100644 --- a/src/render/vulkan_instance.cpp +++ b/src/render/vulkan_instance.cpp @@ -82,12 +82,12 @@ namespace mirai { } } #endif - MIRAI_LOG_INFO("Vulkan instance object created"); + MIRAI_LOG_INFO("Vulkan instance 对象创建"); } void vulkan_instance::on_destroying() { object::on_destroying(); - MIRAI_LOG_INFO("Vulkan instance object destroying"); + MIRAI_LOG_INFO("Vulkan instance 对象销毁"); #if MIRAI_DEBUG if (debug_messenger_) { destroy_debug_messenger(); @@ -97,7 +97,6 @@ namespace mirai { instance_.destroy(); instance_ = nullptr; } - SDL_Quit(); } void_result_t vulkan_instance::create_instance(const vulkan_instance_config& config) { diff --git a/src/window/window.cpp b/src/window/window.cpp index e4bbb6f..a350c9d 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -31,6 +31,12 @@ namespace mirai { surface_ = surface; } + if (!SDL_SetWindowTitle(win_ptr, config.title.c_str())) { + SDL_DestroyWindow(win_ptr); + return MAKE_ERROR_INFO(error_code::window_creation_failed, + "SDL_SetWindowTitle 失败: {}", SDL_GetError()); + } + window_ = win_ptr; return {}; } @@ -52,6 +58,15 @@ namespace mirai { SDL_SetWindowPosition(window_, pos.x(), pos.y()); } + std::string window::get_window_title() { + const char* title = SDL_GetWindowTitle(window_); + if (!title) { + MIRAI_LOG_ERROR("SDL_GetWindowTitle 失败: {}", SDL_GetError()); + return ""; + } + return std::string(title); + } + vec2i window::get_pos() const { int x, y; if (!SDL_GetWindowPosition(window_, &x, &y)) { @@ -69,6 +84,7 @@ namespace mirai { void window::on_destroying() { object::on_destroying(); + MIRAI_LOG_INFO("窗口 {} 销毁", get_window_title()); SDL_Vulkan_DestroySurface(vulkan_context::get().get_vk_instance(), surface_, nullptr); SDL_DestroyWindow(window_); window_ = nullptr; diff --git a/src/window/window.h b/src/window/window.h index 1e8fcda..2086888 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -10,7 +10,7 @@ namespace mirai { vec2i size; // 窗口位置, 空表示默认位置(SDL_WINDOWPOS_CENTERED) std::optional pos; - std::string title; + std::string title = "MIRAI Window"; SDL_WindowFlags flags{}; bool vulkan_window = true; bool visible_on_create = true; @@ -30,6 +30,7 @@ namespace mirai { void show_window(bool show); void move_window(vec2i pos); + std::string get_window_title(); vec2i get_pos() const; diff --git a/src/window/window_manager.cpp b/src/window/window_manager.cpp index 609ac5e..10ad196 100644 --- a/src/window/window_manager.cpp +++ b/src/window/window_manager.cpp @@ -77,6 +77,10 @@ namespace mirai { void window_manager::on_destroying() { object::on_destroying(); + // 确保在 SDL_Quit 之前销毁所有窗口和 surface + main_window_.reset(); + windows_.clear(); + SDL_Quit(); }