OpenClonk
C4SoundSystem Class Reference

#include <C4SoundSystem.h>

Collaboration diagram for C4SoundSystem:
[legend]

Public Member Functions

 C4SoundSystem ()
 
 ~C4SoundSystem ()
 
void Clear ()
 
void Execute ()
 
int32_t LoadEffects (C4Group &hGroup, const char *namespace_prefix, bool group_is_root)
 
C4SoundInstanceNewEffect (const char *szSound, bool fLoop=false, int32_t iVolume=100, C4Object *pObj=nullptr, int32_t iCustomFalloffDistance=0, int32_t iPitch=0, C4SoundModifier *modifier=nullptr)
 
C4SoundInstanceFindInstance (const char *szSound, C4Object *pObj)
 
C4SoundInstanceGetFirstInstance () const
 
C4SoundInstanceGetNextInstance (C4SoundInstance *prev) const
 
bool Init ()
 
void ClearPointers (C4Object *pObj)
 
C4SoundEffectGetFirstSound () const
 

Public Attributes

C4SoundModifierList Modifiers
 

Protected Member Functions

void ClearEffects ()
 
C4SoundEffectGetEffect (const char *szSound)
 
int32_t RemoveEffect (const char *szFilename)
 

Protected Attributes

C4Group SoundFile
 
C4SoundEffectFirstSound {nullptr}
 
bool initialized {false}
 

Detailed Description

Definition at line 38 of file C4SoundSystem.h.

Constructor & Destructor Documentation

◆ C4SoundSystem()

C4SoundSystem::C4SoundSystem ( )
default

◆ ~C4SoundSystem()

C4SoundSystem::~C4SoundSystem ( )
default

Member Function Documentation

◆ Clear()

void C4SoundSystem::Clear ( )

Definition at line 57 of file C4SoundSystem.cpp.

58 {
59  initialized = false;
60  ClearEffects();
61  Modifiers.Clear();
62  // Close sound file
63  SoundFile.Close();
64 }
bool Close()
Definition: C4Group.cpp:971
C4Group SoundFile
Definition: C4SoundSystem.h:56
C4SoundModifierList Modifiers
Definition: C4SoundSystem.h:54

References C4SoundModifierList::Clear(), ClearEffects(), C4Group::Close(), initialized, Modifiers, and SoundFile.

Referenced by C4Game::Clear(), C4Application::Clear(), and C4MainMenu::MenuCommand().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearEffects()

void C4SoundSystem::ClearEffects ( )
protected

Definition at line 66 of file C4SoundSystem.cpp.

67 {
68  // Clear sound bank
69  C4SoundEffect *csfx,*next;
70  for (csfx=FirstSound; csfx; csfx=next)
71  {
72  next=csfx->Next;
73  delete csfx;
74  }
75  FirstSound=nullptr;
76 }
C4SoundEffect * Next
C4SoundEffect * FirstSound
Definition: C4SoundSystem.h:57

References FirstSound, and C4SoundEffect::Next.

Referenced by Clear(), and Init().

Here is the caller graph for this function:

◆ ClearPointers()

void C4SoundSystem::ClearPointers ( C4Object pObj)

Definition at line 239 of file C4SoundSystem.cpp.

240 {
241  for (C4SoundEffect *pEff=FirstSound; pEff; pEff=pEff->Next)
242  pEff->ClearPointers(pObj);
243 }

References FirstSound, and C4SoundEffect::Next.

Referenced by C4Game::ClearPointers().

Here is the caller graph for this function:

◆ Execute()

void C4SoundSystem::Execute ( )

Definition at line 78 of file C4SoundSystem.cpp.

79 {
80 #if AUDIO_TK == AUDIO_TK_OPENAL
82 #endif
83  C4SoundEffect *csfx;
84  for (csfx=FirstSound; csfx; csfx=csfx->Next)
85  {
86  // Instance removal check
87  csfx->Execute();
88  }
89 }
C4Application Application
Definition: C4Globals.cpp:44
C4MusicSystem MusicSystem
Definition: C4Application.h:41
void SelectContext()

References Application, C4SoundEffect::Execute(), FirstSound, C4Application::MusicSystem, C4SoundEffect::Next, and C4MusicSystem::SelectContext().

Referenced by C4Application::GameTick().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindInstance()

C4SoundInstance * C4SoundSystem::FindInstance ( const char *  szSound,
C4Object pObj 
)

