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
 
int32_t SampleRate
 
int32_t Length
 
C4SoundHandle pSample
 
C4SoundInstanceFirstInst
 
C4SoundEffectNext
 

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 ( )

Definition at line 34 of file C4SoundInstance.cpp.

References Name.

34  :
35  Instances (0),
36  pSample (0),
37  FirstInst (nullptr),
38  Next (nullptr)
39 {
40  Name[0]=0;
41 }
C4SoundHandle pSample
char Name[C4MaxSoundName+1]
C4SoundEffect * Next
C4SoundInstance * FirstInst
C4SoundEffect::~C4SoundEffect ( )

Definition at line 43 of file C4SoundInstance.cpp.

References Clear().

44 {
45  Clear();
46 }

Here is the call graph for this function:

Member Function Documentation

void C4SoundEffect::AddInst ( C4SoundInstance pInst)
protected

Definition at line 180 of file C4SoundInstance.cpp.

References FirstInst, Instances, and C4SoundInstance::pNext.

Referenced by New().

181 {
182  pInst->pNext = FirstInst;
183  FirstInst = pInst;
184  Instances++;
185 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst

Here is the caller graph for this function:

void C4SoundEffect::Clear ( )

Definition at line 48 of file C4SoundInstance.cpp.

References FirstInst, pSample, and RemoveInst().

Referenced by ~C4SoundEffect().

49 {
51 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
52  if (pSample) Mix_FreeChunk(pSample);
53 #elif AUDIO_TK == AUDIO_TK_OPENAL
54  if (pSample) alDeleteBuffers(1, &pSample);
55 #endif
56  pSample = 0;
57 }
C4SoundHandle pSample
void RemoveInst(C4SoundInstance *pInst)
C4SoundInstance * FirstInst

Here is the call graph for this function:

Here is the caller graph for this function:

void C4SoundEffect::ClearPointers ( C4Object pObj)

Definition at line 156 of file C4SoundInstance.cpp.

References FirstInst, and C4SoundInstance::pNext.

157 {
158  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
159  pInst->ClearPointers(pObj);
160 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst
void C4SoundEffect::Execute ( )

Definition at line 121 of file C4SoundInstance.cpp.

References FirstInst, C4SoundInstance::pNext, and RemoveInst().

Referenced by C4SoundSystem::Execute().

122 {
123  // check for instances that have stopped and volume changes
124  for (C4SoundInstance *pInst = FirstInst; pInst; )
125  {
126  C4SoundInstance *pNext = pInst->pNext;
127  if (!pInst->Playing())
128  RemoveInst(pInst);
129  else
130  pInst->Execute();
131  pInst = pNext;
132  }
133 }
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 51 of file C4SoundInstance.h.

References Name.

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

Definition at line 148 of file C4SoundInstance.cpp.

References FirstInst, and C4SoundInstance::pNext.

149 {
150  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
151  if (pInst->getObj() == pObj)
152  return pInst;
153  return nullptr;
154 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst
int32_t C4SoundEffect::GetStartedInstanceCount ( int32_t  iX,
int32_t  iY,
int32_t  iRad 
)

Definition at line 162 of file C4SoundInstance.cpp.

References FirstInst, and C4SoundInstance::pNext.

Referenced by C4SoundInstance::CheckStart().

163 {
164  int32_t cnt = 0;
165  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
166  if (pInst->isStarted() && pInst->getObj() && pInst->Inside(iX, iY, iRad))
167  cnt++;
168  return cnt;
169 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst

Here is the caller graph for this function:

int32_t C4SoundEffect::GetStartedInstanceCount ( )

Definition at line 171 of file C4SoundInstance.cpp.

References FirstInst, and C4SoundInstance::pNext.

172 {
173  int32_t cnt = 0;
174  for (C4SoundInstance *pInst = FirstInst; pInst; pInst = pInst->pNext)
175  if (pInst->isStarted() && pInst->Playing() && !pInst->getObj())
176  cnt++;
177  return cnt;
178 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst
bool C4SoundEffect::Load ( const char *  szFileName,
C4Group hGroup,
const char *  namespace_prefix 
)

Definition at line 59 of file C4SoundInstance.cpp.

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

Referenced by C4SoundSystem::LoadEffects().

60 {
61  // Sound check
62  if (!Config.Sound.RXSound) return false;
63  // Locate sound in file
64  StdBuf WaveBuffer;
65  if (!hGroup.LoadEntry(szFileName, &WaveBuffer)) return false;
66  // load it from mem
67  if (!Load((BYTE*)WaveBuffer.getMData(), WaveBuffer.getSize())) return false;
68  // Set name
69  if (namespace_prefix)
70  {
71  // Local sound name
72  SAppend(namespace_prefix, Name, C4MaxSoundName);
73  SAppend("::", Name, C4MaxSoundName);
74  SAppend(szFileName, Name, C4MaxSoundName);
75  }
76  else
77  {
78  // Global sound name
79  SCopy(szFileName, Name, C4MaxSoundName);
80  }
81  return true;
82 }
C4Config Config
Definition: C4Config.cpp:837
Definition: StdBuf.h:37
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
int32_t RXSound
Definition: C4Config.h:127
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:109
bool Load(const char *szFileName, C4Group &hGroup, const char *namespace_prefix)
C4ConfigSound Sound
Definition: C4Config.h:255
void * getMData()
Definition: StdBuf.h:108

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 84 of file C4SoundInstance.cpp.

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

85 {
86  // Sound check
87  if (!Config.Sound.RXSound) return false;
88 
89  SoundInfo info;
90  int32_t options = 0;
91  if (fRaw)
92  options |= SoundLoader::OPTION_Raw;
93  for (SoundLoader* loader = SoundLoader::first_loader; loader; loader = loader->next)
94  {
95  if (loader->ReadInfo(&info, pData, iDataLen))
96  {
97  if (info.final_handle)
98  {
99  // loader supplied the handle specific to the sound system used; just assign to pSample
100  pSample = info.final_handle;
101  }
102  else
103  {
104 #if AUDIO_TK == AUDIO_TK_OPENAL
106  alGenBuffers(1, &pSample);
107  alBufferData(pSample, info.format, &info.sound_data[0], info.sound_data.size(), info.sample_rate);
108 #else
109  Log("SoundLoader does not provide a ready-made handle");
110 #endif
111  }
112  SampleRate = info.sample_rate;
113  Length = info.sample_length*1000;
114  break;
115  }
116  }
117  *Name = '\0';
118  return !!pSample;
119 }
C4Config Config
Definition: C4Config.cpp:837
C4SoundHandle pSample
int32_t RXSound
Definition: C4Config.h:127
int32_t SampleRate
void SelectContext()
char Name[C4MaxSoundName+1]
std::vector< BYTE > sound_data
bool Log(const char *szMessage)
Definition: C4Log.cpp:195
C4ConfigSound Sound
Definition: C4Config.h:255
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 135 of file C4SoundInstance.cpp.

References AddInst(), C4MaxSoundInstances, C4SoundInstance, C4SoundInstance::Create(), and Instances.

Referenced by C4SoundSystem::NewEffect().

136 {
137  // check: too many instances?
138  if (!fLoop && Instances >= C4MaxSoundInstances) return nullptr;
139  // create & init sound instance
140  C4SoundInstance *pInst = new C4SoundInstance();
141  if (!pInst->Create(this, fLoop, iVolume, pObj, 0, iCustomFalloffDistance, iPitch, modifier)) { delete pInst; return nullptr; }
142  // add to list
143  AddInst(pInst);
144  // return
145  return pInst;
146 }
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 186 of file C4SoundInstance.cpp.

References FirstInst, Instances, and C4SoundInstance::pNext.

Referenced by Clear(), and Execute().

187 {
188  if (pInst == FirstInst)
189  FirstInst = pInst->pNext;
190  else
191  {
192  C4SoundInstance *pPos = FirstInst;
193  while (pPos && pPos->pNext != pInst) pPos = pPos->pNext;
194  if (pPos)
195  pPos->pNext = pInst->pNext;
196  }
197  delete pInst;
198  Instances--;
199 }
C4SoundInstance * pNext
C4SoundInstance * FirstInst

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class C4SoundInstance
friend

Definition at line 29 of file C4SoundInstance.h.

Referenced by New().

Member Data Documentation

C4SoundInstance* C4SoundEffect::FirstInst
int32_t C4SoundEffect::Instances

Definition at line 35 of file C4SoundInstance.h.

Referenced by AddInst(), New(), and RemoveInst().

int32_t C4SoundEffect::Length

Definition at line 36 of file C4SoundInstance.h.

Referenced by C4SoundInstance::CheckStart(), and Load().

char C4SoundEffect::Name[C4MaxSoundName+1]
C4SoundHandle C4SoundEffect::pSample

Definition at line 37 of file C4SoundInstance.h.

Referenced by Clear(), Load(), and C4SoundInstance::Start().

int32_t C4SoundEffect::SampleRate

Definition at line 36 of file C4SoundInstance.h.

Referenced by Load().


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