Incremental Engine  1.0.6
A 2D Game Engine to create Idle Games
IncrementalEngine::SoundEngine Class Reference

#include <SoundEngine.h>

Classes

struct  TInfoSource
 

Public Member Functions

 SoundEngine ()
 
virtual ~SoundEngine ()
 
void Reset ()
 
void Pause ()
 
void Stop ()
 
void SoundOn ()
 
void SoundOff ()
 
void SetGain (float gain)
 
float GetGain ()
 
bool Play (const Sound *sound)
 
int CreateSource ()
 
bool DeleteSource (unsigned int source)
 
bool PlaySource (unsigned int source, const Sound *sound, bool loop)
 
bool PauseSource (unsigned int source)
 
bool StopSource (unsigned int source)
 
bool SetSourceGain (unsigned int source, float inGain)
 
bool GetSourceGain (unsigned int source, float &outGain)
 

Private Member Functions

bool InitAL ()
 
void FinalizeAL ()
 
void Clear ()
 
unsigned int GetSource (bool reserved=false)
 
std::string GetALErrorString (ALenum error)
 

Private Attributes

bool _soundOn
 
bool _pause
 
std::vector< TInfoSource_sources
 

Detailed Description

Definition at line 16 of file SoundEngine.h.

Constructor & Destructor Documentation

◆ SoundEngine()

IncrementalEngine::SoundEngine::SoundEngine ( )

Definition at line 10 of file SoundEngine.cpp.

10  :
11  _soundOn(true),
12  _pause(false)
13  {
14  _soundOn = InitAL();
15  assert("SoundEngine failed during its inizialization. Sound turned off." && _soundOn);
16  }

◆ ~SoundEngine()

IncrementalEngine::SoundEngine::~SoundEngine ( )
virtual

Definition at line 18 of file SoundEngine.cpp.

19  {
20  FinalizeAL();
21  }

Member Function Documentation

◆ Clear()

void IncrementalEngine::SoundEngine::Clear ( )
private

Definition at line 242 of file SoundEngine.cpp.

243  {
244  std::vector<TInfoSource>::iterator itSource;
245  for (itSource = _sources.begin(); itSource != _sources.end(); ++itSource)
246  {
247  ALuint source = (*itSource).Source;
248  alDeleteSources(1, &source);
249  }
250 
251  _sources.clear();
252  }

◆ CreateSource()

int IncrementalEngine::SoundEngine::CreateSource ( )

Definition at line 103 of file SoundEngine.cpp.

104  {
105  return GetSource(true);
106  }

◆ DeleteSource()

bool IncrementalEngine::SoundEngine::DeleteSource ( unsigned int  source)

Definition at line 108 of file SoundEngine.cpp.

109  {
110  if (source >= 0 && source < _sources.size())
111  {
112  StopSource(source);
113  _sources[source].Reserved = false;
114  }
115 
116  return true;
117  }

◆ FinalizeAL()

void IncrementalEngine::SoundEngine::FinalizeAL ( )
private

Definition at line 230 of file SoundEngine.cpp.

231  {
232  Clear();
233 
234  ALCcontext* context = alcGetCurrentContext();
235  ALCdevice* device = alcGetContextsDevice(context);
236 
237  alcMakeContextCurrent(NULL);
238  alcDestroyContext(context);
239  alcCloseDevice(device);
240  }

◆ GetALErrorString()

std::string IncrementalEngine::SoundEngine::GetALErrorString ( ALenum  error)
private

Definition at line 289 of file SoundEngine.cpp.

290  {
291  switch (error)
292  {
293  case AL_NO_ERROR:
294  return std::string("AL_NO_ERROR");
295  break;
296  case AL_INVALID_NAME:
297  return std::string("AL_INVALID_NAME");
298  break;
299  case AL_INVALID_ENUM:
300  return std::string("AL_INVALID_ENUM");
301  break;
302  case AL_INVALID_VALUE:
303  return std::string("AL_INVALID_VALUE");
304  break;
305  case AL_INVALID_OPERATION:
306  return std::string("AL_INVALID_OPERATION");
307  break;
308  case AL_OUT_OF_MEMORY:
309  return std::string("AL_OUT_OF_MEMORY");
310  break;
311  }
312 
313  return "";
314  }

◆ GetGain()

float IncrementalEngine::SoundEngine::GetGain ( )

Definition at line 28 of file SoundEngine.cpp.

29  {
30  float gain;
31  alGetListenerf(AL_GAIN, &gain);
32  return gain;
33  }

◆ GetSource()

unsigned int IncrementalEngine::SoundEngine::GetSource ( bool  reserved = false)
private

