Magic_Game/core/Manager/MusicManager.cpp

222 lines
3.7 KiB
C++
Raw Normal View History

#include "..\emanager.h"
#include "..\etool.h"
2018-02-01 22:07:44 +08:00
#include <map>
2018-04-01 23:08:11 +08:00
#if HIGHER_THAN_VS2010
2018-02-01 22:07:44 +08:00
static IXAudio2 * s_pXAudio2 = nullptr;
static IXAudio2MasteringVoice * s_pMasteringVoice = nullptr;
2018-04-01 23:08:11 +08:00
#else
static HINSTANCE s_hInstance = nullptr;
#endif
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-04-01 23:08:11 +08:00
for (auto iter = getMusicList().begin(); iter != getMusicList().end(); iter++)
2018-02-01 22:07:44 +08:00
{
2018-04-01 23:08:11 +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-04-01 23:08:11 +08:00
for (auto iter = getMusicList().begin(); iter != getMusicList().end(); iter++)
2018-02-01 22:07:44 +08:00
{
2018-04-01 23:08:11 +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-04-01 23:08:11 +08:00
for (auto iter = getMusicList().begin(); iter != getMusicList().end(); iter++)
2018-02-01 22:07:44 +08:00
{
2018-04-01 23:08:11 +08:00
(*iter).second->stop();
2018-02-01 22:07:44 +08:00
}
}
2018-04-01 23:08:11 +08:00
#if HIGHER_THAN_VS2010
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);
2018-04-01 23:08:11 +08:00
}
#else
HINSTANCE e2d::MusicManager::getHInstance()
{
return s_hInstance;
}
bool e2d::MusicManager::__init()
{
s_hInstance = HINST_THISCOMPONENT;
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = Music::MusicProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = s_hInstance;
wc.hIcon = 0;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = MUSIC_CLASS_NAME;
if (!RegisterClass(&wc) && 1410 != GetLastError())
{
return false;
}
return true;
}
void e2d::MusicManager::__uninit()
{
for (auto iter = getMusicList().begin(); iter != getMusicList().end(); iter++)
{
(*iter).second->close();
(*iter).second->release();
}
getMusicList().clear();
}
#endif