Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 4dd5932

Browse files
committed
Made NetworkClient m_Sounds and unordered map for quicker insertion and deletion (which is most of what's being done) and renamed it to m_ServerSounds to make it clearer.
Rewrote the ReceiveSoundEvents method to accomodate changes and be cleaner and, hopefully still efficient despite SoundContainers being able to have multiple channels
1 parent 7c59e25 commit 4dd5932

File tree

2 files changed

+47
-75
lines changed

2 files changed

+47
-75
lines changed

Managers/NetworkClient.cpp

Lines changed: 45 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ namespace RTE
8484
}
8585

8686
// Stop all sounds received from server
87-
for (std::map<short int, SoundContainer *>::iterator it = m_Sounds.begin(); it != m_Sounds.end(); ++it)
87+
for (std::unordered_map<unsigned short int, SoundContainer *>::iterator it = m_ServerSounds.begin(); it != m_ServerSounds.end(); ++it)
8888
{
8989
it->second->Stop();
9090
delete it->second;
9191
}
9292

93-
m_Sounds.clear();
93+
m_ServerSounds.clear();
9494
}
9595

9696
//////////////////////////////////////////////////////////////////////////////////////////
@@ -667,90 +667,62 @@ namespace RTE
667667
//g_ConsoleMan.PrintString(buf);
668668
}
669669

