Magic_Game/core/Manager/MusicManager.cpp

125 lines
2.1 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::EMusic *> MusicPair;
typedef std::map<UINT, e2d::EMusic *> MusicList;
static MusicList& getMusicList()
{
static MusicList s_List;
return s_List;
}
2018-02-03 22:04:43 +08:00
bool e2d::EMusicManager::add(const EString & strFilePath)
2018-02-01 22:07:44 +08:00
{
EMusic * pPlayer = get(strFilePath);
if (pPlayer)
{
2018-02-03 22:04:43 +08:00
return true;
2018-02-01 22:07:44 +08:00
}
else
{
UINT nRet = strFilePath.hash();
2018-02-03 22:04:43 +08:00
pPlayer = new EMusic();
2018-02-01 22:07:44 +08:00
if (pPlayer->_open(strFilePath))
{
2018-02-03 22:04:43 +08:00
getMusicList().insert(MusicPair(nRet, pPlayer));
return true;
2018-02-01 22:07:44 +08:00
}
else
{
delete pPlayer;
2018-02-03 22:04:43 +08:00
return false;
2018-02-01 22:07:44 +08:00
}
}
}
e2d::EMusic * e2d::EMusicManager::get(const EString & strFilePath)
{
if (strFilePath.isEmpty())
return nullptr;
UINT nRet = strFilePath.hash();
if (getMusicList().end() != getMusicList().find(nRet))
return getMusicList()[nRet];
return nullptr;
}
void e2d::EMusicManager::pauseAllMusics()
{
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::EMusicManager::resumeAllMusics()
{
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::EMusicManager::stopAllMusics()
{
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::EMusicManager::getIXAudio2()
{
return s_pXAudio2;
}
IXAudio2MasteringVoice * e2d::EMusicManager::getIXAudio2MasteringVoice()
{
return s_pMasteringVoice;
}
bool e2d::EMusicManager::__init()
{
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::EMusicManager::__uninit()
{
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->_close();
delete iter.second;
2018-02-01 22:07:44 +08:00
}
getMusicList().clear();
if (s_pMasteringVoice)
{
s_pMasteringVoice->DestroyVoice();
}
SafeReleaseInterface(&s_pXAudio2);
}