Definition at line 140 of file C4SoundSystem.cpp.

141 {
142  char szName[C4MaxSoundName+2+1];
143  // Evaluate sound name (see GetEffect)
144  SCopy(szSndName,szName,C4MaxSoundName);
145  DefaultExtension(szName,"*");
146  // Find an effect with a matching instance
147  for (C4SoundEffect *csfx = FirstSound; csfx; csfx = csfx->Next)
148  if (WildcardMatch(szName, csfx->Name))
149  {
150  C4SoundInstance *pInst = csfx->GetInstance(pObj);
151  if (pInst) return pInst;
152  }
153  return nullptr;
154 }
const int32_t C4MaxSoundName
Definition: C4SoundSystem.h:27
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:396
void DefaultExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:271

References C4MaxSoundName, DefaultExtension(), FirstSound, C4SoundEffect::Next, SCopy(), and WildcardMatch().

Referenced by GetSoundInstance(), SoundLevel(), SoundPan(), SoundPitch(), and StopSoundEffect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetEffect()

C4SoundEffect * C4SoundSystem::GetEffect ( const char *  szSound)
protected

Definition at line 91 of file C4SoundSystem.cpp.

92 {
93  // Remember wildcards before adding .* extension - if there are 2 versions with different file extensions, play the last added
94  bool bRandomSound = IsWildcardString(szSndName);
95  // Evaluate sound name
96  char szName[C4MaxSoundName+2+1];
97  SCopy(szSndName,szName,C4MaxSoundName);
98  // Any extension accepted
99  DefaultExtension(szName,"*");
100  // Play nth Sound. Standard: 1
101  int32_t iNumber = 1;
102  // Sound with a wildcard: determine number of available matches
103  if (bRandomSound)
104  {
105  iNumber = 0;
106  // Count matching sounds
107  for (C4SoundEffect *pSfx=FirstSound; pSfx; pSfx=pSfx->Next)
108  if (WildcardMatch(szName,pSfx->Name))
109  ++iNumber;
110  // Nothing found? Abort
111  if(iNumber == 0)
112  return nullptr;
113  iNumber=UnsyncedRandom(iNumber)+1;
114  }
115  // Find requested sound effect in bank
116  C4SoundEffect *pSfx;
117  for (pSfx=FirstSound; pSfx; pSfx=pSfx->Next)
118  if (WildcardMatch(szName,pSfx->Name))
119  if(!--iNumber)
120  break;
121  return pSfx; // Is still nullptr if nothing is found
122 }
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
bool IsWildcardString(const char *szString)
Definition: StdFile.cpp:363
char Name[C4MaxSoundName+1]

References C4MaxSoundName, DefaultExtension(), FirstSound, IsWildcardString(), C4SoundEffect::Name, C4SoundEffect::Next, SCopy(), UnsyncedRandom(), and WildcardMatch().

Referenced by NewEffect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFirstInstance()

C4SoundInstance * C4SoundSystem::GetFirstInstance ( ) const

Definition at line 245 of file C4SoundSystem.cpp.

246 {
247  // Return by searching through effect linked list.
248  for (C4SoundEffect *pSfx = FirstSound; pSfx; pSfx = pSfx->Next)
249  if (pSfx->FirstInst) return pSfx->FirstInst;
250  return nullptr;
251 }

References FirstSound, and C4SoundEffect::Next.

Referenced by C4SoundModifierList::SetGlobalModifier().

Here is the caller graph for this function:

◆ GetFirstSound()

C4SoundEffect* C4SoundSystem::GetFirstSound ( ) const
inline

Definition at line 52 of file C4SoundSystem.h.

52 { return FirstSound; }

References FirstSound.

◆ GetNextInstance()

C4SoundInstance * C4SoundSystem::GetNextInstance ( C4SoundInstance prev) const

Definition at line 253 of file C4SoundSystem.cpp.

254 {
255  // Return by searching through instance linked list and parent linked list of effects
256  assert(prev && prev->pEffect);
257  if (prev->pNext) return prev->pNext;
258  for (C4SoundEffect *pSfx = prev->pEffect->Next; pSfx; pSfx = pSfx->Next)
259  if (pSfx->FirstInst) return pSfx->FirstInst;
260  return nullptr;
261 }
C4SoundInstance * pNext
C4SoundEffect * pEffect

