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

Commit 6240570

Browse files
committed
Review Comments
Fixed AudioMan create and GUISound handling, and moved SoundEventsListMutex to header Fixed extraneous includes in MOSprite and ContentFile Fixed bad argument ordering in a few places, and some other little bugs Fixed some stuff in SoundContainer and moved some things in it and AudioMan into header Fixed inlines, casts, typos
1 parent f272d0d commit 6240570

File tree

7 files changed

+52
-108
lines changed

7 files changed

+52
-108
lines changed

Entities/MOSprite.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// Inclusions of header files
1616

1717
#include "MovableObject.h"
18-
#include "SoundContainer.h"
1918
#include "Box.h"
2019

2120
namespace RTE

Entities/SoundContainer.cpp

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,20 @@ namespace RTE {
5959
reader.ReportError(std::string("Failed to load the sample from the file"));
6060
}
6161
m_Sounds.push_back(std::pair<ContentFile, FMOD::Sound *>(newFile, pNewSample));
62-
} else if (propName == "LoopSetting")
62+
} else if (propName == "LoopSetting") {
6363
reader >> m_Loops;
64-
else if (propName == "Priority")
64+
} else if (propName == "Priority") {
6565
reader >> m_Priority;
66-
else if (propName == "AffectedByGlobalPitch")
66+
} else if (propName == "AffectedByGlobalPitch") {
6767
reader >> m_AffectedByGlobalPitch;
68-
else
68+
} else {
6969
// See if the base class(es) can find a match instead
7070
return Entity::ReadProperty(propName, reader);
71+
}
7172

7273
return 0;
7374
}
7475

75-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
76-
77-
int SoundContainer::Save(Writer &writer) const {
78-
return 0;
79-
}
80-
81-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82-
83-
void SoundContainer::Destroy(bool notInherited) {
84-
// Don't delete Sounds since they are owned in the ContentFile static maps
85-
if (!notInherited) {
86-
Entity::Destroy();
87-
}
88-
Clear();
89-
}
90-
9176
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9277

9378
void SoundContainer::AddSound(std::string soundPath) {
@@ -131,12 +116,4 @@ namespace RTE {
131116
FMOD::Sound *soundToStart;
132117
return m_Sounds[m_CurrentSound].second;
133118
}
134-
135-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136-
137-
void SoundContainer::FadeOut(int fadeOutTime) {
138-
if (HasAnySounds()) {
139-
return g_AudioMan.FadeOutSound(this, fadeOutTime);
140-
}
141-
}
142119
}

