From 2aae7528312d459009a250690e986e6e218e322b Mon Sep 17 00:00:00 2001 From: ForserX Date: Tue, 4 Mar 2025 15:37:13 +0300 Subject: [PATCH] Optimize sound collection for Guns subsystem --- src/xrGame/HudItem.cpp | 9 +++++++++ src/xrGame/HudItem.h | 2 ++ src/xrGame/HudSound.cpp | 26 +++++++++++++++++--------- src/xrGame/HudSound.h | 9 ++++++--- src/xrGame/Missile.cpp | 12 ++++-------- src/xrGame/WeaponKnife.cpp | 11 +++-------- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/xrGame/HudItem.cpp b/src/xrGame/HudItem.cpp index 9be3672797..4c69fa1877 100644 --- a/src/xrGame/HudItem.cpp +++ b/src/xrGame/HudItem.cpp @@ -719,6 +719,15 @@ float CHudItem::GetHudFov() return m_nearwall_last_hud_fov; } +void CHudItem::PlaySoundIfExist(LPCSTR alias, const Fvector& position, bool allowOverlap) +{ + HUD_SOUND_ITEM* SndIter = m_sounds.FindSoundItem(alias, false); + if (SndIter != nullptr) + { + m_sounds.PlaySound(SndIter, position, object().H_Root(), !!GetHUDmode(), false, allowOverlap); + } +} + void CHudItem::SetModelBoneStatus(const char* bone, BOOL show) { if (HudItemData()) diff --git a/src/xrGame/HudItem.h b/src/xrGame/HudItem.h index 340aee8809..ed036ea4ae 100644 --- a/src/xrGame/HudItem.h +++ b/src/xrGame/HudItem.h @@ -144,6 +144,8 @@ class CHudItem :public CHUDState virtual float GetHudFov(); virtual bool AllowBore() { return !m_bDisableBore; } + void PlaySoundIfExist(LPCSTR alias, const Fvector& position, bool allowOverlap = false); + protected: IC void SetPending (BOOL H) { m_huditem_flags.set(fl_pending, H);} diff --git a/src/xrGame/HudSound.cpp b/src/xrGame/HudSound.cpp index f55962e1d5..ecd4714065 100644 --- a/src/xrGame/HudSound.cpp +++ b/src/xrGame/HudSound.cpp @@ -153,19 +153,27 @@ HUD_SOUND_ITEM* HUD_SOUND_COLLECTION::FindSoundItem(LPCSTR alias, bool b_assert) } } -void HUD_SOUND_COLLECTION::PlaySound(LPCSTR alias, const Fvector& position, const CObject* parent, - bool hud_mode, bool looped, bool allowOverlap, u8 index) { - xr_vector::iterator it = m_sound_items.begin(); - xr_vector::iterator it_e = m_sound_items.end(); - for(;it!=it_e;++it) +void HUD_SOUND_COLLECTION::PlaySound(HUD_SOUND_ITEM* snd_item, const Fvector& position, const CObject* parent, bool hud_mode, bool looped, bool allowOverlap, u8 index) +{ + for (HUD_SOUND_ITEM& Item : m_sound_items) { - if(it->m_b_exclusive) - HUD_SOUND_ITEM::StopSound (*it); + if (Item.m_b_exclusive) + HUD_SOUND_ITEM::StopSound(Item); } + HUD_SOUND_ITEM::PlaySound(*snd_item, position, parent, hud_mode, looped, allowOverlap, index); +} + +void HUD_SOUND_COLLECTION::PlaySound(LPCSTR alias, const Fvector& position, const CObject* parent, bool hud_mode, bool looped, bool allowOverlap, u8 index) +{ + for (HUD_SOUND_ITEM& Item : m_sound_items) + { + if (Item.m_b_exclusive) + HUD_SOUND_ITEM::StopSound(Item); + } - HUD_SOUND_ITEM* snd_item = FindSoundItem(alias, true); - HUD_SOUND_ITEM::PlaySound (*snd_item, position, parent, hud_mode, looped, allowOverlap, index); + HUD_SOUND_ITEM* snd_item = FindSoundItem(alias, true); + HUD_SOUND_ITEM::PlaySound(*snd_item, position, parent, hud_mode, looped, allowOverlap, index); } void HUD_SOUND_COLLECTION::StopSound(LPCSTR alias) diff --git a/src/xrGame/HudSound.h b/src/xrGame/HudSound.h index 30816e8a9b..3b8b379d53 100644 --- a/src/xrGame/HudSound.h +++ b/src/xrGame/HudSound.h @@ -58,9 +58,12 @@ class HUD_SOUND_COLLECTION HUD_SOUND_ITEM* FindSoundItem(LPCSTR alias, bool b_assert); HUD_SOUND_COLLECTION(); ~HUD_SOUND_COLLECTION(); - shared_str m_alias; //Alundaio: For use when it's part of a layered Collection - void PlaySound(LPCSTR alias, const Fvector& position, const CObject* parent, - bool hud_mode, bool looped = false, bool allowOverlap = false, u8 index = u8(-1)); + shared_str m_alias; + + void PlaySound(HUD_SOUND_ITEM* Iter, const Fvector& position, const CObject* parent, bool hud_mode, bool looped = false, bool allowOverlap = false, u8 index = u8(-1)); + + //Alundaio: For use when it's part of a layered Collection + void PlaySound(LPCSTR alias, const Fvector& position, const CObject* parent, bool hud_mode, bool looped = false, bool allowOverlap = false, u8 index = u8(-1)); void StopSound ( LPCSTR alias); diff --git a/src/xrGame/Missile.cpp b/src/xrGame/Missile.cpp index 82c692cd6d..c4ec2b2a40 100644 --- a/src/xrGame/Missile.cpp +++ b/src/xrGame/Missile.cpp @@ -309,8 +309,7 @@ void CMissile::State(u32 state) { SetPending (TRUE); PlayHUDMotion("anm_show", FALSE, this, GetState()); - if (m_sounds.FindSoundItem("SndShow", false)) - PlaySound("SndShow", Position()); + PlaySoundIfExist("SndShow", Position()); } break; case eIdle: { @@ -323,8 +322,7 @@ void CMissile::State(u32 state) { SetPending (TRUE); PlayHUDMotion ("anm_hide", TRUE, this, GetState()); - if (m_sounds.FindSoundItem("SndHide", false)) - PlaySound("SndHide", Position()); + PlaySoundIfExist("SndHide", Position()); } } break; case eHidden: @@ -346,8 +344,7 @@ void CMissile::State(u32 state) { SetPending (TRUE); m_fThrowForce = m_fMinForce; - if (m_sounds.FindSoundItem("sndThrowBegin", false)) - PlaySound("sndThrowBegin", Position()); + PlaySoundIfExist("sndThrowBegin", Position()); PlayHUDMotion ("anm_throw_begin", TRUE, this, GetState()); } break; case eReady: @@ -358,8 +355,7 @@ void CMissile::State(u32 state) { SetPending (TRUE); m_throw = false; - if (m_sounds.FindSoundItem("sndThrow", false)) - PlaySound("sndThrow", Position()); + PlaySoundIfExist("sndThrow", Position()); PlayHUDMotion ("anm_throw", TRUE, this, GetState()); } break; case eThrowEnd: diff --git a/src/xrGame/WeaponKnife.cpp b/src/xrGame/WeaponKnife.cpp index 5710472cf1..aa6b78256d 100644 --- a/src/xrGame/WeaponKnife.cpp +++ b/src/xrGame/WeaponKnife.cpp @@ -212,10 +212,7 @@ void CWeaponKnife::MakeShot(Fvector const & pos, Fvector const & dir, float cons iAmmoElapsed = (u32)m_magazine.size(); bool SendHit = SendHitAllowed(H_Parent()); - if (m_sounds.FindSoundItem("sndShot", false)) - { - PlaySound("sndShot", pos); - } + PlaySoundIfExist("sndShot", pos); CActor* actor = smart_cast(H_Parent()); if (actor->active_cam() != eacFirstEye) { @@ -322,8 +319,7 @@ void CWeaponKnife::switch2_Hiding () FireEnd (); VERIFY(GetState()==eHiding); PlayHUDMotion("anm_hide", TRUE, this, GetState()); - if (m_sounds.FindSoundItem("SndHide", false)) - PlaySound("SndHide", get_LastFP()); + PlaySoundIfExist("SndHide", get_LastFP()); } void CWeaponKnife::switch2_Hidden() @@ -336,8 +332,7 @@ void CWeaponKnife::switch2_Showing () { VERIFY(GetState()==eShowing); PlayHUDMotion("anm_show", FALSE, this, GetState()); - if (m_sounds.FindSoundItem("SndShow", false)) - PlaySound("SndShow", get_LastFP()); + PlaySoundIfExist("SndShow", get_LastFP()); } void CWeaponKnife::UpdateCL()