OpenClonk
C4SoundEffect Class Reference

#include <C4SoundInstance.h>

Collaboration diagram for C4SoundEffect:
[legend]

Public Member Functions

 C4SoundEffect ()
 
 ~C4SoundEffect ()
 
void Clear ()
 
bool Load (const char *szFileName, C4Group &hGroup, const char *namespace_prefix)
 
bool Load (BYTE *pData, size_t iDataLen, bool fRaw=false)
 
void Execute ()
 
C4SoundInstanceNew (bool fLoop=false, int32_t iVolume=100, C4Object *pObj=nullptr, int32_t iCustomFalloffDistance=0, int32_t iPitch=0, C4SoundModifier *modifier=nullptr)
 
C4SoundInstanceGetInstance (C4Object *pObj)
 
void ClearPointers (C4Object *pObj)
 
int32_t GetStartedInstanceCount (int32_t iX, int32_t iY, int32_t iRad)
 
int32_t GetStartedInstanceCount ()
 
const char * GetFullName () const
 

Public Attributes

char Name [C4MaxSoundName+1]
 
int32_t Instances {0}
 
int32_t SampleRate
 
int32_t Length
 
C4SoundHandle pSample {0}
 
C4SoundInstanceFirstInst {nullptr}
 
C4SoundEffectNext {nullptr}
 

Protected Member Functions

void AddInst (C4SoundInstance *pInst)
 
void RemoveInst (C4SoundInstance *pInst)
 

Friends

class C4SoundInstance
 

Detailed Description

Definition at line 27 of file C4SoundInstance.h.

Constructor & Destructor Documentation

◆ C4SoundEffect()

C4SoundEffect::C4SoundEffect ( )

Definition at line 32 of file C4SoundInstance.cpp.

33 {
34  Name[0]=0;
35 }
char Name[C4MaxSoundName+1]

◆ ~C4SoundEffect()

C4SoundEffect::~C4SoundEffect ( )

Definition at line 37 of file C4SoundInstance.cpp.

38 {
39  Clear();
40 }

Member Function Documentation

◆ AddInst()

void C4SoundEffect::AddInst ( C4SoundInstance pInst)
protected

Definition at line 174 of file C4SoundInstance.cpp.

175 {
176  pInst->pNext = FirstInst;
177  FirstInst = pInst;
178  Instances++;
179 }
C4SoundInstance * FirstInst
C4SoundInstance * pNext

References C4SoundInstance::pNext.

◆ Clear()

void C4SoundEffect::Clear ( )

Definition at line 42 of file C4SoundInstance.cpp.

43 {
45 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
46  if (pSample) Mix_FreeChunk(pSample);
47 #elif AUDIO_TK == AUDIO_TK_OPENAL
48  if (pSample) alDeleteBuffers(1, &pSample);
49 #endif
50  pSample = 0;
51 }
void RemoveInst(C4SoundInstance *pInst)
C4SoundHandle pSample

◆ ClearPointers()

void C4SoundEffect::ClearPointers ( C4Object pObj)

Definition at line 150 of file C4SoundInstance.cpp.

151 {
152  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
153  pInst->ClearPointers(pObj);
154 }

References C4SoundInstance::pNext.

◆ Execute()

void C4SoundEffect::Execute ( )

Definition at line 115 of file C4SoundInstance.cpp.

116 {
117  // check for instances that have stopped and volume changes
118  for (C4SoundInstance *pInst = FirstInst; pInst; )
119  {
120  C4SoundInstance *pNext = pInst->pNext;
121  if (!pInst->Playing())
122  RemoveInst(pInst);
123  else
124  pInst->Execute();
125  pInst = pNext;
126  }
127 }

References C4SoundInstance::pNext.

Referenced by C4SoundSystem::Execute().

Here is the caller graph for this function:

◆ GetFullName()

const char* C4SoundEffect::GetFullName ( ) const
inline

Definition at line 51 of file C4SoundInstance.h.

51 { return Name; }; // return full name including the path prefix

References Name.

◆ GetInstance()

C4SoundInstance * C4SoundEffect::GetInstance ( C4Object pObj)

Definition at line 142 of file C4SoundInstance.cpp.

