Files
XCEngine/engine/src/Scene/SceneManager.cpp
ssdfasd 810b0861c5 Docs: Add audio module architecture design document
- Add XCEngine音频模块架构设计.md
- Design audio system following Unity-style architecture
- Include AudioSourceComponent, AudioListenerComponent, AudioClip, AudioMixer
- Document DSP effect system (FFT, Reverb, EQ, Compressor)
- Document 3D spatial audio with HRTF support
- Define IAudioBackend abstraction layer with WASAPI/OpenAL backends
- Outline 5-phase implementation priorities
2026-03-20 19:59:06 +08:00

116 lines
2.7 KiB
C++

#include "Scene/SceneManager.h"
namespace XCEngine {
namespace Components {
SceneManager& SceneManager::Get() {
static SceneManager instance;
return instance;
}
Scene* SceneManager::CreateScene(const std::string& name) {
auto scene = std::make_unique<Scene>(name);
Scene* ptr = scene.get();
m_scenes[name] = std::move(scene);
if (!m_activeScene) {
m_activeScene = ptr;
}
m_onSceneLoaded.Invoke(ptr);
return ptr;
}
void SceneManager::LoadScene(const std::string& filePath) {
auto scene = std::make_unique<Scene>();
scene->Load(filePath);
Scene* ptr = scene.get();
std::string name = filePath;
size_t pos = name.find_last_of("/\\");
if (pos != std::string::npos) {
name = name.substr(pos + 1);
}
pos = name.find_last_of('.');
if (pos != std::string::npos) {
name = name.substr(0, pos);
}
m_scenes[name] = std::move(scene);
m_onSceneLoaded.Invoke(ptr);
}
void SceneManager::LoadSceneAsync(const std::string& filePath, std::function<void(Scene*)> callback) {
LoadScene(filePath);
if (callback) {
Scene* scene = GetScene(filePath);
callback(scene);
}
}
void SceneManager::UnloadScene(Scene* scene) {
if (!scene) {
return;
}
if (m_activeScene == scene) {
m_activeScene = nullptr;
}
auto it = m_scenes.begin();
while (it != m_scenes.end()) {
if (it->second.get() == scene) {
m_onSceneUnloaded.Invoke(scene);
it = m_scenes.erase(it);
break;
} else {
++it;
}
}
if (m_activeScene == nullptr && !m_scenes.empty()) {
m_activeScene = m_scenes.begin()->second.get();
m_onActiveSceneChanged.Invoke(m_activeScene);
}
}
void SceneManager::UnloadScene(const std::string& sceneName) {
auto it = m_scenes.find(sceneName);
if (it != m_scenes.end()) {
UnloadScene(it->second.get());
}
}
void SceneManager::SetActiveScene(Scene* scene) {
if (m_activeScene == scene) {
return;
}
m_activeScene = scene;
m_onActiveSceneChanged.Invoke(scene);
}
void SceneManager::SetActiveScene(const std::string& sceneName) {
auto it = m_scenes.find(sceneName);
if (it != m_scenes.end()) {
SetActiveScene(it->second.get());
}
}
Scene* SceneManager::GetScene(const std::string& name) const {
auto it = m_scenes.find(name);
if (it != m_scenes.end()) {
return it->second.get();
}
return nullptr;
}
std::vector<Scene*> SceneManager::GetAllScenes() const {
std::vector<Scene*> result;
for (auto& pair : m_scenes) {
result.push_back(pair.second.get());
}
return result;
}
} // namespace Components
} // namespace XCEngine