Magic_Game/core/Tool/Player.cpp

195 lines
3.3 KiB
C++
Raw Normal View History

2018-05-17 23:53:27 +08:00
#include "..\e2dtool.h"
e2d::Player::Player()
: _volume(1.f)
2018-05-17 23:53:27 +08:00
{
}
e2d::Player::~Player()
2018-05-17 23:53:27 +08:00
{
if (!_musicList.empty())
{
2018-08-12 14:30:28 +08:00
for (const auto& pair : _musicList)
{
delete pair.second;
}
}
CoUninitialize();
}
bool e2d::Player::preload(const String & filePath)
{
if (filePath.isEmpty())
return false;
Music * music = new (std::nothrow) Music();
if (music && music->open(filePath))
2018-08-12 12:06:06 +08:00
{
music->setVolume(_volume);
_musicList.insert(std::make_pair(filePath.hash(), music));
return true;
2018-08-12 12:06:06 +08:00
}
return false;
}
bool e2d::Player::play(const String & filePath, int nLoopCount)
{
if (filePath.isEmpty())
return false;
2018-07-04 15:33:09 +08:00
if (Player::preload(filePath))
{
auto music = _musicList[filePath.hash()];
if (music->play(nLoopCount))
{
return true;
}
}
return false;
}
void e2d::Player::pause(const String & filePath)
{
if (filePath.isEmpty())
return;
size_t hash = filePath.hash();
if (_musicList.end() != _musicList.find(hash))
_musicList[hash]->pause();
}
void e2d::Player::resume(const String & filePath)
{
if (filePath.isEmpty())
return;
size_t hash = filePath.hash();
if (_musicList.end() != _musicList.find(hash))
_musicList[hash]->resume();
}
void e2d::Player::stop(const String & filePath)
{
if (filePath.isEmpty())
return;
size_t hash = filePath.hash();
if (_musicList.end() != _musicList.find(hash))
_musicList[hash]->stop();
}
2018-05-17 23:53:27 +08:00
bool e2d::Player::isPlaying(const String & filePath)
2018-05-17 23:53:27 +08:00
{
if (filePath.isEmpty())
return false;
size_t hash = filePath.hash();
if (_musicList.end() != _musicList.find(hash))
return _musicList[hash]->isPlaying();
return false;
2018-05-17 23:53:27 +08:00
}
bool e2d::Player::preload(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_musicList.end() != _musicList.find(res.resNameId))
2018-05-17 23:53:27 +08:00
return true;
2018-08-12 12:06:06 +08:00
Music * music = new (std::nothrow) Music();
2018-08-12 12:06:06 +08:00
if (music && music->open(res))
{
music->setVolume(_volume);
_musicList.insert(std::make_pair(res.resNameId, music));
return true;
2018-05-17 23:53:27 +08:00
}
return false;
}
bool e2d::Player::play(const Resource& res, int nLoopCount)
2018-05-17 23:53:27 +08:00
{
if (Player::preload(res))
2018-05-17 23:53:27 +08:00
{
auto music = _musicList[res.resNameId];
2018-05-17 23:53:27 +08:00
if (music->play(nLoopCount))
{
return true;
}
}
return false;
}
void e2d::Player::pause(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_musicList.end() != _musicList.find(res.resNameId))
_musicList[res.resNameId]->pause();
2018-05-17 23:53:27 +08:00
}
void e2d::Player::resume(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_musicList.end() != _musicList.find(res.resNameId))
_musicList[res.resNameId]->resume();
2018-05-17 23:53:27 +08:00
}
void e2d::Player::stop(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_musicList.end() != _musicList.find(res.resNameId))
_musicList[res.resNameId]->stop();
2018-05-17 23:53:27 +08:00
}
bool e2d::Player::isPlaying(const Resource& res)
2018-05-17 23:53:27 +08:00
{
if (_musicList.end() != _musicList.find(res.resNameId))
return _musicList[res.resNameId]->isPlaying();
2018-05-17 23:53:27 +08:00
return false;
}
2018-07-28 20:06:27 +08:00
float e2d::Player::getVolume()
2018-05-17 23:53:27 +08:00
{
return _volume;
2018-05-17 23:53:27 +08:00
}
2018-07-28 20:06:27 +08:00
void e2d::Player::setVolume(float volume)
2018-05-17 23:53:27 +08:00
{
2018-07-28 20:06:27 +08:00
_volume = std::min(std::max(volume, -224.f), 224.f);
2018-08-12 14:30:28 +08:00
for (const auto& pair : _musicList)
{
pair.second->setVolume(_volume);
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::pauseAll()
{
2018-08-12 14:30:28 +08:00
for (const auto& pair : _musicList)
{
pair.second->pause();
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::resumeAll()
{
2018-08-12 14:30:28 +08:00
for (const auto& pair : _musicList)
{
pair.second->resume();
}
2018-05-17 23:53:27 +08:00
}
void e2d::Player::stopAll()
{
2018-08-12 14:30:28 +08:00
for (const auto& pair : _musicList)
{
pair.second->stop();
}
}
void e2d::Player::clearCache()
{
2018-08-12 14:30:28 +08:00
for (const auto& pair : _musicList)
{
delete pair.second;
}
2018-07-08 02:48:04 +08:00
_musicList.clear();
2018-05-17 23:53:27 +08:00
}