Added support for multiple background music sources to CocosDenshion for Blackberry

This commit is contained in:
moadib 2013-02-12 19:04:35 +04:00
parent ba8f26fed9
commit f7996aa691
1 changed files with 66 additions and 31 deletions

View File

@ -58,11 +58,15 @@ namespace CocosDenshion
static float s_volume = 1.0f; static float s_volume = 1.0f;
static float s_effectVolume = 1.0f; static float s_effectVolume = 1.0f;
static bool s_isBackgroundInitialized = false;
static std::string s_currentBackgroundStr;
ALuint s_backgroundBuffer; struct backgroundMusicData {
ALuint s_backgroundSource; ALuint buffer;
ALuint source;
};
typedef map<string, backgroundMusicData *> BackgroundMusicsMap;
BackgroundMusicsMap s_backgroundMusics;
static ALuint s_backgroundSource = AL_NONE;
static SimpleAudioEngine *s_engine = 0; static SimpleAudioEngine *s_engine = 0;
@ -101,18 +105,25 @@ namespace CocosDenshion
static void stopBackground(bool bReleaseData) static void stopBackground(bool bReleaseData)
{ {
alSourceStop(s_backgroundSource); alSourceStop(s_backgroundSource);
if (bReleaseData) if (bReleaseData)
{ {
s_currentBackgroundStr = ""; for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
s_isBackgroundInitialized = false; {
if (it->second->source == s_backgroundSource)
alDeleteBuffers(1, &s_backgroundBuffer); {
checkALError("stopBackground"); alDeleteBuffers(1, &it->second->buffer);
alDeleteSources(1, &s_backgroundSource); checkALError("stopBackground");
checkALError("stopBackground"); alDeleteSources(1, &it->second->source);
checkALError("stopBackground");
s_backgroundMusics.erase(it);
break;
}
}
} }
s_backgroundSource = AL_NONE;
} }
static void setBackgroundVolume(float volume) static void setBackgroundVolume(float volume)
@ -158,6 +169,18 @@ namespace CocosDenshion
// and the background too // and the background too
stopBackground(true); stopBackground(true);
for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
{
alSourceStop(it->second->source);
checkALError("end");
alDeleteBuffers(1, &it->second->buffer);
checkALError("end");
alDeleteSources(1, &it->second->source);
checkALError("end");
delete it->second;
}
s_backgroundMusics.clear();
} }
// //
@ -262,52 +285,64 @@ namespace CocosDenshion
// Changing file path to full path // Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
if (!s_isBackgroundInitialized || s_currentBackgroundStr != fullPath) BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
if (it == s_backgroundMusics.end())
{ {
string path = fullPath; ALuint buffer = AL_NONE;
if (isOGGFile(fullPath.data()))
if (isOGGFile(path.data()))
{ {
s_backgroundBuffer = createBufferFromOGG(path.data()); buffer = createBufferFromOGG(fullPath.data());
} }
else else
{ {
s_backgroundBuffer = alutCreateBufferFromFile(path.data()); buffer = alutCreateBufferFromFile(fullPath.data());
} }
checkALError("preloadBackgroundMusic"); checkALError("preloadBackgroundMusic");
if (s_backgroundBuffer == AL_NONE) if (buffer == AL_NONE)
{ {
fprintf(stderr, "Error loading file: '%s'\n", path.data()); fprintf(stderr, "Error loading file: '%s'\n", fullPath.data());
alDeleteBuffers(1, &s_backgroundBuffer); alDeleteBuffers(1, &buffer);
return; return;
} }
alGenSources(1, &s_backgroundSource); ALuint source = AL_NONE;
alGenSources(1, &source);
checkALError("preloadBackgroundMusic"); checkALError("preloadBackgroundMusic");
alSourcei(s_backgroundSource, AL_BUFFER, s_backgroundBuffer); alSourcei(source, AL_BUFFER, buffer);
checkALError("preloadBackgroundMusic"); checkALError("preloadBackgroundMusic");
s_currentBackgroundStr = fullPath; backgroundMusicData* data = new backgroundMusicData();
data->buffer = buffer;
data->source = source;
s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data));
} }
s_currentBackgroundStr = fullPath;
s_isBackgroundInitialized = true;
} }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{ {
if (s_backgroundSource != AL_NONE)
stopBackgroundMusic(false);
// Changing file path to full path // Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
if (!s_isBackgroundInitialized) BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
if (it == s_backgroundMusics.end())
{
preloadBackgroundMusic(fullPath.c_str()); preloadBackgroundMusic(fullPath.c_str());
it = s_backgroundMusics.find(fullPath);
}
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); if (it != s_backgroundMusics.end())
alSourcePlay(s_backgroundSource); {
checkALError("playBackgroundMusic"); s_backgroundSource = it->second->source;
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE);
alSourcePlay(s_backgroundSource);
checkALError("playBackgroundMusic");
}
} }
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData) void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)