Definition at line 254 of file SoundEngine.cpp.

255  {
256  unsigned int result = 0;
257  ALint state;
258  ALenum error;
259 
260  for (unsigned int i = 0; i < _sources.size(); ++i)
261  {
262  if (!_sources[i].Reserved)
263  {
264  alGetSourcei(_sources[i].Source, AL_SOURCE_STATE, &state);
265  if (state != AL_PLAYING && state != AL_PAUSED)
266  {
267  _sources[i].Reserved = reserved;
268  return i;
269  }
270  }
271  }
272 
273  TInfoSource l_Info;
274  alGenSources(1, &l_Info.Source);
275  if ((error = alGetError()) != AL_NO_ERROR)
276  {
277  result = -1;
278  }
279  else
280  {
281  l_Info.Reserved = reserved;
282  _sources.push_back(l_Info);
283  result = (unsigned int)_sources.size() - 1;
284  }
285 
286  return result;
287  }

◆ GetSourceGain()

bool IncrementalEngine::SoundEngine::GetSourceGain ( unsigned int  source,
float &  outGain 
)

Definition at line 201 of file SoundEngine.cpp.

202  {
203  if (source >= 0 && source < _sources.size() && _sources[source].Reserved)
204  {
205  alGetSourcef(_sources[source].Source, AL_GAIN, &gain);
206  return true;
207  }
208  else
209  {
210  return false;
211  }
212  }

◆ InitAL()

bool IncrementalEngine::SoundEngine::InitAL ( )
private

Definition at line 214 of file SoundEngine.cpp.

215  {
216  ALenum error;
217  ALCdevice* device = alcOpenDevice(NULL);
218  ALCcontext* context = alcCreateContext(device, NULL);
219 
220  alcMakeContextCurrent(context);
221 
222  if ((error = alcGetError(device)) != ALC_NO_ERROR)
223  {
224  return false;
225  }
226 
227  return alutInitWithoutContext(NULL, NULL) == AL_TRUE;
228  }

◆ Pause()

void IncrementalEngine::SoundEngine::Pause ( )

Definition at line 68 of file SoundEngine.cpp.

69  {
70  ALint state;
71  std::vector<TInfoSource>::iterator it;
72  _pause = !_pause;
73 
74  for (it = _sources.begin(); it != _sources.end(); ++it)
75  {
76  alGetSourcei((*it).Source, AL_SOURCE_STATE, &state);
77  if (state == AL_PLAYING && _pause)
78  {
79  alSourcePause((*it).Source);
80  }
81  else if (state == AL_PAUSED && !_pause)
82  {
83  alSourcePlay((*it).Source);
84  }
85  }
86  }

◆ PauseSource()

bool IncrementalEngine::SoundEngine::PauseSource ( unsigned int  source)

Definition at line 145 of file SoundEngine.cpp.

146  {
147  if (source >= 0 && source < _sources.size() && _sources[source].Reserved)
148  {
149  ALint state;
150 
151  alGetSourcei(_sources[source].Source, AL_SOURCE_STATE, &state);
152  if (state == AL_PLAYING)
153  {
154  alSourcePause(_sources[source].Source);
155  }
156  else if (state == AL_PAUSED)
157  {
158  alSourcePlay(_sources[source].Source);
159  }
160  return true;
161  }
162  else
163  {
164  return false;
165  }
166  }

◆ Play()

bool IncrementalEngine::SoundEngine::Play ( const Sound sound)

Definition at line 35 of file SoundEngine.cpp.

36  {
37  if (_soundOn)
38  {
39  ALfloat vector[3] = { 0.0f, 0.0f, 0.0f };
40  int index = GetSource(false);
41 
42  if (index == -1)
43  {
44  return false;
45  }
46 
47  alSourcei(_sources[index].Source, AL_BUFFER, sound->Buffer);
48  alSourcei(_sources[index].Source, AL_SOURCE_RELATIVE, AL_TRUE);
49  alSourcef(_sources[index].Source, AL_PITCH, 1.0f);
50  alSourcef(_sources[index].Source, AL_GAIN, 1.0f);
51  alSourcefv(_sources[index].Source, AL_POSITION, vector);
52  alSourcefv(_sources[index].Source, AL_VELOCITY, vector);
53  alSourcefv(_sources[index].Source, AL_DIRECTION, vector);
54  alSourcei(_sources[index].Source, AL_LOOPING, AL_FALSE);
55 
56  alSourcePlay(_sources[index].Source);
57  }
58 
59  return true;
60  }

◆ PlaySource()

bool IncrementalEngine::SoundEngine::PlaySource ( unsigned int  source,
const Sound sound,
bool  loop 
)

