| 
									
										
										
										
											2018-09-05 13:33:39 +08:00
										 |  |  | #include "..\e2dtool.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | e2d::Player * e2d::Player::instance_ = nullptr; | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | e2d::Player * e2d::Player::GetInstance() | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (!instance_) | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		instance_ = new (std::nothrow) Player; | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	return instance_; | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::DestroyInstance() | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (instance_) | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		delete instance_; | 
					
						
							|  |  |  | 		instance_ = nullptr; | 
					
						
							| 
									
										
										
										
											2018-09-02 14:30:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | e2d::Player::Player() | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	: volume_(1.f) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | e2d::Player::~Player() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (!musics_.empty()) | 
					
						
							| 
									
										
										
										
											2018-07-21 22:57:21 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		for (const auto& pair : musics_) | 
					
						
							| 
									
										
										
										
											2018-07-21 22:57:21 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  | 			pair.second->Release(); | 
					
						
							| 
									
										
										
										
											2018-07-21 22:57:21 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | bool e2d::Player::Preload(const String & file_path) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (file_path.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Music * music = new (std::nothrow) Music(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  | 	if (music) | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  | 		music->Retain(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (music->Open(file_path)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			music->SetVolume(volume_); | 
					
						
							|  |  |  | 			musics_.insert(std::make_pair(file_path.GetHash(), music)); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			music->Release(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-08-12 12:06:06 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | bool e2d::Player::Play(const String & file_path, int loop_count) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (file_path.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-07-04 15:33:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (Player::Preload(file_path)) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		auto music = musics_[file_path.GetHash()]; | 
					
						
							|  |  |  | 		if (music->Play(loop_count)) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::Pause(const String & file_path) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (file_path.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	size_t hash = file_path.GetHash(); | 
					
						
							|  |  |  | 	if (musics_.end() != musics_.find(hash)) | 
					
						
							|  |  |  | 		musics_[hash]->Pause(); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::Resume(const String & file_path) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (file_path.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	size_t hash = file_path.GetHash(); | 
					
						
							|  |  |  | 	if (musics_.end() != musics_.find(hash)) | 
					
						
							|  |  |  | 		musics_[hash]->Resume(); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::Stop(const String & file_path) | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (file_path.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	size_t hash = file_path.GetHash(); | 
					
						
							|  |  |  | 	if (musics_.end() != musics_.find(hash)) | 
					
						
							|  |  |  | 		musics_[hash]->Stop(); | 
					
						
							| 
									
										
										
										
											2018-07-03 20:19:00 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | bool e2d::Player::IsPlaying(const String & file_path) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (file_path.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	size_t hash = file_path.GetHash(); | 
					
						
							|  |  |  | 	if (musics_.end() != musics_.find(hash)) | 
					
						
							|  |  |  | 		return musics_[hash]->IsPlaying(); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | bool e2d::Player::Preload(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-07 17:56:03 +08:00
										 |  |  | 	if (musics_.end() != musics_.find(res.id)) | 
					
						
							| 
									
										
										
										
											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-07-08 02:41:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  | 	if (music) | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  | 		music->Retain(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (music->Open(res)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			music->SetVolume(volume_); | 
					
						
							| 
									
										
										
										
											2018-09-07 17:56:03 +08:00
										 |  |  | 			musics_.insert(std::make_pair(res.id, music)); | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			music->Release(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | bool e2d::Player::Play(const Resource& res, int loop_count) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	if (Player::Preload(res)) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-07 17:56:03 +08:00
										 |  |  | 		auto music = musics_[res.id]; | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		if (music->Play(loop_count)) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::Pause(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-07 17:56:03 +08:00
										 |  |  | 	if (musics_.end() != musics_.find(res.id)) | 
					
						
							|  |  |  | 		musics_[res.id]->Pause(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::Resume(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-07 17:56:03 +08:00
										 |  |  | 	if (musics_.end() != musics_.find(res.id)) | 
					
						
							|  |  |  | 		musics_[res.id]->Resume(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::Stop(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-07 17:56:03 +08:00
										 |  |  | 	if (musics_.end() != musics_.find(res.id)) | 
					
						
							|  |  |  | 		musics_[res.id]->Stop(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | bool e2d::Player::IsPlaying(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-07 17:56:03 +08:00
										 |  |  | 	if (musics_.end() != musics_.find(res.id)) | 
					
						
							|  |  |  | 		return musics_[res.id]->IsPlaying(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | float e2d::Player::GetVolume() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	return volume_; | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::SetVolume(float volume) | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	volume_ = std::min(std::max(volume, -224.f), 224.f); | 
					
						
							|  |  |  | 	for (const auto& pair : musics_) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		pair.second->SetVolume(volume_); | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::PauseAll() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	for (const auto& pair : musics_) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		pair.second->Pause(); | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::ResumeAll() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	for (const auto& pair : musics_) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		pair.second->Resume(); | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::StopAll() | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	for (const auto& pair : musics_) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 		pair.second->Stop(); | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | void e2d::Player::ClearCache() | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	for (const auto& pair : musics_) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-21 22:57:21 +08:00
										 |  |  | 		delete pair.second; | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	musics_.clear(); | 
					
						
							| 
									
										
										
										
											2018-05-17 23:53:27 +08:00
										 |  |  | } |