From 4da9bb66d0fa7fd69ce81e9e78d4a2bc8fd3ea89 Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Sat, 16 Dec 2017 14:08:53 +0500 Subject: [PATCH] xrGame/HudSound.cpp: use range-based for instead of iterators Also code cleanup --- src/xrGame/HudSound.cpp | 122 +++++++++++++--------------------------- 1 file changed, 40 insertions(+), 82 deletions(-) diff --git a/src/xrGame/HudSound.cpp b/src/xrGame/HudSound.cpp index fafc7277c36..280fab789d6 100644 --- a/src/xrGame/HudSound.cpp +++ b/src/xrGame/HudSound.cpp @@ -4,6 +4,7 @@ float psHUDSoundVolume = 1.0f; float psHUDStepSoundVolume = 1.0f; + void InitHudSoundSettings() { psHUDSoundVolume = pSettings->r_float("hud_sound", "hud_sound_vol_k"); @@ -12,7 +13,7 @@ void InitHudSoundSettings() void HUD_SOUND_ITEM::LoadSound(LPCSTR section, LPCSTR line, HUD_SOUND_ITEM& hud_snd, int type) { - hud_snd.m_activeSnd = NULL; + hud_snd.m_activeSnd = nullptr; hud_snd.sounds.clear(); string256 sound_line; @@ -39,7 +40,7 @@ void HUD_SOUND_ITEM::LoadSound(LPCSTR section, LPCSTR line, ref_sound& snd, int _GetItem(str, 0, buf_str); snd.create(buf_str, st_Effect, type); - if (volume != NULL) + if (volume != nullptr) { *volume = 1.f; if (count > 1) @@ -50,7 +51,7 @@ void HUD_SOUND_ITEM::LoadSound(LPCSTR section, LPCSTR line, ref_sound& snd, int } } - if (delay != NULL) + if (delay != nullptr) { *delay = 0; if (count > 2) @@ -64,12 +65,12 @@ void HUD_SOUND_ITEM::LoadSound(LPCSTR section, LPCSTR line, ref_sound& snd, int void HUD_SOUND_ITEM::DestroySound(HUD_SOUND_ITEM& hud_snd) { - xr_vector::iterator it = hud_snd.sounds.begin(); - for (; it != hud_snd.sounds.end(); ++it) - (*it).snd.destroy(); + for (auto& sound : hud_snd.sounds) + sound.snd.destroy(); + hud_snd.sounds.clear(); - hud_snd.m_activeSnd = NULL; + hud_snd.m_activeSnd = nullptr; } void HUD_SOUND_ITEM::PlaySound( @@ -78,7 +79,7 @@ void HUD_SOUND_ITEM::PlaySound( if (hud_snd.sounds.empty()) return; - hud_snd.m_activeSnd = NULL; + hud_snd.m_activeSnd = nullptr; StopSound(hud_snd); u32 flags = b_hud_mode ? sm_2D : 0; @@ -100,22 +101,19 @@ void HUD_SOUND_ITEM::PlaySound( void HUD_SOUND_ITEM::StopSound(HUD_SOUND_ITEM& hud_snd) { - xr_vector::iterator it = hud_snd.sounds.begin(); - for (; it != hud_snd.sounds.end(); ++it) - (*it).snd.stop(); - hud_snd.m_activeSnd = NULL; + for (auto& sound : hud_snd.sounds) + sound.snd.stop(); + + hud_snd.m_activeSnd = nullptr; } //---------------------------------------------------------- HUD_SOUND_COLLECTION::~HUD_SOUND_COLLECTION() { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - - for (; it != it_e; ++it) + for (auto& sound_item : m_sound_items) { - HUD_SOUND_ITEM::StopSound(*it); - HUD_SOUND_ITEM::DestroySound(*it); + HUD_SOUND_ITEM::StopSound(sound_item); + HUD_SOUND_ITEM::DestroySound(sound_item); } m_sound_items.clear(); @@ -127,23 +125,17 @@ HUD_SOUND_ITEM* HUD_SOUND_COLLECTION::FindSoundItem(LPCSTR alias, bool b_assert) if (it != m_sound_items.end()) return &*it; - else - { - R_ASSERT3(!b_assert, "sound item not found in collection", alias); - return NULL; - } + + R_ASSERT3(!b_assert, "sound item not found in collection", alias); + return nullptr; } void HUD_SOUND_COLLECTION::PlaySound( LPCSTR alias, const Fvector& position, const IGameObject* parent, bool hud_mode, bool looped, u8 index) { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - for (; it != it_e; ++it) - { - if (it->m_b_exclusive) - HUD_SOUND_ITEM::StopSound(*it); - } + for (auto& sound_item : m_sound_items) + if (sound_item.m_b_exclusive) + HUD_SOUND_ITEM::StopSound(sound_item); HUD_SOUND_ITEM* snd_item = FindSoundItem(alias, true); HUD_SOUND_ITEM::PlaySound(*snd_item, position, parent, hud_mode, looped, index); @@ -164,13 +156,8 @@ void HUD_SOUND_COLLECTION::SetPosition(LPCSTR alias, const Fvector& pos) void HUD_SOUND_COLLECTION::StopAllSounds() { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - - for (; it != it_e; ++it) - { - HUD_SOUND_ITEM::StopSound(*it); - } + for (auto& sound_item : m_sound_items) + HUD_SOUND_ITEM::StopSound(sound_item); } void HUD_SOUND_COLLECTION::LoadSound(LPCSTR section, LPCSTR line, LPCSTR alias, bool exclusive, int type) @@ -192,76 +179,47 @@ void HUD_SOUND_COLLECTION::LoadSound(LPCSTR section, LPCSTR line, LPCSTR alias, #ifdef LAYERED_SND_SHOOT HUD_SOUND_COLLECTION_LAYERED::~HUD_SOUND_COLLECTION_LAYERED() { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - - for (; it != it_e; ++it) - { - it->~HUD_SOUND_COLLECTION(); - } + for (auto& sound_item : m_sound_items) + sound_item.~HUD_SOUND_COLLECTION(); m_sound_items.clear(); } void HUD_SOUND_COLLECTION_LAYERED::StopAllSounds() { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - - for (; it != it_e; ++it) - { - it->StopAllSounds(); - } + for (auto& sound_item : m_sound_items) + sound_item.StopAllSounds(); } void HUD_SOUND_COLLECTION_LAYERED::StopSound(pcstr alias) { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - - for (; it != it_e; ++it) - { - if (it->m_alias == alias) - it->StopSound(alias); - } + for (auto& sound_item : m_sound_items) + if (sound_item.m_alias == alias) + sound_item.StopSound(alias); } void HUD_SOUND_COLLECTION_LAYERED::SetPosition(pcstr alias, const Fvector& pos) { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - - for (; it != it_e; ++it) - { - if (it->m_alias == alias) - it->SetPosition(alias, pos); - } + for (auto& sound_item : m_sound_items) + if (sound_item.m_alias == alias) + sound_item.SetPosition(alias, pos); } void HUD_SOUND_COLLECTION_LAYERED::PlaySound(pcstr alias, const Fvector& position, const IGameObject* parent, bool hud_mode, bool looped, u8 index) { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - - for (; it != it_e; ++it) - { - if (it->m_alias == alias) - it->PlaySound(alias, position, parent, hud_mode, looped, index); - } + for (auto& sound_item : m_sound_items) + if (sound_item.m_alias == alias) + sound_item.PlaySound(alias, position, parent, hud_mode, looped, index); } HUD_SOUND_ITEM* HUD_SOUND_COLLECTION_LAYERED::FindSoundItem(pcstr alias, bool b_assert) { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); + for (auto& sound_item : m_sound_items) + if (sound_item.m_alias == alias) + return sound_item.FindSoundItem(alias, b_assert); - for (; it != it_e; ++it) - { - if (it->m_alias == alias) - return it->FindSoundItem(alias, b_assert); - } return nullptr; }