forked from rtpHarry/Sokoban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundCache.cpp
74 lines (57 loc) · 1.58 KB
/
SoundCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "SoundCache.h"
SoundCache::SoundCache(void)
{
}
SoundCache::~SoundCache(void)
{
}
// Add a new sample into the cache
// Note: Check return value, if its AL_NONE then sample has not been cached
SampleID SoundCache::LoadSample(string FileName)
{
// check for file in loaded sounds list
vector<SoundCacheSample>::iterator ii;
for(ii=m_SoundCache.begin(); ii!=m_SoundCache.end(); ii++)
{
// check if sample matches
if ( ii->FileName == FileName )
{
// found: +1 to its references, return its OpenALSoundID
ii->References++;
return ii->OpenALSampleID;
}
}
// not found:
SoundCacheSample NewSample;
// load sample in OpenAL
NewSample.OpenALSampleID = _LoadSampleFromFile(FileName);
// updates its references
NewSample.References = 1;
// set the filename
NewSample.FileName = FileName;
// add it to the cache list
m_SoundCache.push_back(NewSample);
// return its SampleID
return NewSample.OpenALSampleID;
}
// Load a sample from a file
SampleID SoundCache::_LoadSampleFromFile(string FileName)
{
// Load the sound into OpenAL
//////////////////////////////////////////////////////////////////////////
ALuint buffer;
ALenum error;
// Create an AL buffer from the given sound file
buffer = alutCreateBufferFromFile (FileName.c_str());
if (buffer == AL_NONE)
{
error = alutGetError ();
fprintf (stderr, "Error loading file: '%s'\n", alutGetErrorString (error));
//alutExit ();
//exit (EXIT_FAILURE);
return AL_NONE;
}
// Return the reference to the caller
//////////////////////////////////////////////////////////////////////////
return buffer;
}