Entities/SoundContainer.h

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace RTE {
4040
/// </summary>
4141
/// <param name="soundPath">A path to the sound for this sound to have.</param>
4242
/// <param name="loops">The number of times this SoundContainer's sounds will loop. 0 means play once. -1 means play infinitely until stopped.</param>
43-
/// <param name="affectedByGlobalPitch">Whether this SoundContainer's sounds' frequency will be affected by the global pitch</param>
43+
/// <param name="affectedByGlobalPitch">Whether this SoundContainer's sounds' frequency will be affected by the global pitch.</param>
4444
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
4545
int Create(std::string soundPath, int loops = 0, bool affectedByGlobalPitch = true);
4646

@@ -51,6 +51,24 @@ namespace RTE {
5151
void AddSound(std::string soundPath);
5252
#pragma endregion
5353

54+
#pragma region Destruction
55+
/// <summary>
56+
/// Destructor method used to clean up a SoundContainer object before deletion from system memory.
57+
/// </summary>
58+
virtual ~SoundContainer() { Destroy(true); }
59+
60+
/// <summary>
61+
/// Destroys and resets (through Clear()) the SoundContainer object. It doesn't delete the Sound files, since they're owned by ContentFile static maps.
62+
/// </summary>
63+
/// <param name="notInherited">Whether to only destroy the members defined in this derived class, or to destroy all inherited members also.</param>
64+
virtual void Destroy(bool notInherited = false) { if (!notInherited) { Entity::Destroy(); } Clear(); }
65+
66+
/// <summary>
67+
/// Resets the entire SoundContainer, including its inherited members, to their default settings or values.
68+
/// </summary>
69+
virtual void Reset() { Clear(); Entity::Reset(); }
70+
#pragma endregion
71+
5472
#pragma region INI Handling
5573
/// <summary>
5674
/// Reads a property value from a Reader stream.
@@ -70,27 +88,7 @@ namespace RTE {
7088
/// </summary>
7189
/// <param name="writer">A Writer that the SoundContainer will save itself with.</param>
7290
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
73-
virtual int Save(Writer &writer) const;
74-
#pragma endregion
75-
76-
#pragma region Destruction
77-
/// <summary>
78-
/// Destructor method used to clean up a SoundContainer object before deletion from system memory.
79-
/// </summary>
80-
virtual ~SoundContainer() { Destroy(true); }
81-
82-
/// <summary>
83-
/// Destroys and resets (through Clear()) the SoundContainer object.
84-
/// </summary>
85-
/// <param name="notInherited">Whether to only destroy the members defined in this derived class, or to destroy all inherited members also.</param>
86-
virtual void Destroy(bool notInherited = false);
87-
#pragma endregion
88-
89-
#pragma region Virtual Override Methods
90-
/// <summary>
91-
/// Resets the entire SoundContainer, including its inherited members, to their default settings or values.
92-
/// </summary>
93-
virtual void Reset() { Clear(); Entity::Reset(); }
91+
virtual int Save(Writer &writer) const { return 0; }
9492
#pragma endregion
9593

9694
#pragma region Getters and Setters
@@ -219,7 +217,7 @@ namespace RTE {
219217
/// <param name="attenuation">How much distance attenuation to apply to the SoundContainer.</param>
220218
/// <param name="player">Player to start playback of this SoundContainer for.</param>
221219
/// <returns>Whether this SoundContainer successfully started playing on any channels.</returns>
222-
bool Play(float attenuation, int player) { return HasAnySounds() ? g_AudioMan.PlaySound(this, player, attenuation) : false; }
220+
bool Play(float attenuation, int player) { return HasAnySounds() ? g_AudioMan.PlaySound(this, attenuation, player) : false; }
223221

224222
/// <summary>
225223
/// Stops playback of this SoundContainer for all players.
@@ -244,7 +242,7 @@ namespace RTE {
244242
/// Fades out playback of the SoundContainer to 0 volume.
245243
/// </summary>
246244
/// <param name="fadeOutTime">How long the fadeout should take.</param>
247-
void FadeOut(int fadeOutTime = 1000);
245+
void FadeOut(int fadeOutTime = 1000) { if (HasAnySounds()) { return g_AudioMan.FadeOutSound(this, fadeOutTime); } }
248246
#pragma endregion
249247

250248
protected:

Main.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,8 +1880,6 @@ int main(int argc, char *argv[]) {
18801880
new PresetMan();
18811881
new FrameMan();
18821882
new AudioMan();
1883-
g_AudioMan.Create();
1884-
new GUISound(); //NOTE: Due to being unable to check if the fmod sound system actually exists and is up and running, AudioMan create should happen before anything that uses audio comes into existence
18851883
new UInputMan();
18861884
new ActivityMan();
18871885
new MovableMan();
@@ -1909,7 +1907,7 @@ int main(int argc, char *argv[]) {
19091907
g_TimerMan.Create();
19101908
g_PresetMan.Create();
19111909
g_FrameMan.Create();
1912-
g_AudioMan.Create();
1910+
g_AudioMan.Create(); //NOTE: By necessity of when things can be instantiated, this internally does: new GUISound()
19131911
g_GUISound.Create();
19141912
g_UInputMan.Create();
19151913
if (g_NetworkServer.IsServerModeEnabled()) { g_UInputMan.SetMultiplayerMode(true); }

Managers/AudioMan.cpp

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#include "ConsoleMan.h"
33
#include "SettingsMan.h"
44
#include "SoundContainer.h"
5+
#include "GUISound.h"
56

67
namespace RTE {
78
const std::string AudioMan::m_ClassName = "AudioMan";
89

9-
// I know this is a crime, but if I include it in FrameMan.h the whole thing will collapse due to int redefinitions in Allegro
10-
std::mutex g_SoundEventsListMutex[c_MaxClients];
10+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1111

1212
FMOD_RESULT F_CALLBACK AudioMan::MusicChannelEndedCallback(FMOD_CHANNELCONTROL *channelControl, FMOD_CHANNELCONTROL_TYPE channelControlType, FMOD_CHANNELCONTROL_CALLBACK_TYPE callbackType, void *unusedCommandData1, void *unusedCommandData2) {
1313
if (channelControlType == FMOD_CHANNELCONTROL_CHANNEL && callbackType == FMOD_CHANNELCONTROL_CALLBACK_END) {
@@ -33,7 +33,7 @@ namespace RTE {
3333
}
3434

3535
if (result != FMOD_OK) {
36-
g_ConsoleMan.PrintString("ERROR: An error occured when Ending a sound in SoundContainer " + channelSoundContainer->GetPresetName() + ": " + std::string(FMOD_ErrorString(result)));
36+
g_ConsoleMan.PrintString("ERROR: An error occurred when Ending a sound in SoundContainer " + channelSoundContainer->GetPresetName() + ": " + std::string(FMOD_ErrorString(result)));
3737
return result;
3838
}
3939
}
@@ -86,6 +86,8 @@ namespace RTE {
8686
SetSoundsVolume(m_SoundsVolume);
8787
SetMusicVolume(m_MusicVolume);
8888

89+
new GUISound(); //NOTE: Anything that instantiates SoundContainers needs to wait until the Audio System is up and running before they start doing that. It'll fail safely even if Audio is not enabled.
90+
8991
return 0;
9092
}
9193

@@ -106,9 +108,7 @@ namespace RTE {
106108
m_AudioSystem->update();
107109

108110
// Done waiting for silence
109-
if (!IsMusicPlaying() && m_SilenceTimer.IsPastRealTimeLimit()) {
110-
PlayNextStream();
111-
}
111+
if (!IsMusicPlaying() && m_SilenceTimer.IsPastRealTimeLimit()) { PlayNextStream(); }
112112
}
113113
}
114114

@@ -119,15 +119,11 @@ namespace RTE {
119119
return;
120120
}
121121

122-
if (m_IsInMultiplayerMode) {
123-
RegisterSoundEvent(-1, SOUND_SET_PITCH, 0, 0, std::unordered_set<short int>(), 0, pitch, excludeMusic);
124-
}
122+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_PITCH, 0, 0, std::unordered_set<short int>(), 0, pitch, excludeMusic); }
125123

126124
m_GlobalPitch = Limit(pitch, 8, 0.125); //Limit pitch change to 8 octaves up or down
127125

128-
if (!excludeMusic) {
129-
m_MusicChannelGroup->setPitch(m_GlobalPitch);
130-
}
126+
if (!excludeMusic) { m_MusicChannelGroup->setPitch(m_GlobalPitch); }
131127

132128
int numChannels;
133129
FMOD_RESULT result = m_SoundChannelGroup->getNumChannels(&numChannels);
@@ -143,24 +139,12 @@ namespace RTE {
143139
result == FMOD_OK ? soundChannel->getUserData(&userData) : result;
144140
SoundContainer *channelSoundContainer = (SoundContainer *)userData;
145141

146-
if (channelSoundContainer->IsAffectedByGlobalPitch()) {
147-
soundChannel->setPitch(pitch);
148-
}
142+
if (channelSoundContainer->IsAffectedByGlobalPitch()) { soundChannel->setPitch(pitch); }
149143
}
150144
}
151145
}
152146
}
153147

154-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
155-
156-
void AudioMan::SetMusicVolume(double volume) {
157-
m_MusicVolume = Limit(volume, 1, 0);
158-
159-
if (m_AudioEnabled) {
160-
m_MusicChannelGroup->setVolume(m_MusicVolume);
161-
}
162-
}
163-
164148
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
165149

166150
void AudioMan::SetTempMusicVolume(double volume) {
@@ -179,13 +163,12 @@ namespace RTE {
179163
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
180164

181165
bool AudioMan::SetMusicPitch(float pitch) {
182-
if (m_IsInMultiplayerMode) {
183-
RegisterMusicEvent(-1, MUSIC_SET_PITCH, 0, 0, 0.0, pitch);
184-
}
185166
if (!m_AudioEnabled) {
186167
return false;
187168
}
188169

170+
if (m_IsInMultiplayerMode) { RegisterMusicEvent(-1, MUSIC_SET_PITCH, 0, 0, 0.0, pitch); }
171+
189172
pitch = Limit(pitch, 8, 0.125); //Limit pitch change to 8 octaves up or down
190173
FMOD_RESULT result = m_MusicChannelGroup->setPitch(pitch);
191174

@@ -209,7 +192,7 @@ namespace RTE {
209192
if (result != FMOD_OK) {
210193
g_ConsoleMan.PrintString("ERROR: Could not get music position: " + std::string(FMOD_ErrorString(result)));
211194
}
212-
return result == FMOD_OK ? ((double)position) / 1000 : 0;
195+
return result == FMOD_OK ? (static_cast<double>(position)) / 1000 : 0;
213196
}
214197
return 0;
215198
}
@@ -235,16 +218,6 @@ namespace RTE {
235218
}
236219
}
237220

238-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
239-
240-
void AudioMan::SetSoundsVolume(double volume) {
241-
m_SoundsVolume = volume;
242-
243-
if (m_AudioEnabled) {
244-
m_SoundChannelGroup->setVolume(volume);
245-
}
246-
}
247-
248221
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
249222

250223
bool AudioMan::SetSoundAttenuation(SoundContainer *pSoundContainer, float distance) {
@@ -275,6 +248,7 @@ namespace RTE {
275248
if (!m_AudioEnabled || !pSoundContainer || !pSoundContainer->IsBeingPlayed()) {
276249
return false;
277250
}
251+
278252
if (IsInMultiplayerMode() && pSoundContainer) {
279253
RegisterSoundEvent(-1, SOUND_SET_PITCH, pSoundContainer->GetHash(), 0, pSoundContainer->GetPlayingChannels(), pSoundContainer->GetLoopSetting(), pitch, pSoundContainer->IsAffectedByGlobalPitch());
280254
}
@@ -298,9 +272,7 @@ namespace RTE {
298272

299273
void AudioMan::PlayMusic(const char *filePath, int loops, double volumeOverrideIfNotMuted) {
300274
if (m_AudioEnabled) {
301-
if (m_IsInMultiplayerMode) {
302-
RegisterMusicEvent(-1, MUSIC_PLAY, filePath, loops, 0.0, 1.0);
303-
}
275+
if (m_IsInMultiplayerMode) { RegisterMusicEvent(-1, MUSIC_PLAY, filePath, loops, 0.0, 1.0); }
304276

305277
FMOD_RESULT result = m_MusicChannelGroup->stop();
306278
if (result != FMOD_OK) {
@@ -331,7 +303,7 @@ namespace RTE {
331303
if (volumeOverrideIfNotMuted >= 0 && m_MusicVolume > 0) {
332304
result = musicChannel->setVolume(volumeOverrideIfNotMuted);
333305
if (result != FMOD_OK && (loops != 0 && loops != 1)) {
334-
g_ConsoleMan.PrintString("ERROR: Failed to set vollume override for music file: " + std::string(filePath) + ". This means it will stay at " + std::to_string(m_MusicVolume) + ": " + std::string(FMOD_ErrorString(result)));
306+
g_ConsoleMan.PrintString("ERROR: Failed to set volume override for music file: " + std::string(filePath) + ". This means it will stay at " + std::to_string(m_MusicVolume) + ": " + std::string(FMOD_ErrorString(result)));
335307
}
336308
}
337309

@@ -385,9 +357,7 @@ namespace RTE {
385357

386358
void AudioMan::StopMusic() {
387359
if (m_AudioEnabled) {
388-
if (m_IsInMultiplayerMode) {
389-
RegisterMusicEvent(-1, MUSIC_STOP, 0, 0, 0.0, 0.0);
390-
}
360+
if (m_IsInMultiplayerMode) { RegisterMusicEvent(-1, MUSIC_STOP, 0, 0, 0.0, 0.0); }
391361

392362
FMOD_RESULT result = m_MusicChannelGroup->stop();
393363
if (result != FMOD_OK) {
@@ -449,6 +419,7 @@ namespace RTE {
449419
if (!m_AudioEnabled || !pSoundContainer) {
450420
return false;
451421
}
422+
452423
priority = priority < 0 ? pSoundContainer->GetPriority() : priority;
453424
pitch = pSoundContainer->IsAffectedByGlobalPitch() ? m_GlobalPitch : pitch;
454425

Managers/AudioMan.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ namespace RTE {
155155
/// Sets the music to a specific volume. Does not affect sounds.
156156
/// </summary>
157157
/// <param name="volume">The desired volume scalar. 0.0-1.0.</param>
158-
void SetMusicVolume(double volume = 1.0);
158+
void SetMusicVolume(double volume = 1.0) { m_MusicVolume = Limit(volume, 1, 0); if (m_AudioEnabled) { m_MusicChannelGroup->setVolume(m_MusicVolume); } }
159159

160160
/// <summary>
161161
/// Sets the music to a specific volume, but it will only last until a new song is played. Useful for fading etc.
@@ -200,7 +200,7 @@ namespace RTE {
200200
/// Sets the volume of all sounds to a specific volume. Does not affect music.
201201
/// </summary>
202202
/// <param name="volume">The desired volume scalar. 0.0-1.0.</param>
203-
void SetSoundsVolume(double volume = 1.0);
203+
void SetSoundsVolume(double volume = 1.0) { m_SoundsVolume = volume; if (m_AudioEnabled) { m_SoundChannelGroup->setVolume(volume); } }
204204

205205
/// <summary>
206206
/// Sets/updates the distance attenuation for a specific SoundContainer. Will only have an effect if the sound is currently being played.
@@ -269,7 +269,7 @@ namespace RTE {
269269
/// </summary>
270270
/// <param name="filePath">The path to the sound file to play.</param>
271271
/// <returns>Returns the new sound object being played. OWNERSHIP IS TRANSFERRED!</returns>
272-
SoundContainer *PlaySound(const char *filePath) { return PlaySound(filePath, -1, 0); }
272+
SoundContainer *PlaySound(const char *filePath) { return PlaySound(filePath, 0, -1); }
273273

274274
/// <summary>
275275
/// Starts playing a certain sound file with a certain attenuation for a certain player.
@@ -278,7 +278,7 @@ namespace RTE {
278278
/// <param name="attenuation">Distance attenuation scalar: 0 = full volume, 1.0 = max distant, but not silent.</param>
279279
/// <param name="player">Which player to play the SoundContainer's sounds for, -1 means all players.</param>
280280
/// <returns></returns>
281-
SoundContainer *PlaySound(const char *filePath, float attenuation, int player) { return PlaySound(filePath, player, attenuation, 0, PRIORITY_NORMAL, -1); }
281+
SoundContainer *PlaySound(const char *filePath, float attenuation, int player) { return PlaySound(filePath, attenuation, player, 0, PRIORITY_NORMAL, -1); }
282282

283283
/// <summary>
284284
/// Starts playing a certain sound file with various configuration settings.
@@ -304,7 +304,7 @@ namespace RTE {
304304
/// <param name="priority">The priority of this sound - higher priority sounds are more likely to be heard. -1 means it'll use the SoundContainer's value. Defaults to -1.</param>
305305
/// <param name="pitch">/// The pitch to play this SoundContainer's at where 1 is unmodified frequency and each multiple of 2 is an octave up or down. Defaults to 1.</param>
306306
/// <returns>Whether or not playback of the Sound was successful.</returns>
307-
bool PlaySound(SoundContainer *pSoundContainer, float attenuation = 0.0, int player = -1, int priority = -1, double pitch = 1);
307+
bool PlaySound(SoundContainer *pSoundContainer, float attenuation = 0, int player = -1, int priority = -1, double pitch = 1);
308308

309309
/// <summary>
310310
/// Stops playing all sounds in a given SoundContainer.
@@ -424,5 +424,7 @@ namespace RTE {
424424
AudioMan(const AudioMan &reference);
425425
AudioMan & operator=(const AudioMan &rhs);
426426
};
427+
428+
std::mutex g_SoundEventsListMutex[c_MaxClients];
427429
}
428430
#endif

System/ContentFile.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "ContentFile.h"
22
#include "PresetMan.h"
3-
#include "AudioMan.h"
43

54
namespace RTE {
65

0 commit comments

Comments
 (0)