Skip to content

Commit

Permalink
Clean up:
Browse files Browse the repository at this point in the history
- move `static` variable at function scope.
- use reference
  • Loading branch information
Jarod42 committed Apr 16, 2024
1 parent 58acd93 commit aced99f
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/sound/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ struct SelectionHandling {
unsigned char HowMany; /// number of sound played in this group
};

/// FIXME: docu
SelectionHandling SelectionHandler;

static int ViewPointOffset; /// Distance to Volume Mapping
int DistanceSilent; /// silent distance

Expand All @@ -94,23 +91,24 @@ static Mix_Chunk *SimpleChooseSample(const CSound &sound)
/**
** Choose the sample to play
*/
static Mix_Chunk *ChooseSample(CSound *sound, bool selection, Origin &source)
static Mix_Chunk *ChooseSample(CSound &sound, bool selection, Origin &source)
{
Mix_Chunk *result = nullptr;

if (!sound || !SoundEnabled()) {
if (!SoundEnabled()) {
return nullptr;
}

if (sound->Number == TWO_GROUPS) {
Mix_Chunk *result = nullptr;
static SelectionHandling SelectionHandler{};

if (sound.Number == TWO_GROUPS) {
// handle a special sound (selection)
if (SelectionHandler.Sound != nullptr && (SelectionHandler.Source.Base == source.Base && SelectionHandler.Source.Id == source.Id)) {
if (SelectionHandler.Sound == sound->Sound.TwoGroups.First) {
if (SelectionHandler.Sound == sound.Sound.TwoGroups.First) {
result = SimpleChooseSample(*SelectionHandler.Sound);
SelectionHandler.HowMany++;
if (SelectionHandler.HowMany >= 3) {
SelectionHandler.HowMany = 0;
SelectionHandler.Sound = sound->Sound.TwoGroups.Second;
SelectionHandler.Sound = sound.Sound.TwoGroups.Second;
}
} else {
//FIXME: checks for error
Expand All @@ -120,23 +118,23 @@ static Mix_Chunk *ChooseSample(CSound *sound, bool selection, Origin &source)
SelectionHandler.HowMany++;
if (SelectionHandler.HowMany >= SelectionHandler.Sound->Number) {
SelectionHandler.HowMany = 0;
SelectionHandler.Sound = sound->Sound.TwoGroups.First;
SelectionHandler.Sound = sound.Sound.TwoGroups.First;
}
} else {
result = SelectionHandler.Sound->Sound.OneSound;
SelectionHandler.HowMany = 0;
SelectionHandler.Sound = sound->Sound.TwoGroups.First;
SelectionHandler.Sound = sound.Sound.TwoGroups.First;
}
}
} else {
SelectionHandler.Source = source;
SelectionHandler.Sound = sound->Sound.TwoGroups.First;
SelectionHandler.Sound = sound.Sound.TwoGroups.First;
result = SimpleChooseSample(*SelectionHandler.Sound);
SelectionHandler.HowMany = 1;
}
} else {
// normal sound/sound group handling
result = SimpleChooseSample(*sound);
result = SimpleChooseSample(sound);
if (SelectionHandler.Source.Base == source.Base && SelectionHandler.Source.Id == source.Id) {
SelectionHandler.HowMany = 0;
SelectionHandler.Sound = nullptr;
Expand Down Expand Up @@ -261,7 +259,7 @@ void PlayUnitSound(const CUnit &unit, EUnitVoice voice, bool sampleUnique)
return;
}

Mix_Chunk *sample = ChooseSample(sound, selection, source);
Mix_Chunk *sample = ChooseSample(*sound, selection, source);

if (sampleUnique && SampleIsPlaying(sample)) {
return;
Expand Down Expand Up @@ -302,7 +300,7 @@ void PlayUnitSound(const CUnit &unit, CSound *sound)
return;
}

int channel = PlaySample(ChooseSample(sound, false, source));
int channel = PlaySample(ChooseSample(*sound, false, source));
if (channel == -1) {
return;
}
Expand Down Expand Up @@ -333,7 +331,7 @@ void PlayMissileSound(const Missile &missile, CSound *sound)
return;
}

int channel = PlaySample(ChooseSample(sound, false, source));
int channel = PlaySample(ChooseSample(*sound, false, source));
if (channel == -1) {
return;
}
Expand All @@ -354,7 +352,7 @@ void PlayGameSound(CSound *sound, unsigned char volume, bool always)
}
Origin source = {nullptr, 0};

Mix_Chunk *sample = ChooseSample(sound, false, source);
Mix_Chunk *sample = ChooseSample(*sound, false, source);

if (!sample || (!always && SampleIsPlaying(sample))) {
return;
Expand Down

0 comments on commit aced99f

Please sign in to comment.