OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 25 of file C4SoundInstance.h.

Constructor & Destructor Documentation

C4SoundEffect::C4SoundEffect ( )

Definition at line 32 of file C4SoundInstance.cpp.

33 {
34  Name[0]=0;
35 }
char Name[C4MaxSoundName+1]
C4SoundEffect::~C4SoundEffect ( )

Definition at line 37 of file C4SoundInstance.cpp.

38 {
39  Clear();
40 }

Member Function Documentation

void C4SoundEffect::AddInst ( C4SoundInstance pInst)
protected

Definition at line 174 of file C4SoundInstance.cpp.

References C4SoundInstance::pNext.

175 {
176  pInst->pNext = FirstInst;
177  FirstInst = pInst;
178  Instances++;
179 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst
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 }
C4SoundHandle pSample
void RemoveInst(C4SoundInstance *pInst)
C4SoundInstance * FirstInst
void C4SoundEffect::ClearPointers ( C4Object pObj)

Definition at line 150 of file C4SoundInstance.cpp.

References C4SoundInstance::pNext.

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

Definition at line 115 of file C4SoundInstance.cpp.

References C4SoundInstance::Execute(), and C4SoundInstance::pNext.

Referenced by C4SoundSystem::Execute().

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 }
C4SoundInstance * pNext
void RemoveInst(C4SoundInstance *pInst)
C4SoundInstance * FirstInst

Here is the call graph for this function:

Here is the caller graph for this function:

const char* C4SoundEffect::GetFullName ( ) const
inline

Definition at line 49 of file C4SoundInstance.h.

References Name.

49 { return Name; }; // return full name including the path prefix
char Name[C4MaxSoundName+1]
C4SoundInstance * C4SoundEffect::GetInstance ( C4Object pObj)

Definition at line 142 of file C4SoundInstance.cpp.

References C4SoundInstance::pNext.

143 {
144  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
145  if (pInst->getObj() == pObj)
146  return pInst;
147  return nullptr;
148 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst
int32_t C4SoundEffect::GetStartedInstanceCount ( int32_t  iX,
int32_t  iY,
int32_t  iRad 
)

Definition at line 156 of file C4SoundInstance.cpp.

References C4SoundInstance::pNext.

Referenced by C4SoundInstance::CheckStart().

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 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst

Here is the caller graph for this function:

int32_t C4SoundEffect::GetStartedInstanceCount ( )

Definition at line 165 of file C4SoundInstance.cpp.

References C4SoundInstance::pNext.

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 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst
bool C4SoundEffect::Load ( const char *  szFileName,
C4Group hGroup,
const char *  namespace_prefix 
)

Definition at line 53 of file C4SoundInstance.cpp.

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

Referenced by C4SoundSystem::LoadEffects().

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 }
C4Config Config
Definition: C4Config.cpp:833
Definition: StdBuf.h:29
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:235
int32_t RXSound
Definition: C4Config.h:126
bool LoadEntry(const char *szEntryName, char **lpbpBuf, size_t *ipSize=nullptr, int iAppendZeros=0)
Definition: C4Group.cpp:1893
uint8_t BYTE
char Name[C4MaxSoundName+1]
const int32_t C4MaxSoundName
Definition: C4SoundSystem.h:27
size_t getSize() const
Definition: StdBuf.h:101
bool Load(const char *szFileName, C4Group &hGroup, const char *namespace_prefix)
C4ConfigSound Sound
Definition: C4Config.h:254
void * getMData()
Definition: StdBuf.h:100

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 78 of file C4SoundInstance.cpp.

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.

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:833
C4SoundHandle pSample
int32_t RXSound
Definition: C4Config.h:126
int32_t SampleRate
void SelectContext()
char Name[C4MaxSoundName+1]
std::vector< BYTE > sound_data
bool Log(const char *szMessage)
Definition: C4Log.cpp:192
C4ConfigSound Sound
Definition: C4Config.h:254
C4Application Application
Definition: C4Globals.cpp:44
C4MusicSystem MusicSystem
Definition: C4Application.h:41

Here is the call graph for this function:

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.

References C4MaxSoundInstances, and C4SoundInstance::Create().

Referenced by C4SoundSystem::NewEffect().

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 }
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)
friend class C4SoundInstance
void AddInst(C4SoundInstance *pInst)
const int32_t C4MaxSoundInstances
Definition: C4SoundSystem.h:28

Here is the call graph for this function:

Here is the caller graph for this function:

void C4SoundEffect::RemoveInst ( C4SoundInstance pInst)
protected

Definition at line 180 of file C4SoundInstance.cpp.

References C4SoundInstance::pNext.

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 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst

Friends And Related Function Documentation

friend class C4SoundInstance
friend

Definition at line 27 of file C4SoundInstance.h.

Member Data Documentation

C4SoundInstance* C4SoundEffect::FirstInst {nullptr}

Definition at line 36 of file C4SoundInstance.h.

int32_t C4SoundEffect::Instances {0}

Definition at line 33 of file C4SoundInstance.h.

int32_t C4SoundEffect::Length

Definition at line 34 of file C4SoundInstance.h.

Referenced by C4SoundInstance::CheckStart().

char C4SoundEffect::Name[C4MaxSoundName+1]
C4SoundHandle C4SoundEffect::pSample {0}

Definition at line 35 of file C4SoundInstance.h.

Referenced by C4SoundInstance::Start().

int32_t C4SoundEffect::SampleRate

Definition at line 34 of file C4SoundInstance.h.


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