Magic_Game/core/tools/Player.cpp

215 lines
3.8 KiB
C++
Raw Normal View History

2018-10-06 10:25:29 +08:00
#include "..\e2dtool.h"
2018-11-08 00:21:59 +08:00
namespace easy2d
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
std::map<size_t, Music*> Player::musics_;
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
Player::Player()
: volume_(1.f)
{
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
Player::~Player()
{
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
bool Player::Load(const std::wstring & file_path)
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
if (file_path.empty())
return false;
2018-11-08 00:21:59 +08:00
Music * music = new (std::nothrow) Music();
if (music)
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
if (music->Load(file_path))
{
music->SetVolume(volume_);
size_t hash_code = std::hash<std::wstring>{}(file_path);
musics_.insert(std::make_pair(hash_code, music));
return true;
}
else
{
music->Release();
}
2018-10-06 10:25:29 +08:00
}
return false;
2018-11-08 00:21:59 +08:00
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
bool Player::Play(const std::wstring & file_path, int loop_count)
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
if (file_path.empty())
return false;
if (Load(file_path))
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
auto music = musics_[std::hash<std::wstring>{}(file_path)];
if (music->Play(loop_count))
{
return true;
}
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
return false;
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
void Player::Pause(const std::wstring & file_path)
{
if (file_path.empty())
return;
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
size_t hash_code = std::hash<std::wstring>{}(file_path);
if (musics_.end() != musics_.find(hash_code))
musics_[hash_code]->Pause();
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
void Player::Resume(const std::wstring & file_path)
{
if (file_path.empty())
return;
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
size_t hash_code = std::hash<std::wstring>{}(file_path);
if (musics_.end() != musics_.find(hash_code))
musics_[hash_code]->Resume();
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
void Player::Stop(const std::wstring & file_path)
{
if (file_path.empty())
return;
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
size_t hash_code = std::hash<std::wstring>{}(file_path);
if (musics_.end() != musics_.find(hash_code))
musics_[hash_code]->Stop();
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
bool Player::IsPlaying(const std::wstring & file_path)
{
if (file_path.empty())
return false;
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
size_t hash_code = std::hash<std::wstring>{}(file_path);
if (musics_.end() != musics_.find(hash_code))
return musics_[hash_code]->IsPlaying();
return false;
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
bool Player::Load(Resource& res)
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
size_t hash_code = res.GetHashCode();
if (musics_.end() != musics_.find(hash_code))
2018-10-06 10:25:29 +08:00
return true;
2018-11-08 00:21:59 +08:00
Music * music = new (std::nothrow) Music();
if (music)
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
if (music->Load(res))
{
music->SetVolume(volume_);
musics_.insert(std::make_pair(hash_code, music));
return true;
}
else
{
music->Release();
}
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
return false;
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
bool Player::Play(Resource& res, int loop_count)
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
if (Load(res))
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
size_t hash_code = res.GetHashCode();
auto music = musics_[hash_code];
if (music->Play(loop_count))
{
return true;
}
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
return false;
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
void Player::Pause(Resource& res)
{
size_t hash_code = res.GetHashCode();
if (musics_.end() != musics_.find(hash_code))
musics_[hash_code]->Pause();
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
void Player::Resume(Resource& res)
{
size_t hash_code = res.GetHashCode();
if (musics_.end() != musics_.find(hash_code))
musics_[hash_code]->Resume();
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
void Player::Stop(Resource& res)
{
size_t hash_code = res.GetHashCode();
if (musics_.end() != musics_.find(hash_code))
musics_[hash_code]->Stop();
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
bool Player::IsPlaying(Resource& res)
{
size_t hash_code = res.GetHashCode();
if (musics_.end() != musics_.find(hash_code))
return musics_[hash_code]->IsPlaying();
return false;
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
float Player::GetVolume() const
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
return volume_;
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
void Player::SetVolume(float volume)
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
volume_ = std::min(std::max(volume, -224.f), 224.f);
for (const auto& pair : musics_)
{
pair.second->SetVolume(volume_);
}
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
void Player::PauseAll()
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
for (const auto& pair : musics_)
{
pair.second->Pause();
}
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
void Player::ResumeAll()
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
for (const auto& pair : musics_)
{
pair.second->Resume();
}
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
void Player::StopAll()
{
for (const auto& pair : musics_)
{
pair.second->Stop();
}
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
void Player::ClearCache()
2018-10-06 10:25:29 +08:00
{
2018-11-08 00:21:59 +08:00
if (musics_.empty())
return;
for (const auto& pair : musics_)
{
pair.second->Release();
}
musics_.clear();
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
}