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

@ -10,21 +10,29 @@ using namespace std;
namespace CocosDenshion { namespace CocosDenshion {
typedef map<unsigned int, MciPlayer> EffectList; typedef map<unsigned int, MciPlayer> EffectList;
typedef pair<unsigned int ,MciPlayer> Effect; 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 char s_szRootPath[MAX_PATH];
static DWORD s_dwRootLen;
static char s_szFullPath[MAX_PATH];
static const char * _FullPath(const char * szPath); static const char * _FullPath(const char * szPath);
static unsigned int _Hash(const char *key); static unsigned int _Hash(const char *key);
#define BREAK_IF(cond) if (cond) break; #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() SimpleAudioEngine::SimpleAudioEngine()
{ {
} }
@ -35,12 +43,15 @@ SimpleAudioEngine::~SimpleAudioEngine()
SimpleAudioEngine* SimpleAudioEngine::sharedEngine() SimpleAudioEngine* SimpleAudioEngine::sharedEngine()
{ {
static SimpleAudioEngine s_SharedEngine;
return &s_SharedEngine; return &s_SharedEngine;
} }
void SimpleAudioEngine::end() void SimpleAudioEngine::end()
{ {
return; sharedMusic().Close();
sharedList().clear();
return;
} }
void SimpleAudioEngine::setResource(const char* pszResPath, const char* pszZipFileName) void SimpleAudioEngine::setResource(const char* pszResPath, const char* pszZipFileName)
@ -53,40 +64,40 @@ void SimpleAudioEngine::setResource(const char* pszResPath, const char* pszZipFi
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{ {
if (! pszFilePath) if (! pszFilePath)
{ {
return; return;
} }
s_Music.Open(_FullPath(pszFilePath), _Hash(pszFilePath)); sharedMusic().Open(_FullPath(pszFilePath), _Hash(pszFilePath));
s_Music.Play((bLoop) ? -1 : 1); sharedMusic().Play((bLoop) ? -1 : 1);
} }
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData) void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
{ {
if (bReleaseData) if (bReleaseData)
{ {
s_Music.Close(); sharedMusic().Close();
} }
else else
{ {
s_Music.Stop(); sharedMusic().Stop();
} }
} }
void SimpleAudioEngine::pauseBackgroundMusic() void SimpleAudioEngine::pauseBackgroundMusic()
{ {
s_Music.Pause(); sharedMusic().Pause();
} }
void SimpleAudioEngine::resumeBackgroundMusic() void SimpleAudioEngine::resumeBackgroundMusic()
{ {
s_Music.Resume(); sharedMusic().Resume();
} }
void SimpleAudioEngine::rewindBackgroundMusic() void SimpleAudioEngine::rewindBackgroundMusic()
{ {
s_Music.Rewind(); sharedMusic().Rewind();
} }
bool SimpleAudioEngine::willPlayBackgroundMusic() bool SimpleAudioEngine::willPlayBackgroundMusic()
@ -96,7 +107,7 @@ bool SimpleAudioEngine::willPlayBackgroundMusic()
bool SimpleAudioEngine::isBackgroundMusicPlaying() bool SimpleAudioEngine::isBackgroundMusicPlaying()
{ {
return s_Music.IsPlaying(); return sharedMusic().IsPlaying();
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -107,46 +118,46 @@ unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath)
{ {
unsigned int nRet = _Hash(pszFilePath); unsigned int nRet = _Hash(pszFilePath);
preloadEffect(pszFilePath); preloadEffect(pszFilePath);
EffectList::iterator p = s_List.find(nRet); EffectList::iterator p = sharedList().find(nRet);
if (p != s_List.end()) if (p != sharedList().end())
{ {
p->second.Rewind(); p->second.Rewind();
} }
return nRet; return nRet;
} }
void SimpleAudioEngine::stopEffect(unsigned int nSoundId) void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
{ {
EffectList::iterator p = s_List.find(nSoundId); EffectList::iterator p = sharedList().find(nSoundId);
if (p != s_List.end()) if (p != sharedList().end())
{ {
p->second.Stop(); p->second.Stop();
} }
} }
void SimpleAudioEngine::preloadEffect(const char* pszFilePath) void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
{ {
int nRet = 0; int nRet = 0;
do do
{ {
BREAK_IF(! pszFilePath); BREAK_IF(! pszFilePath);
nRet = _Hash(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())); sharedList().insert(Effect(nRet, MciPlayer()));
MciPlayer& player = s_List[nRet]; MciPlayer& player = sharedList()[nRet];
player.Open(_FullPath(pszFilePath), nRet); player.Open(_FullPath(pszFilePath), nRet);
BREAK_IF(nRet == player.GetSoundID()); BREAK_IF(nRet == player.GetSoundID());
s_List.erase(nRet); sharedList().erase(nRet);
nRet = 0; nRet = 0;
} while (0); } while (0);
} }
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
@ -157,21 +168,16 @@ void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
void SimpleAudioEngine::unloadEffect(const char* pszFilePath) void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
{ {
unsigned int nID = _Hash(pszFilePath); unsigned int nID = _Hash(pszFilePath);
s_List.erase(nID); sharedList().erase(nID);
} }
// void SimpleAudioEngine::unloadEffectAll()
// {
// s_List.clear();
// }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// volume interface // volume interface
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
float SimpleAudioEngine::getBackgroundMusicVolume() float SimpleAudioEngine::getBackgroundMusicVolume()
{ {
return 1.0; return 1.0;
} }
void SimpleAudioEngine::setBackgroundMusicVolume(float volume) void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
@ -180,7 +186,7 @@ void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
float SimpleAudioEngine::getEffectsVolume() float SimpleAudioEngine::getEffectsVolume()
{ {
return 1.0; return 1.0;
} }
void SimpleAudioEngine::setEffectsVolume(float volume) void SimpleAudioEngine::setEffectsVolume(float volume)
@ -193,44 +199,44 @@ void SimpleAudioEngine::setEffectsVolume(float volume)
const char * _FullPath(const char * szPath) const char * _FullPath(const char * szPath)
{ {
if (! s_szRootPath[0]) if (! s_szRootPath[0])
{ {
s_dwRootLen = GetModuleFileName(NULL, s_szRootPath, sizeof(s_szRootPath)); s_dwRootLen = GetModuleFileName(NULL, s_szRootPath, sizeof(s_szRootPath));
while (--s_dwRootLen) while (--s_dwRootLen)
{ {
if ('\\' == s_szRootPath[s_dwRootLen]) if ('\\' == s_szRootPath[s_dwRootLen])
{ {
s_szRootPath[s_dwRootLen + 1] = 0; s_szRootPath[s_dwRootLen + 1] = 0;
strcpy_s(s_szFullPath, sizeof(s_szFullPath), s_szRootPath); strcpy_s(s_szFullPath, sizeof(s_szFullPath), s_szRootPath);
++s_dwRootLen; ++s_dwRootLen;
break; break;
} }
} }
} }
if (0 != szPath[0] && ':' != szPath[1]) if (0 != szPath[0] && ':' != szPath[1])
{ {
strcpy_s(s_szFullPath + s_dwRootLen, sizeof(s_szFullPath) - s_dwRootLen, szPath); strcpy_s(s_szFullPath + s_dwRootLen, sizeof(s_szFullPath) - s_dwRootLen, szPath);
return s_szFullPath; return s_szFullPath;
} }
else else
{ {
return szPath; return szPath;
} }
} }
unsigned int _Hash(const char *key) unsigned int _Hash(const char *key)
{ {
unsigned int len = strlen(key); unsigned int len = strlen(key);
const char *end=key+len; const char *end=key+len;
unsigned int hash; unsigned int hash;
for (hash = 0; key < end; key++) for (hash = 0; key < end; key++)
{ {
hash *= 16777619; hash *= 16777619;
hash ^= (unsigned int) (unsigned char) toupper(*key); hash ^= (unsigned int) (unsigned char) toupper(*key);
} }
return (hash); return (hash);
} }
} // end of namespace CocosDenshion } // end of namespace CocosDenshion