Magic_Game/core/Manager/MusicManager.cpp

130 lines
2.0 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dmanager.h"
#include "..\e2dtool.h"
2018-02-01 22:07:44 +08:00
#include <map>
2018-04-01 23:08:11 +08:00
typedef std::pair<UINT, e2d::Music *> MusicPair;
typedef std::map<UINT, e2d::Music *> MusicMap;
2018-02-01 22:07:44 +08:00
static MusicMap& GetMusicList()
2018-02-01 22:07:44 +08:00
{
static MusicMap s_List;
2018-02-01 22:07:44 +08:00
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
{
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];
2018-03-02 23:49:57 +08:00
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];
2018-02-01 22:07:44 +08:00
return nullptr;
}
void e2d::MusicManager::pauseAll()
2018-02-01 22:07:44 +08:00
{
2018-04-24 20:22:41 +08:00
for (auto pair : GetMusicList())
2018-02-01 22:07:44 +08:00
{
2018-04-24 20:22:41 +08:00
pair.second->pause();
2018-02-01 22:07:44 +08:00
}
}
void e2d::MusicManager::resumeAll()
2018-02-01 22:07:44 +08:00
{
2018-04-24 20:22:41 +08:00
for (auto pair : GetMusicList())
2018-02-01 22:07:44 +08:00
{
2018-04-24 20:22:41 +08:00
pair.second->resume();
2018-02-01 22:07:44 +08:00
}
}
void e2d::MusicManager::stopAll()
2018-02-01 22:07:44 +08:00
{
2018-04-24 20:22:41 +08:00
for (auto pair : GetMusicList())
2018-02-01 22:07:44 +08:00
{
2018-04-24 20:22:41 +08:00
pair.second->stop();
2018-02-01 22:07:44 +08:00
}
}
void e2d::MusicManager::__uninit()
2018-02-01 22:07:44 +08:00
{
2018-04-24 20:22:41 +08:00
for (auto pair : GetMusicList())
2018-02-01 22:07:44 +08:00
{
2018-04-24 20:22:41 +08:00
pair.second->close();
pair.second->release();
2018-02-01 22:07:44 +08:00
}
GetMusicList().clear();
2018-04-01 23:08:11 +08:00
}