fixed bug: tests::CocosDenshionTest can't exit program when music playing.

This commit is contained in:
yangws 2011-01-24 14:31:51 +08:00
parent e23c1eb2f6
commit 90e8762cd5
1 changed files with 89 additions and 83 deletions

View File

@ -12,19 +12,27 @@ namespace CocosDenshion {
typedef map<unsigned int, MciPlayer> EffectList;
typedef pair<unsigned int, MciPlayer> Effect;
static SimpleAudioEngine s_SharedEngine;
static EffectList s_List;
static MciPlayer s_Music;
static char s_szRootPath[MAX_PATH];
static DWORD s_dwRootLen;
static char s_szFullPath[MAX_PATH];
static const char * _FullPath(const char * szPath);
static unsigned int _Hash(const char *key);
#define BREAK_IF(cond) if (cond) break;
static EffectList& sharedList()
{
static EffectList s_List;
return s_List;
}
static MciPlayer& sharedMusic()
{
static MciPlayer s_Music;
return s_Music;
}
SimpleAudioEngine::SimpleAudioEngine()
{
}
@ -35,11 +43,14 @@ SimpleAudioEngine::~SimpleAudioEngine()
SimpleAudioEngine* SimpleAudioEngine::sharedEngine()
{
static SimpleAudioEngine s_SharedEngine;
return &s_SharedEngine;
}
void SimpleAudioEngine::end()
{
sharedMusic().Close();
sharedList().clear();
return;
}
@ -58,35 +69,35 @@ void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
return;
}
s_Music.Open(_FullPath(pszFilePath), _Hash(pszFilePath));
s_Music.Play((bLoop) ? -1 : 1);
sharedMusic().Open(_FullPath(pszFilePath), _Hash(pszFilePath));
sharedMusic().Play((bLoop) ? -1 : 1);
}
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
{
if (bReleaseData)
{
s_Music.Close();
sharedMusic().Close();
}
else
{
s_Music.Stop();
sharedMusic().Stop();
}
}
void SimpleAudioEngine::pauseBackgroundMusic()
{
s_Music.Pause();
sharedMusic().Pause();
}
void SimpleAudioEngine::resumeBackgroundMusic()
{
s_Music.Resume();
sharedMusic().Resume();
}
void SimpleAudioEngine::rewindBackgroundMusic()
{
s_Music.Rewind();
sharedMusic().Rewind();
}
bool SimpleAudioEngine::willPlayBackgroundMusic()
@ -96,7 +107,7 @@ bool SimpleAudioEngine::willPlayBackgroundMusic()
bool SimpleAudioEngine::isBackgroundMusicPlaying()
{
return s_Music.IsPlaying();
return sharedMusic().IsPlaying();
}
//////////////////////////////////////////////////////////////////////////
@ -109,8 +120,8 @@ unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath)
preloadEffect(pszFilePath);
EffectList::iterator p = s_List.find(nRet);
if (p != s_List.end())
EffectList::iterator p = sharedList().find(nRet);
if (p != sharedList().end())
{
p->second.Rewind();
}
@ -120,8 +131,8 @@ unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath)
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
{
EffectList::iterator p = s_List.find(nSoundId);
if (p != s_List.end())
EffectList::iterator p = sharedList().find(nSoundId);
if (p != sharedList().end())
{
p->second.Stop();
}
@ -136,15 +147,15 @@ void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
nRet = _Hash(pszFilePath);
BREAK_IF(s_List.end() != s_List.find(nRet));
BREAK_IF(sharedList().end() != sharedList().find(nRet));
s_List.insert(Effect(nRet, MciPlayer()));
MciPlayer& player = s_List[nRet];
sharedList().insert(Effect(nRet, MciPlayer()));
MciPlayer& player = sharedList()[nRet];
player.Open(_FullPath(pszFilePath), nRet);
BREAK_IF(nRet == player.GetSoundID());
s_List.erase(nRet);
sharedList().erase(nRet);
nRet = 0;
} while (0);
}
@ -157,14 +168,9 @@ void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
{
unsigned int nID = _Hash(pszFilePath);
s_List.erase(nID);
sharedList().erase(nID);
}
// void SimpleAudioEngine::unloadEffectAll()
// {
// s_List.clear();
// }
//////////////////////////////////////////////////////////////////////////
// volume interface
//////////////////////////////////////////////////////////////////////////