diff --git a/src/core/GameApplication.cpp b/src/core/GameApplication.cpp index 4f17f16..e1cd044 100644 --- a/src/core/GameApplication.cpp +++ b/src/core/GameApplication.cpp @@ -102,5 +102,10 @@ void GameApplication::run() { m_windowManager->endUI(); m_windowManager->Present(); + cleanup(); } + +void GameApplication::cleanup() { + m_sceneManager->destoryQuitedScene(); +} diff --git a/src/core/GameApplication.h b/src/core/GameApplication.h index cfcad0c..9111c04 100644 --- a/src/core/GameApplication.h +++ b/src/core/GameApplication.h @@ -22,7 +22,7 @@ private: std::unique_ptr m_uiRenderer; std::unique_ptr m_debugManager; Config m_config; - + void cleanup(); public: GameApplication(); diff --git a/src/scenes/base/SceneManager.cpp b/src/scenes/base/SceneManager.cpp index 193be9e..0a53413 100644 --- a/src/scenes/base/SceneManager.cpp +++ b/src/scenes/base/SceneManager.cpp @@ -150,6 +150,7 @@ void SceneManager::changeScene(const std::string& sceneName) { // 退出当前场景 if (m_scene) { m_scene->onExit(); + m_quitedScene = m_scene; } // 切换到目标场景 m_scene = target; @@ -205,4 +206,14 @@ void SceneManager::handleSceneEvent(const SceneEvent& event) { SDL_Log("SceneManager::handleSceneEvent: Unhandled event type!\n"); break; } +} + +void SceneManager::destoryQuitedScene() { + if (!m_quitedScene) { + return; + } + const auto info = typeid(*m_quitedScene.get()).name(); + m_quitedScene.reset(); + std::cout << "SceneManager: " << info << " destroyed\n"; + } \ No newline at end of file diff --git a/src/scenes/base/SceneManager.h b/src/scenes/base/SceneManager.h index 833baf0..4d1235d 100644 --- a/src/scenes/base/SceneManager.h +++ b/src/scenes/base/SceneManager.h @@ -79,7 +79,7 @@ public: void popScene(); /** - * @brief 替换当前场景 + * @brief 替换当前场景,在帧结束销毁退出的场景,如果要保留场景,应该用pushScene * @param sceneName 场景名称,用于缓存查找或工厂创建 * @details 当前场景将接收 onExit() 回调并被替换。若缓存中存在该名称的场景则复用, * 否则通过工厂函数创建新场景。新场景成为当前场景并接收 onEnter() 回调 @@ -117,6 +117,10 @@ public: * @param event 场景事件对象 */ void handleSceneEvent(const SceneEvent& event); + /** + * @brief 在帧结束的时候销毁退出的场景 + */ + void destoryQuitedScene(); private: SDL_Renderer* m_renderer; ///< SDL 渲染器指针 @@ -126,7 +130,7 @@ private: std::stack> m_scenes; ///< 场景栈,存储场景层级关系(使用 shared_ptr) std::unordered_map> m_sceneCache; ///< 场景缓存,按名字缓存场景以便切换时复用 std::unordered_map()>> m_sceneFactories; ///< 场景工厂映射,按名字动态创建场景实例 - + std::shared_ptr m_quitedScene; ///< 已经退出的场景,准备在最后销毁 /** * @brief 获取窗口尺寸的辅助方法 * @return 返回 {宽度, 高度},如果获取失败则返回默认值 {1600, 900}