Magic_Game/core/utils/Player.cpp

234 lines
5.0 KiB
C++
Raw Normal View History

// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Player.h"
2018-11-12 20:46:54 +08:00
#include "../base/Music.h"
2018-10-06 10:25:29 +08:00
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
Player::Player()
: volume_(1.f)
{
}
2018-10-06 10:25:29 +08:00
2018-11-08 00:21:59 +08:00
Player::~Player()
{
2018-11-12 20:46:54 +08:00
ClearCache();
2018-11-08 00:21:59 +08:00
}
2018-10-06 10:25:29 +08:00
bool Player::Load(const String & 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<String>{}(file_path);
2018-11-12 20:46:54 +08:00
musics_cache_.insert(std::make_pair(hash_code, music));
2018-11-08 00:21:59 +08:00
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
bool Player::Play(const String & 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-12 20:46:54 +08:00
auto music = musics_cache_[std::hash<String>{}(file_path)];
2018-11-08 00:21:59 +08:00
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
}
void Player::Pause(const String & file_path)
2018-11-08 00:21:59 +08:00
{
if (file_path.empty())
return;
2018-10-06 10:25:29 +08:00
size_t hash_code = std::hash<String>{}(file_path);
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.find(hash_code))
musics_cache_[hash_code]->Pause();
2018-11-08 00:21:59 +08:00
}
2018-10-06 10:25:29 +08:00
void Player::Resume(const String & file_path)
2018-11-08 00:21:59 +08:00
{
if (file_path.empty())
return;
2018-10-06 10:25:29 +08:00
size_t hash_code = std::hash<String>{}(file_path);
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.find(hash_code))
musics_cache_[hash_code]->Resume();
2018-11-08 00:21:59 +08:00
}
2018-10-06 10:25:29 +08:00
void Player::Stop(const String & file_path)
2018-11-08 00:21:59 +08:00
{
if (file_path.empty())
return;
2018-10-06 10:25:29 +08:00
size_t hash_code = std::hash<String>{}(file_path);
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.find(hash_code))
musics_cache_[hash_code]->Stop();
2018-11-08 00:21:59 +08:00
}
2018-10-06 10:25:29 +08:00
bool Player::IsPlaying(const String & file_path)
2018-11-08 00:21:59 +08:00
{
if (file_path.empty())
return false;
2018-10-06 10:25:29 +08:00
size_t hash_code = std::hash<String>{}(file_path);
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.find(hash_code))
return musics_cache_[hash_code]->IsPlaying();
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::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();
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.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_);
2018-11-12 20:46:54 +08:00
musics_cache_.insert(std::make_pair(hash_code, music));
2018-11-08 00:21:59 +08:00
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();
2018-11-12 20:46:54 +08:00
auto music = musics_cache_[hash_code];
2018-11-08 00:21:59 +08:00
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();
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.find(hash_code))
musics_cache_[hash_code]->Pause();
2018-11-08 00:21:59 +08:00
}
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();
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.find(hash_code))
musics_cache_[hash_code]->Resume();
2018-11-08 00:21:59 +08:00
}
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();
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.find(hash_code))
musics_cache_[hash_code]->Stop();
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::IsPlaying(Resource& res)
{
size_t hash_code = res.GetHashCode();
2018-11-12 20:46:54 +08:00
if (musics_cache_.end() != musics_cache_.find(hash_code))
return musics_cache_[hash_code]->IsPlaying();
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
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);
2018-11-12 20:46:54 +08:00
for (const auto& pair : musics_cache_)
2018-11-08 00:21:59 +08:00
{
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-12 20:46:54 +08:00
for (const auto& pair : musics_cache_)
2018-11-08 00:21:59 +08:00
{
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-12 20:46:54 +08:00
for (const auto& pair : musics_cache_)
2018-11-08 00:21:59 +08:00
{
pair.second->Resume();
}
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
void Player::StopAll()
{
2018-11-12 20:46:54 +08:00
for (const auto& pair : musics_cache_)
2018-11-08 00:21:59 +08:00
{
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-12 20:46:54 +08:00
if (musics_cache_.empty())
2018-11-08 00:21:59 +08:00
return;
2018-11-12 20:46:54 +08:00
for (const auto& pair : musics_cache_)
2018-11-08 00:21:59 +08:00
{
pair.second->Release();
}
2018-11-12 20:46:54 +08:00
musics_cache_.clear();
2018-10-06 10:25:29 +08:00
}
2018-11-08 00:21:59 +08:00
}