Definition at line 119 of file SoundEngine.cpp.

120  {
121  if (_soundOn)
122  {
123  if (source >= 0 && source < _sources.size() && _sources[source].Reserved)
124  {
125  StopSource(source);
126 
127  alSourcei(_sources[source].Source, AL_BUFFER, sound->Buffer);
128  alSourcef(_sources[source].Source, AL_PITCH, 1.0f);
129  alSourcef(_sources[source].Source, AL_GAIN, 1.0f);
130  alSourcei(_sources[source].Source, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
131 
132  alSourcePlay(_sources[source].Source);
133 
134  return true;
135  }
136  else
137  {
138  return false;
139  }
140  }
141 
142  return true;
143  }

◆ Reset()

void IncrementalEngine::SoundEngine::Reset ( )

Definition at line 62 of file SoundEngine.cpp.

63  {
64  Stop();
65  Clear();
66  }

◆ SetGain()

void IncrementalEngine::SoundEngine::SetGain ( float  gain)

Definition at line 23 of file SoundEngine.cpp.

24  {
25  alListenerf(AL_GAIN, gain);
26  }

◆ SetSourceGain()

bool IncrementalEngine::SoundEngine::SetSourceGain ( unsigned int  source,
float  inGain 
)

Definition at line 188 of file SoundEngine.cpp.

189  {
190  if (source >= 0 && source < _sources.size() && _sources[source].Reserved)
191  {
192  alSourcef(_sources[source].Source, AL_GAIN, gain);
193  return true;
194  }
195  else
196  {
197  return false;
198  }
199  }

◆ SoundOff()

void IncrementalEngine::SoundEngine::SoundOff ( )
inline

Definition at line 26 of file SoundEngine.h.

26 { _soundOn = false; }

◆ SoundOn()

void IncrementalEngine::SoundEngine::SoundOn ( )
inline

Definition at line 25 of file SoundEngine.h.

25 { _soundOn = true; }

◆ Stop()

void IncrementalEngine::SoundEngine::Stop ( )

Definition at line 88 of file SoundEngine.cpp.

89  {
90  ALint state;
91  std::vector<TInfoSource>::iterator it;
92 
93  for (it = _sources.begin(); it != _sources.end(); ++it)
94  {
95  alGetSourcei((*it).Source, AL_SOURCE_STATE, &state);
96  if (state == AL_PLAYING || state == AL_PAUSED)
97  {
98  alSourceStop((*it).Source);
99  }
100  }
101  }

◆ StopSource()

bool IncrementalEngine::SoundEngine::StopSource ( unsigned int  source)

Definition at line 168 of file SoundEngine.cpp.

169  {
170  if (source >= 0 && source < _sources.size() && _sources[source].Reserved)
171  {
172  ALint state;
173 
174  alGetSourcei(_sources[source].Source, AL_SOURCE_STATE, &state);
175  if (state == AL_PLAYING || state == AL_PAUSED)
176  {
177  alSourceStop(_sources[source].Source);
178  }
179 
180  return true;
181  }
182  else
183  {
184  return false;
185  }
186  }

Member Data Documentation

◆ _pause

bool IncrementalEngine::SoundEngine::_pause
private

Definition at line 48 of file SoundEngine.h.

◆ _soundOn

bool IncrementalEngine::SoundEngine::_soundOn
private

Definition at line 47 of file SoundEngine.h.

◆ _sources

std::vector<TInfoSource> IncrementalEngine::SoundEngine::_sources
private

Definition at line 50 of file SoundEngine.h.

IncrementalEngine::SoundEngine::FinalizeAL
void FinalizeAL()
Definition: SoundEngine.cpp:230
IncrementalEngine::SoundEngine::_sources
std::vector< TInfoSource > _sources
Definition: SoundEngine.h:50
IncrementalEngine::SoundEngine::GetSource
unsigned int GetSource(bool reserved=false)
Definition: SoundEngine.cpp:254
IncrementalEngine::SoundEngine::StopSource
bool StopSource(unsigned int source)
Definition: SoundEngine.cpp:168
IncrementalEngine::SoundEngine::_soundOn
bool _soundOn
Definition: SoundEngine.h:47
IncrementalEngine::SoundEngine::Clear
void Clear()
Definition: SoundEngine.cpp:242
IncrementalEngine::SoundEngine::InitAL
bool InitAL()
Definition: SoundEngine.cpp:214
IncrementalEngine::SoundEngine::_pause
bool _pause
Definition: SoundEngine.h:48
IncrementalEngine::SoundEngine::Stop
void Stop()
Definition: SoundEngine.cpp:88