References C4SoundEffect::Next, C4SoundInstance::pEffect, and C4SoundInstance::pNext.

Referenced by C4SoundModifierList::SetGlobalModifier().

Here is the caller graph for this function:

◆ Init()

bool C4SoundSystem::Init ( )

Definition at line 35 of file C4SoundSystem.cpp.

36 {
39  return false;
40 
41  // Might be reinitialisation
42  ClearEffects();
43  // (re)init EFX
44  Modifiers.Init();
45  // Open sound file
46  if (!SoundFile.IsOpen())
47  if (!Reloc.Open(SoundFile, C4CFN_Sound)) return false;
48  // Load static sound from Sound.ocg
49  LoadEffects(SoundFile, nullptr, false);
50 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
51  Mix_AllocateChannels(C4MaxSoundInstances);
52 #endif
53  initialized = true;
54  return true;
55 }
#define C4CFN_Sound
Definition: C4Components.h:26
C4Reloc Reloc
Definition: C4Reloc.cpp:21
const int32_t C4MaxSoundInstances
Definition: C4SoundSystem.h:28
bool IsOpen() const
Definition: C4Group.cpp:2373
bool InitializeMOD()
bool Open(C4Group &group, const char *filename) const
Definition: C4Reloc.cpp:156
int32_t LoadEffects(C4Group &hGroup, const char *namespace_prefix, bool group_is_root)

References Application, C4CFN_Sound, C4MaxSoundInstances, ClearEffects(), C4SoundModifierList::Init(), initialized, C4MusicSystem::InitializeMOD(), C4Group::IsOpen(), LoadEffects(), Modifiers, C4MusicSystem::MODInitialized, C4Application::MusicSystem, C4Reloc::Open(), Reloc, and SoundFile.

Referenced by C4MainMenu::MenuCommand(), and C4Application::PreInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LoadEffects()

int32_t C4SoundSystem::LoadEffects ( C4Group hGroup,
const char *  namespace_prefix,
bool  group_is_root 
)

Definition at line 158 of file C4SoundSystem.cpp.

159 {
160  // Local definition sounds: If there is a Sound.ocg in the group, load the sound from there
161  if(group_is_root && hGroup.FindEntry(C4CFN_Sound))
162  {
163  C4Group g;
164  g.OpenAsChild(&hGroup, C4CFN_Sound, false, false);
165  return LoadEffects(g, namespace_prefix, false);
166  }
167  int32_t iNum=0;
168  char szFilename[_MAX_FNAME_LEN];
169  char szFileType[_MAX_FNAME_LEN];
170  C4SoundEffect *nsfx;
171  // Process segmented list of file types
172  for (int32_t i = 0; SCopySegment(C4CFN_SoundFiles, i, szFileType, '|', _MAX_FNAME); i++)
173  {
174  // Search all sound files in group
175  hGroup.ResetSearch();
176  while (hGroup.FindNextEntry(szFileType, szFilename))
177  // Create and load effect
178  if ((nsfx = new C4SoundEffect))
179  {
180  if (nsfx->Load(szFilename, hGroup, namespace_prefix))
181  {
182  // Overload same name effects
183  RemoveEffect(nsfx->Name);
184  // Add effect
185  nsfx->Next=FirstSound;
186  FirstSound=nsfx;
187  iNum++;
188  }
189  else
190  delete nsfx;
191  }
192  }
193  // Load subgroups from Sound.ocg and other subgroups
194  if (!group_is_root)
195  {
196  hGroup.ResetSearch();
197  while (hGroup.FindNextEntry(C4CFN_SoundSubgroups, szFilename))
198  {
199  // Load from subgroup as a sub-namespace
200  // get namespace name
201  StdStrBuf sub_namespace;
202  if (namespace_prefix)
203  {
204  sub_namespace.Copy(namespace_prefix);
205  sub_namespace.Append("::");
206  }
207  sub_namespace.Append(szFilename, strlen(szFilename) - strlen(C4CFN_SoundSubgroups) + 1);
208  // load from child group
209  C4Group subgroup;
210  if (subgroup.OpenAsChild(&hGroup, szFilename, false, false))
211  {
212  iNum += LoadEffects(subgroup, sub_namespace.getData(), false);
213  }
214  }
215  }
216  return iNum;
217 }
#define C4CFN_SoundFiles
Definition: C4Components.h:172
#define C4CFN_SoundSubgroups
Definition: C4Components.h:27
#define _MAX_FNAME
#define _MAX_FNAME_LEN
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:279
bool FindNextEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr, bool start_at_filename=false)
Definition: C4Group.cpp:2217
bool OpenAsChild(C4Group *mother, const char *entry_name, bool is_exclusive=false, bool do_create=false)
Definition: C4Group.cpp:1952
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1316
bool FindEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr)
Definition: C4Group.cpp:2211
bool Load(const char *szFileName, C4Group &hGroup, const char *namespace_prefix)
int32_t RemoveEffect(const char *szFilename)
const char * getData() const
Definition: StdBuf.h:442
void Copy()
Definition: StdBuf.h:467
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519