143 {
144  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
145  if (pInst->getObj() == pObj)
146  return pInst;
147  return nullptr;
148 }

References C4SoundInstance::pNext.

◆ GetStartedInstanceCount() [1/2]

int32_t C4SoundEffect::GetStartedInstanceCount ( )

Definition at line 165 of file C4SoundInstance.cpp.

166 {
167  int32_t cnt = 0;
168  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
169  if (pInst->isStarted() && pInst->Playing() && !pInst->getObj())
170  cnt++;
171  return cnt;
172 }

References C4SoundInstance::pNext.

◆ GetStartedInstanceCount() [2/2]

int32_t C4SoundEffect::GetStartedInstanceCount ( int32_t  iX,
int32_t  iY,
int32_t  iRad 
)

Definition at line 156 of file C4SoundInstance.cpp.

157 {
158  int32_t cnt = 0;
159  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
160  if (pInst->isStarted() && pInst->getObj() && pInst->Inside(iX, iY, iRad))
161  cnt++;
162  return cnt;
163 }

References C4SoundInstance::pNext.

Referenced by C4SoundInstance::CheckStart().

Here is the caller graph for this function:

◆ Load() [1/2]

bool C4SoundEffect::Load ( BYTE pData,
size_t  iDataLen,
bool  fRaw = false 
)

Definition at line 78 of file C4SoundInstance.cpp.

79 {
80  // Sound check
81  if (!Config.Sound.RXSound) return false;
82 
83  SoundInfo info;
84  int32_t options = 0;
85  if (fRaw)
86  options |= SoundLoader::OPTION_Raw;
87  for (SoundLoader* loader = SoundLoader::first_loader; loader; loader = loader->next)
88  {
89  if (loader->ReadInfo(&info, pData, iDataLen))
90  {
91  if (info.final_handle)
92  {
93  // loader supplied the handle specific to the sound system used; just assign to pSample
94  pSample = info.final_handle;
95  }
96  else
97  {
98 #if AUDIO_TK == AUDIO_TK_OPENAL
100  alGenBuffers(1, &pSample);
101  alBufferData(pSample, info.format, &info.sound_data[0], info.sound_data.size(), info.sample_rate);
102 #else
103  Log("SoundLoader does not provide a ready-made handle");
104 #endif
105  }
106  SampleRate = info.sample_rate;
107  Length = info.sample_length*1000;
108  break;
109  }
110  }
111  *Name = '\0';
112  return !!pSample;
113 }
C4Config Config
Definition: C4Config.cpp:930
C4Application Application
Definition: C4Globals.cpp:44
bool Log(const char *szMessage)
Definition: C4Log.cpp:204
C4MusicSystem MusicSystem
Definition: C4Application.h:41
C4ConfigSound Sound
Definition: C4Config.h:258
int32_t RXSound
Definition: C4Config.h:126
void SelectContext()
int32_t SampleRate
std::vector< BYTE > sound_data

References Application, Config, C4SoundLoaders::SoundInfo::final_handle, C4SoundLoaders::SoundLoader::first_loader, C4SoundLoaders::SoundInfo::format, Log(), C4Application::MusicSystem, C4SoundLoaders::SoundLoader::next, C4SoundLoaders::SoundLoader::OPTION_Raw, C4ConfigSound::RXSound, C4SoundLoaders::SoundInfo::sample_length, C4SoundLoaders::SoundInfo::sample_rate, C4MusicSystem::SelectContext(), C4Config::Sound, and C4SoundLoaders::SoundInfo::sound_data.

Here is the call graph for this function:

◆ Load() [2/2]

bool C4SoundEffect::Load ( const char *  szFileName,
C4Group hGroup,
const char *  namespace_prefix 
)

Definition at line 53 of file C4SoundInstance.cpp.