670-
void NetworkClient::ReceiveSoundEventsMsg(RakNet::Packet * p)
671-
{
670+
void NetworkClient::ReceiveSoundEventsMsg(RakNet::Packet * p) {
672671
MsgSoundEvents * msg = (MsgSoundEvents *)p->data;
673672
AudioMan::NetworkSoundData * sndDataPtr = (AudioMan::NetworkSoundData *)((char *)msg + sizeof(MsgSoundEvents));
674673

675-
for (int i = 0; i < msg->SoundEventsCount; i++)
676-
{
677-
if (sndDataPtr->State == AudioMan::SOUND_PLAY)
678-
{
679-
std::string path = ContentFile::GetPathFromHash(sndDataPtr->SoundHash);
680-
if (path != "")
681-
{
682-
SoundContainer *pSound = new SoundContainer();
683-
pSound->Create(path, sndDataPtr->AffectedByPitch > 0 ? true : false, sndDataPtr->Loops);
684-
g_AudioMan.SetSoundPitch(pSound, sndDataPtr->Pitch);
685-
686-
pSound->Play(sndDataPtr->Distance);
687-
688-
// Stop sound at these channels just in case
689-
for (std::unordered_set<short int>::iterator channelIterator = sndDataPtr->Channels.begin(); channelIterator != sndDataPtr->Channels.end(); ++channelIterator) {
690-
if (m_Sounds.count(*channelIterator) > 0) {
691-
m_Sounds[(*channelIterator)]->Stop();
692-
delete m_Sounds[(*channelIterator)];
693-
}
694-
695-
m_Sounds[(*channelIterator)] = pSound;
674+
for (int msgIndex = 0; msgIndex < msg->SoundEventsCount; msgIndex++) {
675+
if (sndDataPtr->State == AudioMan::SOUND_SET_GLOBAL_PITCH) {
676+
g_AudioMan.SetGlobalPitch(sndDataPtr->Pitch, sndDataPtr->AffectedByGlobalPitch); //Note AffectedByGlobalPitch is hackily used to determine whether this affects music
677+
} else {
678+
SoundContainer *newlyMadeSoundContainer;
679+
if (sndDataPtr->State == AudioMan::SOUND_PLAY) {
680+
newlyMadeSoundContainer = new SoundContainer();
681+
newlyMadeSoundContainer->Create(sndDataPtr->Loops, sndDataPtr->AffectedByGlobalPitch);
682+
for (size_t soundFileHash : sndDataPtr->SoundFileHashes) {
683+
if (soundFileHash != 0) { newlyMadeSoundContainer->AddSound(ContentFile::GetPathFromHash(soundFileHash)); }
696684
}
697-
698-
//char buf[128];
699-
//sprintf_s(buf, sizeof(buf), "PLAY %d %d %f %s", sndDataPtr->Loops, pSound->GetCurrentChannel(), sndDataPtr->Pitch, path.c_str());
700-
//g_ConsoleMan.PrintString(buf);
701-
}
702-
else
703-
{
704-
//char buf[128];
705-
//sprintf_s(buf, sizeof(buf), "NO SOUND %d", sndDataPtr->SoundHash);
706-
//g_ConsoleMan.PrintString(buf);
685+
g_AudioMan.PlaySound(newlyMadeSoundContainer, sndDataPtr->Distance, -1, -1, sndDataPtr->Pitch);
707686
}
708-
}
709-
else if (sndDataPtr->State == AudioMan::SOUND_SET_PITCH)
710-
{
711-
if (sndDataPtr->SoundHash == 0)
712-
{
713-
g_AudioMan.SetGlobalPitch(sndDataPtr->Pitch, sndDataPtr->AffectedByPitch > 0 ? true : false);
714-
//char buf[128];
715-
//sprintf_s(buf, sizeof(buf), "GLOBAL PITCH %f %d", sndDataPtr->Pitch, sndDataPtr->AffectedByPitch);
716-
//g_ConsoleMan.PrintString(buf);
717-
}
718-
else
719-
{
720-
for (std::unordered_set<short int>::iterator channelIterator = sndDataPtr->Channels.begin(); channelIterator != sndDataPtr->Channels.end(); ++channelIterator) {
721-
if (m_Sounds.count(*channelIterator) > 0) {
722-
//char buf[128];
723-
//sprintf_s(buf, sizeof(buf), "PITCH %d %f", m_Sounds[sndDataPtr->Channel]->GetCurrentChannel(), sndDataPtr->Pitch);
724-
//g_ConsoleMan.PrintString(buf);
725-
726-
g_AudioMan.SetSoundPitch(m_Sounds[(*channelIterator)], sndDataPtr->Pitch);
727-
} else {
728-
//char buf[128];
729-
//sprintf_s(buf, sizeof(buf), "Not found %d", sndDataPtr->Channel);
730-
//g_ConsoleMan.PrintString(buf);
687+
688+
// The set of channels that have already been handled for this event, used to potentially avoid repeating actions while still keeping being sure to potentially iterate over every provided channel index
689+
std::unordered_set<SoundContainer *> alreadyHandledSoundContainers;
690+
691+
for (unsigned short serverSoundChannelIndex : sndDataPtr->Channels) {
692+
if (serverSoundChannelIndex < c_NumberOfAudioChannels && m_ServerSounds.find(serverSoundChannelIndex) != m_ServerSounds.end()) {
693+
SoundContainer *soundContainerToHandle = m_ServerSounds[serverSoundChannelIndex];
694+
if (alreadyHandledSoundContainers.find(soundContainerToHandle) == alreadyHandledSoundContainers.end()) {
695+
switch (sndDataPtr->State) {
696+
case AudioMan::SOUND_PLAY:
697+
soundContainerToHandle->Stop();
698+
delete soundContainerToHandle;
699+
break;
700+
case AudioMan::SOUND_STOP:
701+
soundContainerToHandle->Stop();
702+
break;
703+
case AudioMan::SOUND_SET_ATTENUATION:
704+
soundContainerToHandle->UpdateAttenuation(sndDataPtr->Distance);
705+
break;
706+
case AudioMan::SOUND_SET_PITCH:
707+
g_AudioMan.SetSoundPitch(soundContainerToHandle, sndDataPtr->Pitch);
708+
break;
709+
case AudioMan::SOUND_FADE_OUT:
710+
g_AudioMan.FadeOutSound(soundContainerToHandle, sndDataPtr->FadeOutTime);
711+
break;
712+
default:
713+
RTEAbort("Multiplayer client tried to receive unhandled Sound Event, of state " + sndDataPtr->State);
714+
}
715+
alreadyHandledSoundContainers.insert(soundContainerToHandle);
731716
}
732-
}
733-
}
734-
}
735-
else if (sndDataPtr->State == AudioMan::SOUND_STOP)
736-
{
737-
for (std::unordered_set<short int>::iterator channelIterator = sndDataPtr->Channels.begin(); channelIterator != sndDataPtr->Channels.end(); ++channelIterator) {
738-
if (m_Sounds.count(*channelIterator) > 0) {
739-
//char buf[128];
740-
//sprintf_s(buf, sizeof(buf), "STOP %d", m_Sounds[sndDataPtr->Channel]->GetCurrentChannel());
741-
//g_ConsoleMan.PrintString(buf);
742717

743-
m_Sounds[(*channelIterator)]->Stop();
718+
// We always have to add the newly made sound container to the map of server sounds, regardless of whether we were able to delete existing sounds above
719+
if (sndDataPtr->State == AudioMan::SOUND_PLAY) { m_ServerSounds[serverSoundChannelIndex] = newlyMadeSoundContainer; }
744720
}
745721
}
746722
}
747723

748724
sndDataPtr++;
749725
}
750-
751-
//char buf[128];
752-
//sprintf_s(buf, sizeof(buf), "%d %d %d", msg->FrameNumber, m_PostEffects[msg->FrameNumber].size(), msg->PostEffectsCount);
753-
//g_ConsoleMan.PrintString(buf);
754726
}
755727

756728

Managers/NetworkClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ namespace RTE
249249
Vector m_TargetPos[FRAMES_TO_REMEMBER];
250250
std::list<PostEffect> m_PostEffects[FRAMES_TO_REMEMBER];
251251

252-
// List of sounds received from server. OWNED!!!
253-
std::map<short int, SoundContainer *> m_Sounds;
252+
// Unordered map of SoundContainers received from server. OWNED!!!
253+
std::unordered_map<unsigned short int, SoundContainer *> m_ServerSounds;
254254

255255
BITMAP * m_pSceneBackgroundBitmap;
256256
BITMAP * m_pSceneForegroundBitmap;

0 commit comments

Comments
 (0)