References _MAX_FNAME, _MAX_FNAME_LEN, StdStrBuf::Append(), C4CFN_Sound, C4CFN_SoundFiles, C4CFN_SoundSubgroups, StdStrBuf::Copy(), C4Group::FindEntry(), C4Group::FindNextEntry(), FirstSound, StdStrBuf::getData(), C4SoundEffect::Load(), C4SoundEffect::Name, C4SoundEffect::Next, C4Group::OpenAsChild(), RemoveEffect(), C4Group::ResetSearch(), and SCopySegment().

Referenced by Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ NewEffect()

C4SoundInstance * C4SoundSystem::NewEffect ( const char *  szSound,
bool  fLoop = false,
int32_t  iVolume = 100,
C4Object pObj = nullptr,
int32_t  iCustomFalloffDistance = 0,
int32_t  iPitch = 0,
C4SoundModifier modifier = nullptr 
)

Definition at line 124 of file C4SoundSystem.cpp.

125 {
126  // Sound not active
127  if (!initialized || !Config.Sound.RXSound) return nullptr;
128  // Get sound
129  C4SoundEffect *csfx;
130  if (!(csfx = GetEffect(szSndName)))
131  {
132  // Warn about missing or incorrectly spelled sound to allow finding mistakes earlier.
133  DebugLogF("Warning: could not find sound matching '%s'", szSndName);
134  return nullptr;
135  }
136  // Play
137  return csfx->New(fLoop, iVolume, pObj, iCustomFalloffDistance, iPitch, modifier);
138 }
C4Config Config
Definition: C4Config.cpp:930
bool DebugLogF(const char *strMessage ...)
Definition: C4Log.cpp:290
C4ConfigSound Sound
Definition: C4Config.h:258
int32_t RXSound
Definition: C4Config.h:126
C4SoundInstance * New(bool fLoop=false, int32_t iVolume=100, C4Object *pObj=nullptr, int32_t iCustomFalloffDistance=0, int32_t iPitch=0, C4SoundModifier *modifier=nullptr)
C4SoundEffect * GetEffect(const char *szSound)

References Config, DebugLogF(), GetEffect(), initialized, C4SoundEffect::New(), C4ConfigSound::RXSound, and C4Config::Sound.

Referenced by StartSoundEffect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveEffect()

int32_t C4SoundSystem::RemoveEffect ( const char *  szFilename)
protected

Definition at line 219 of file C4SoundSystem.cpp.

220 {
221  int32_t iResult=0;
222  C4SoundEffect *pNext,*pPrev=nullptr;
223  for (C4SoundEffect *pSfx=FirstSound; pSfx; pSfx=pNext)
224  {
225  pNext=pSfx->Next;
226  if (WildcardMatch(szFilename,pSfx->Name))
227  {
228  delete pSfx;
229  if (pPrev) pPrev->Next=pNext;
230  else FirstSound=pNext;
231  iResult++;
232  }
233  else
234  pPrev=pSfx;
235  }
236  return iResult;
237 }
int iResult
Definition: C4GroupMain.cpp:40

References FirstSound, iResult, C4SoundEffect::Next, and WildcardMatch().

Referenced by LoadEffects().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ FirstSound

C4SoundEffect* C4SoundSystem::FirstSound {nullptr}
protected

◆ initialized

bool C4SoundSystem::initialized {false}
protected

Definition at line 58 of file C4SoundSystem.h.

Referenced by Clear(), Init(), and NewEffect().

◆ Modifiers

◆ SoundFile

C4Group C4SoundSystem::SoundFile
protected

Definition at line 56 of file C4SoundSystem.h.

Referenced by Clear(), and Init().


The documentation for this class was generated from the following files: