diff --git a/src/kiwano-audio/SoundPlayer.cpp b/src/kiwano-audio/SoundPlayer.cpp index bfe7a562..db48a810 100644 --- a/src/kiwano-audio/SoundPlayer.cpp +++ b/src/kiwano-audio/SoundPlayer.cpp @@ -35,78 +35,77 @@ SoundPlayer::~SoundPlayer() ClearCache(); } +size_t SoundPlayer::GetId(const String& file_path) const +{ + return std::hash()(file_path); +} + +size_t SoundPlayer::GetId(const Resource& res) const +{ + return static_cast(res.GetId()); +} + size_t SoundPlayer::Load(const String& file_path) { - size_t hash = std::hash()(file_path); - if (sound_cache_.end() != sound_cache_.find(hash)) - return hash; + size_t id = GetId(file_path); + if (sound_cache_.end() != sound_cache_.find(id)) + return id; SoundPtr sound = MakePtr(); - if (sound) + if (sound && sound->Load(file_path)) { - if (sound->Load(file_path)) - { - sound->SetVolume(volume_); - sound_cache_.insert(std::make_pair(hash, sound)); - return hash; - } + sound->SetVolume(volume_); + sound_cache_.insert(std::make_pair(id, sound)); + return id; } return 0; } size_t SoundPlayer::Load(const Resource& res) { - size_t hash_code = static_cast(res.GetId()); - if (sound_cache_.end() != sound_cache_.find(hash_code)) - return hash_code; + size_t id = GetId(res); + if (sound_cache_.end() != sound_cache_.find(id)) + return id; SoundPtr sound = MakePtr(); - if (sound) + if (sound && sound->Load(res)) { - if (sound->Load(res)) - { - sound->SetVolume(volume_); - sound_cache_.insert(std::make_pair(hash_code, sound)); - return hash_code; - } + sound->SetVolume(volume_); + sound_cache_.insert(std::make_pair(id, sound)); + return id; } return 0; } void SoundPlayer::Play(size_t id, int loop_count) { - auto iter = sound_cache_.find(id); - if (sound_cache_.end() != iter) - iter->second->Play(loop_count); + if (auto sound = GetSound(id)) + sound->Play(loop_count); } void SoundPlayer::Pause(size_t id) { - auto iter = sound_cache_.find(id); - if (sound_cache_.end() != iter) - iter->second->Pause(); + if (auto sound = GetSound(id)) + sound->Pause(); } void SoundPlayer::Resume(size_t id) { - auto iter = sound_cache_.find(id); - if (sound_cache_.end() != iter) - iter->second->Resume(); + if (auto sound = GetSound(id)) + sound->Resume(); } void SoundPlayer::Stop(size_t id) { - auto iter = sound_cache_.find(id); - if (sound_cache_.end() != iter) - iter->second->Stop(); + if (auto sound = GetSound(id)) + sound->Stop(); } bool SoundPlayer::IsPlaying(size_t id) { - auto iter = sound_cache_.find(id); - if (sound_cache_.end() != iter) - return iter->second->IsPlaying(); + if (auto sound = GetSound(id)) + return sound->IsPlaying(); return false; } @@ -124,6 +123,14 @@ void SoundPlayer::SetVolume(float volume) } } +SoundPtr SoundPlayer::GetSound(size_t id) const +{ + auto iter = sound_cache_.find(id); + if (iter != sound_cache_.end()) + return iter->second; + return SoundPtr(); +} + void SoundPlayer::PauseAll() { for (auto& pair : sound_cache_) diff --git a/src/kiwano-audio/SoundPlayer.h b/src/kiwano-audio/SoundPlayer.h index d9ae2719..6ed8d8f6 100644 --- a/src/kiwano-audio/SoundPlayer.h +++ b/src/kiwano-audio/SoundPlayer.h @@ -91,6 +91,23 @@ public: /// @param volume 音量大小,1.0 为原始音量, 大于 1 为放大音量, 0 为最小音量 void SetVolume(float volume); + /// \~chinese + /// @brief 获取本地音频文件id + /// @param file_path 本地音频文件路径 + /// @return 音频标识符 + size_t GetId(const String& file_path) const; + + /// \~chinese + /// @brief 获取音频资源id + /// @param res 音频资源 + /// @return 音频标识符 + size_t GetId(const Resource& res) const; + + /// \~chinese + /// @brief 获取音乐对象 + /// @param id 音频标识符 + SoundPtr GetSound(size_t id) const; + /// \~chinese /// @brief 暂停所有音频 void PauseAll(); diff --git a/src/kiwano-imgui/ImGuiLayer.cpp b/src/kiwano-imgui/ImGuiLayer.cpp index 128f28d2..29babfcc 100644 --- a/src/kiwano-imgui/ImGuiLayer.cpp +++ b/src/kiwano-imgui/ImGuiLayer.cpp @@ -53,7 +53,7 @@ bool ImGuiLayer::CheckVisibility(RenderContext& ctx) const void ImGuiLayer::AddItem(const String& name, const ImGuiPipeline& item) { - pipelines_.insert(std::make_pair(name, item)); + pipelines_[name] = item; } void ImGuiLayer::RemoveItem(const String& name) diff --git a/src/kiwano/base/component/ComponentManager.cpp b/src/kiwano/base/component/ComponentManager.cpp index 392c4b43..792e61cd 100644 --- a/src/kiwano/base/component/ComponentManager.cpp +++ b/src/kiwano/base/component/ComponentManager.cpp @@ -49,7 +49,7 @@ Component* ComponentManager::AddComponent(size_t index, ComponentPtr component) { component->InitComponent(target_); - components_.insert(std::make_pair(index, component)); + components_[index] = component; } return component.Get(); } diff --git a/src/kiwano/core/Serializable.h b/src/kiwano/core/Serializable.h index c160cf7d..1c934657 100644 --- a/src/kiwano/core/Serializable.h +++ b/src/kiwano/core/Serializable.h @@ -427,7 +427,7 @@ inline Deserializer& operator>>(Deserializer& deserializer, Map<_KTy, _Ty>& map) _KTy key; _Ty value; deserializer >> key >> value; - map.insert(std::make_pair(key, value)); + map[key] = value; } return deserializer; } diff --git a/src/kiwano/render/Font.cpp b/src/kiwano/render/Font.cpp index 6150bbed..2bbfe4ce 100644 --- a/src/kiwano/render/Font.cpp +++ b/src/kiwano/render/Font.cpp @@ -119,13 +119,13 @@ FontCache::~FontCache() {} void FontCache::AddFont(size_t key, FontPtr font) { - font_cache_.insert(std::make_pair(key, font)); + font_cache_[key] = font; } void FontCache::AddFontByFamily(const String& font_family, FontPtr font) { String family = TransformFamily(font_family); - font_family_cache_.insert(std::make_pair(family, font)); + font_family_cache_[family] = font; } FontPtr FontCache::GetFont(size_t key) const diff --git a/src/kiwano/render/TextureCache.cpp b/src/kiwano/render/TextureCache.cpp index c6922b8c..8797fe3f 100644 --- a/src/kiwano/render/TextureCache.cpp +++ b/src/kiwano/render/TextureCache.cpp @@ -32,12 +32,12 @@ TextureCache::~TextureCache() void TextureCache::AddTexture(size_t key, TexturePtr texture) { - texture_cache_.insert(std::make_pair(key, texture)); + texture_cache_[key] = texture; } void TextureCache::AddGifImage(size_t key, GifImagePtr gif) { - gif_texture_cache_.insert(std::make_pair(key, gif)); + gif_texture_cache_[key] = gif; } TexturePtr TextureCache::GetTexture(size_t key) const diff --git a/src/kiwano/utils/ConfigIni.cpp b/src/kiwano/utils/ConfigIni.cpp index 529d190c..e27993c5 100644 --- a/src/kiwano/utils/ConfigIni.cpp +++ b/src/kiwano/utils/ConfigIni.cpp @@ -311,13 +311,13 @@ void ConfigIni::SetSectionMap(const SectionMap& sections) void ConfigIni::SetSection(const String& section, const ValueMap& values) { - sections_.insert(std::make_pair(section, values)); + sections_[section] = values; } void ConfigIni::SetString(const String& section, const String& key, const String& value) { if (HasSection(section)) - sections_[section].insert(std::make_pair(key, value)); + sections_[section][key] = value; else SetSection(section, ValueMap{ { key, value } }); } @@ -363,19 +363,11 @@ void ConfigIni::DeleteKey(const String& section, const String& key) ConfigIni::ValueMap& ConfigIni::operator[](const String& section) { - if (!HasSection(section)) - { - sections_.insert(std::make_pair(section, ValueMap())); - } return sections_[section]; } const ConfigIni::ValueMap& ConfigIni::operator[](const String& section) const { - if (!HasSection(section)) - { - const_cast(sections_).insert(std::make_pair(section, ValueMap())); - } return sections_.at(section); } diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp index b47490c4..96ff0205 100644 --- a/src/kiwano/utils/ResourceCache.cpp +++ b/src/kiwano/utils/ResourceCache.cpp @@ -47,7 +47,7 @@ bool ResourceCache::LoadFromXmlFile(const String& file_path) void ResourceCache::AddObject(const String& id, ObjectBasePtr obj) { - object_cache_.insert(std::make_pair(id, obj)); + object_cache_[id] = obj; } void ResourceCache::Remove(const String& id) diff --git a/src/kiwano/utils/UserData.cpp b/src/kiwano/utils/UserData.cpp index cfe12a88..bb353bf9 100644 --- a/src/kiwano/utils/UserData.cpp +++ b/src/kiwano/utils/UserData.cpp @@ -34,19 +34,19 @@ Any UserData::Get(const String& key, const Any& default_data) const void UserData::Set(const String& key, const Any& data) { - data_.insert(std::make_pair(key, data)); + data_[key] = data; } void UserData::Set(const DataPair& pair) { - data_.insert(pair); + data_[pair.first] = pair.second; } void UserData::Set(const std::initializer_list& list) { for (const auto& pair : list) { - data_.insert(pair); + data_[pair.first] = pair.second; } }