Magic_Game/core/Manager/MusicManager.cpp

168 lines
2.8 KiB
C++
Raw Normal View History

2018-02-01 22:07:44 +08:00
#include "..\emanagers.h"
#include "..\etools.h"
#include <map>
static IXAudio2 * s_pXAudio2 = nullptr;
static IXAudio2MasteringVoice * s_pMasteringVoice = nullptr;
typedef std::pair<UINT, e2d::Music *> MusicPair;
typedef std::map<UINT, e2d::Music *> MusicList;
2018-02-01 22:07:44 +08:00
static MusicList& getMusicList()
{
static MusicList s_List;
return s_List;
}
bool e2d::MusicManager::preload(String& strFilePath)
2018-02-01 22:07:44 +08:00
{
2018-03-02 23:49:57 +08:00
UINT nRet = strFilePath.getHashCode();
if (getMusicList().end() != getMusicList().find(nRet))
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
return true;
2018-02-01 22:07:44 +08:00
}
else
{
2018-03-02 23:49:57 +08:00
Music * pPlayer = new Music();
2018-02-01 22:07:44 +08:00
2018-03-02 23:49:57 +08:00
if (pPlayer->open(strFilePath))
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
getMusicList().insert(MusicPair(nRet, pPlayer));
2018-03-02 23:49:57 +08:00
pPlayer->retain();
2018-02-03 22:04:43 +08:00
return true;
2018-02-01 22:07:44 +08:00
}
else
{
2018-03-02 23:49:57 +08:00
pPlayer->release();
pPlayer = nullptr;
}
}
return false;
}
bool e2d::MusicManager::play(String& strFilePath, int nLoopCount)
2018-03-02 23:49:57 +08:00
{
if (MusicManager::preload(strFilePath))
{
UINT nRet = strFilePath.getHashCode();
Music * pMusic = getMusicList()[nRet];
if (pMusic->play(nLoopCount))
{
return true;
2018-02-01 22:07:44 +08:00
}
}
2018-03-02 23:49:57 +08:00
return false;
}
void e2d::MusicManager::pause(String& strFilePath)
2018-03-02 23:49:57 +08:00
{
auto music = MusicManager::get(strFilePath);
if (music)
{
music->pause();
}
}
void e2d::MusicManager::resume(String& strFilePath)
2018-03-02 23:49:57 +08:00
{
auto music = MusicManager::get(strFilePath);
if (music)
{
music->resume();
}
}
void e2d::MusicManager::stop(String& strFilePath)
2018-03-02 23:49:57 +08:00
{
auto music = MusicManager::get(strFilePath);
if (music)
{
music->stop();
}
2018-02-01 22:07:44 +08:00
}
e2d::Music * e2d::MusicManager::get(String& strFilePath)
2018-02-01 22:07:44 +08:00
{
if (strFilePath.isEmpty())
return nullptr;
UINT nRet = strFilePath.getHashCode();
2018-02-01 22:07:44 +08:00
if (getMusicList().end() != getMusicList().find(nRet))
return getMusicList()[nRet];
return nullptr;
}
void e2d::MusicManager::pauseAll()
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
for (auto iter : getMusicList())
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
iter.second->pause();
2018-02-01 22:07:44 +08:00
}
}
void e2d::MusicManager::resumeAll()
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
for (auto iter : getMusicList())
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
iter.second->resume();
2018-02-01 22:07:44 +08:00
}
}
void e2d::MusicManager::stopAll()
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
for (auto iter : getMusicList())
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
iter.second->stop();
2018-02-01 22:07:44 +08:00
}
}
IXAudio2 * e2d::MusicManager::getIXAudio2()
2018-02-01 22:07:44 +08:00
{
return s_pXAudio2;
}
IXAudio2MasteringVoice * e2d::MusicManager::getIXAudio2MasteringVoice()
2018-02-01 22:07:44 +08:00
{
return s_pMasteringVoice;
}
bool e2d::MusicManager::__init()
2018-02-01 22:07:44 +08:00
{
HRESULT hr;
if (FAILED(hr = XAudio2Create(&s_pXAudio2, 0)))
{
WARN_IF(true, "Failed to init XAudio2 engine");
return false;
}
if (FAILED(hr = s_pXAudio2->CreateMasteringVoice(&s_pMasteringVoice)))
{
WARN_IF(true, "Failed creating mastering voice");
SafeReleaseInterface(&s_pXAudio2);
return false;
}
return true;
}
void e2d::MusicManager::__uninit()
2018-02-01 22:07:44 +08:00
{
2018-02-03 22:04:43 +08:00
for (auto iter : getMusicList())
2018-02-01 22:07:44 +08:00
{
2018-03-02 23:49:57 +08:00
iter.second->close();
iter.second->release();
2018-02-01 22:07:44 +08:00
}
getMusicList().clear();
if (s_pMasteringVoice)
{
s_pMasteringVoice->DestroyVoice();
}
SafeReleaseInterface(&s_pXAudio2);
}