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

Commit 85ccf0f

Browse files
committed
Fixed initialization of network sound data members by using fill in RegisterSoundEvent
Cleaned up NetworkClient, reorganizing things so they actually work, and make more sense - sound containers are no longer deleted, they're just repurposed. I need to investigate this more, it may be that pooling was doing this already, but losing pointers to them could make the already handled set useless.
1 parent 654dc5f commit 85ccf0f

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

Managers/AudioMan.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ namespace RTE {
573573

574574
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
575575

576-
void AudioMan::GetSoundEvents(int player, std::list<NetworkSoundData> & list) {
576+
void AudioMan::GetSoundEvents(int player, std::list<NetworkSoundData> &list) {
577577
if (player < 0 || player >= c_MaxClients) {
578578
return;
579579
}
@@ -600,9 +600,11 @@ namespace RTE {
600600
if (player >= 0 && player < c_MaxClients) {
601601
NetworkSoundData soundData;
602602
soundData.State = state;
603+
std::fill_n(soundData.Channels, c_MaxPlayingSoundsPerContainer, c_MaxAudioChannels + 1);
603604
if (channels) {
604605
std::copy(channels->begin(), channels->end(), soundData.Channels);
605606
}
607+
std::fill_n(soundData.SoundFileHashes, c_MaxAudioChannels, 0);
606608
if (soundFileHashes) {
607609
std::copy(soundFileHashes->begin(), soundFileHashes->end(), soundData.SoundFileHashes);
608610
}

Managers/AudioMan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ namespace RTE {
5656

5757
struct NetworkSoundData {
5858
unsigned char State;
59-
unsigned short int Channels[c_MaxPlayingSoundsPerContainer]{c_MaxAudioChannels + 1};
60-
size_t SoundFileHashes[c_MaxAudioChannels]{0};
59+
unsigned short int Channels[c_MaxPlayingSoundsPerContainer];
60+
size_t SoundFileHashes[c_MaxAudioChannels];
6161
short int Distance;
6262
short int Loops;
6363
float Pitch;

Managers/NetworkClient.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -675,27 +675,26 @@ namespace RTE
675675
if (sndDataPtr->State == AudioMan::SOUND_SET_GLOBAL_PITCH) {
676676
g_AudioMan.SetGlobalPitch(sndDataPtr->Pitch, sndDataPtr->AffectedByGlobalPitch); //Note AffectedByGlobalPitch is hackily used to determine whether this affects music
677677
} 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)); }
684-
}
685-
g_AudioMan.PlaySound(newlyMadeSoundContainer, sndDataPtr->Distance, -1, -1, sndDataPtr->Pitch);
686-
}
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
678+
// The set of SoundContainers that have already been handled for this event, used to hopefully avoid repeating actions when iterating over provided sound channel indices
689679
std::unordered_set<SoundContainer *> alreadyHandledSoundContainers;
690680

691681
for (unsigned short serverSoundChannelIndex : sndDataPtr->Channels) {
692-
if (serverSoundChannelIndex < c_MaxAudioChannels && m_ServerSounds.find(serverSoundChannelIndex) != m_ServerSounds.end()) {
693-
SoundContainer *soundContainerToHandle = m_ServerSounds[serverSoundChannelIndex];
682+
if (serverSoundChannelIndex < c_MaxAudioChannels && (sndDataPtr->State == AudioMan::SOUND_PLAY || m_ServerSounds.find(serverSoundChannelIndex) != m_ServerSounds.end())) {
683+
SoundContainer *soundContainerToHandle = m_ServerSounds.find(serverSoundChannelIndex) == m_ServerSounds.end() ? NULL : m_ServerSounds.at(serverSoundChannelIndex);
694684
if (alreadyHandledSoundContainers.find(soundContainerToHandle) == alreadyHandledSoundContainers.end()) {
695685
switch (sndDataPtr->State) {
696686
case AudioMan::SOUND_PLAY:
697-
soundContainerToHandle->Stop();
698-
delete soundContainerToHandle;
687+
if (soundContainerToHandle == NULL) {
688+
soundContainerToHandle = new SoundContainer();
689+
} else {
690+
soundContainerToHandle->Stop();
691+
soundContainerToHandle->Reset();
692+
}
693+
soundContainerToHandle->Create(sndDataPtr->Loops, sndDataPtr->AffectedByGlobalPitch);
694+
for (size_t soundFileHash : sndDataPtr->SoundFileHashes) {
695+
if (soundFileHash != 0) { soundContainerToHandle->AddSound(ContentFile::GetPathFromHash(soundFileHash)); }
696+
}
697+
g_AudioMan.PlaySound(soundContainerToHandle, sndDataPtr->Distance, -1, -1, sndDataPtr->Pitch);
699698
break;
700699
case AudioMan::SOUND_STOP:
701700
soundContainerToHandle->Stop();

0 commit comments

Comments
 (0)