Skip to content

Commit

Permalink
Optimize sound collection for Guns subsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
ForserX committed Mar 4, 2025
1 parent 9680945 commit 2aae752
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 28 deletions.
9 changes: 9 additions & 0 deletions src/xrGame/HudItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions src/xrGame/HudItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);}
Expand Down
26 changes: 17 additions & 9 deletions src/xrGame/HudSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HUD_SOUND_ITEM>::iterator it = m_sound_items.begin();
xr_vector<HUD_SOUND_ITEM>::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)
Expand Down
9 changes: 6 additions & 3 deletions src/xrGame/HudSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 4 additions & 8 deletions src/xrGame/Missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
11 changes: 3 additions & 8 deletions src/xrGame/WeaponKnife.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CActor*>(H_Parent());
if (actor->active_cam() != eacFirstEye) {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 2aae752

Please sign in to comment.