Use a deque fpr OpenAL's free sources

This commit is contained in:
Chris Robinson 2012-03-28 05:35:51 -07:00
parent a3291ef360
commit 293f33914e
2 changed files with 26 additions and 25 deletions

View file

@ -436,19 +436,22 @@ void OpenAL_Output::init(const std::string &devname)
alcGetIntegerv(mDevice, ALC_STEREO_SOURCES, 1, &maxstereo); alcGetIntegerv(mDevice, ALC_STEREO_SOURCES, 1, &maxstereo);
throwALCerror(mDevice); throwALCerror(mDevice);
mFreeSources.resize(std::min<ALCuint>(maxmono+maxstereo, 256)); try
for(size_t i = 0;i < mFreeSources.size();i++)
{ {
ALuint src; ALCuint maxtotal = std::min<ALCuint>(maxmono+maxstereo, 256);
for(size_t i = 0;i < maxtotal;i++)
{
ALuint src = 0;
alGenSources(1, &src); alGenSources(1, &src);
if(alGetError() != AL_NO_ERROR) throwALerror();
mFreeSources.push_back(src);
}
}
catch(std::exception &e)
{ {
mFreeSources.resize(i); std::cout <<"Error: "<<e.what()<<", trying to continue"<< std::endl;
break;
} }
mFreeSources[i] = src; if(mFreeSources.empty())
}
if(mFreeSources.size() == 0)
fail("Could not allocate any sources"); fail("Could not allocate any sources");
} }
@ -456,10 +459,10 @@ void OpenAL_Output::deinit()
{ {
mStreamThread->removeAll(); mStreamThread->removeAll();
if(!mFreeSources.empty()) while(!mFreeSources.empty())
{ {
alDeleteSources(mFreeSources.size(), mFreeSources.data()); alDeleteSources(1, &mFreeSources.front());
mFreeSources.clear(); mFreeSources.pop_front();
} }
mBufferRefs.clear(); mBufferRefs.clear();
@ -584,8 +587,8 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float
if(mFreeSources.empty()) if(mFreeSources.empty())
fail("No free sources"); fail("No free sources");
src = mFreeSources.back(); src = mFreeSources.front();
mFreeSources.pop_back(); mFreeSources.pop_front();
try try
{ {
@ -633,8 +636,8 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const float *pos,
if(mFreeSources.empty()) if(mFreeSources.empty())
fail("No free sources"); fail("No free sources");
src = mFreeSources.back(); src = mFreeSources.front();
mFreeSources.pop_back(); mFreeSources.pop_front();
try try
{ {
@ -682,8 +685,8 @@ SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, floa
if(mFreeSources.empty()) if(mFreeSources.empty())
fail("No free sources"); fail("No free sources");
src = mFreeSources.back(); src = mFreeSources.front();
mFreeSources.pop_back(); mFreeSources.pop_front();
try try
{ {
@ -726,8 +729,8 @@ SoundPtr OpenAL_Output::streamSound3D(const std::string &fname, const float *pos
if(mFreeSources.empty()) if(mFreeSources.empty())
fail("No free sources"); fail("No free sources");
src = mFreeSources.back(); src = mFreeSources.front();
mFreeSources.pop_back(); mFreeSources.pop_front();
try try
{ {

View file

@ -21,8 +21,9 @@ namespace MWSound
ALCdevice *mDevice; ALCdevice *mDevice;
ALCcontext *mContext; ALCcontext *mContext;
typedef std::vector<ALuint> IDVec; typedef std::deque<ALuint> IDDq;
IDVec mFreeSources; IDDq mFreeSources;
IDDq mUnusedBuffers;
typedef std::map<std::string,ALuint> NameMap; typedef std::map<std::string,ALuint> NameMap;
NameMap mBufferCache; NameMap mBufferCache;
@ -30,9 +31,6 @@ namespace MWSound
typedef std::map<ALuint,ALuint> IDRefMap; typedef std::map<ALuint,ALuint> IDRefMap;
IDRefMap mBufferRefs; IDRefMap mBufferRefs;
typedef std::deque<ALuint> IDDq;
IDDq mUnusedBuffers;
uint64_t mBufferCacheMemSize; uint64_t mBufferCacheMemSize;
ALuint getBuffer(const std::string &fname); ALuint getBuffer(const std::string &fname);