54 {
55  // Sound check
56  if (!Config.Sound.RXSound) return false;
57  // Locate sound in file
58  StdBuf WaveBuffer;
59  if (!hGroup.LoadEntry(szFileName, &WaveBuffer)) return false;
60  // load it from mem
61  if (!Load((BYTE*)WaveBuffer.getMData(), WaveBuffer.getSize())) return false;
62  // Set name
63  if (namespace_prefix)
64  {
65  // Local sound name
66  SAppend(namespace_prefix, Name, C4MaxSoundName);
67  SAppend("::", Name, C4MaxSoundName);
68  SAppend(szFileName, Name, C4MaxSoundName);
69  }
70  else
71  {
72  // Global sound name
73  SCopy(szFileName, Name, C4MaxSoundName);
74  }
75  return true;
76 }
const int32_t C4MaxSoundName
Definition: C4SoundSystem.h:27
uint8_t BYTE
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:263
bool LoadEntry(const char *entry_name, char **buffer, size_t *size_info=nullptr, int zeros_to_append=0)
Definition: C4Group.cpp:2375
bool Load(const char *szFileName, C4Group &hGroup, const char *namespace_prefix)
Definition: StdBuf.h:30
size_t getSize() const
Definition: StdBuf.h:101
void * getMData()
Definition: StdBuf.h:100

References C4MaxSoundName, Config, StdBuf::getMData(), StdBuf::getSize(), C4Group::LoadEntry(), C4ConfigSound::RXSound, SAppend(), SCopy(), and C4Config::Sound.

Referenced by C4SoundSystem::LoadEffects().

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

◆ New()

C4SoundInstance * C4SoundEffect::New ( bool  fLoop = false,
int32_t  iVolume = 100,
C4Object pObj = nullptr,
int32_t  iCustomFalloffDistance = 0,
int32_t  iPitch = 0,
C4SoundModifier modifier = nullptr 
)

Definition at line 129 of file C4SoundInstance.cpp.

130 {
131  // check: too many instances?
132  if (!fLoop && Instances >= C4MaxSoundInstances) return nullptr;
133  // create & init sound instance
134  C4SoundInstance *pInst = new C4SoundInstance();
135  if (!pInst->Create(this, fLoop, iVolume, pObj, 0, iCustomFalloffDistance, iPitch, modifier)) { delete pInst; return nullptr; }
136  // add to list
137  AddInst(pInst);
138  // return
139  return pInst;
140 }
const int32_t C4MaxSoundInstances
Definition: C4SoundSystem.h:28
void AddInst(C4SoundInstance *pInst)
friend class C4SoundInstance
bool Create(C4SoundEffect *pEffect, bool fLoop=false, int32_t iVolume=100, C4Object *pObj=nullptr, int32_t iNearInstanceMax=0, int32_t iFalloffDistance=0, int32_t inPitch=0, C4SoundModifier *modifier=nullptr)

References C4MaxSoundInstances, and C4SoundInstance::Create().

Referenced by C4SoundSystem::NewEffect().

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

◆ RemoveInst()

void C4SoundEffect::RemoveInst ( C4SoundInstance pInst)
protected

Definition at line 180 of file C4SoundInstance.cpp.

181 {
182  if (pInst == FirstInst)
183  FirstInst = pInst->pNext;
184  else
185  {
186  C4SoundInstance *pPos = FirstInst;
187  while (pPos && pPos->pNext != pInst) pPos = pPos->pNext;
188  if (pPos)
189  pPos->pNext = pInst->pNext;
190  }
191  delete pInst;
192  Instances--;
193 }

References C4SoundInstance::pNext.

Friends And Related Function Documentation

◆ C4SoundInstance

friend class C4SoundInstance
friend

Definition at line 29 of file C4SoundInstance.h.

Member Data Documentation

◆ FirstInst

C4SoundInstance* C4SoundEffect::FirstInst {nullptr}

Definition at line 38 of file C4SoundInstance.h.

◆ Instances

int32_t C4SoundEffect::Instances {0}

Definition at line 35 of file C4SoundInstance.h.

◆ Length

int32_t C4SoundEffect::Length

Definition at line 36 of file C4SoundInstance.h.

Referenced by C4SoundInstance::CheckStart().

◆ Name

char C4SoundEffect::Name[C4MaxSoundName+1]

◆ Next

◆ pSample

C4SoundHandle C4SoundEffect::pSample {0}

Definition at line 37 of file C4SoundInstance.h.

Referenced by C4SoundInstance::Start().

◆ SampleRate

int32_t C4SoundEffect::SampleRate

Definition at line 36 of file C4SoundInstance.h.


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