Replace with 'struct' for AudioInfo/ProfileHelper.

This commit is contained in:
Dhilan007 2014-09-10 14:09:42 +08:00
parent e45a4ff1bd
commit e588b371bd
3 changed files with 46 additions and 46 deletions

View File

@ -42,10 +42,10 @@ const float AudioEngine::TIME_UNKNOWN = -1.0f;
//audio file path,audio IDs //audio file path,audio IDs
std::unordered_map<std::string,std::list<int>> AudioEngine::_audioPathIDMap; std::unordered_map<std::string,std::list<int>> AudioEngine::_audioPathIDMap;
//profileName,ProfileManage //profileName,ProfileHelper
std::unordered_map<std::string, AudioEngine::ProfileManage> AudioEngine::_audioPathProfileManageMap; std::unordered_map<std::string, AudioEngine::ProfileHelper> AudioEngine::_audioPathProfileHelperMap;
int AudioEngine::_maxInstances = kMaxSources; int AudioEngine::_maxInstances = kMaxSources;
AudioEngine::ProfileManage* AudioEngine::_defaultProfileManage; AudioEngine::ProfileHelper* AudioEngine::_defaultProfileHelper;
std::unordered_map<int, AudioEngine::AudioInfo> AudioEngine::_audioIDInfoMap; std::unordered_map<int, AudioEngine::AudioInfo> AudioEngine::_audioIDInfoMap;
AudioEngineImpl* AudioEngine::_audioEngineImpl = nullptr; AudioEngineImpl* AudioEngine::_audioEngineImpl = nullptr;
@ -54,8 +54,8 @@ void AudioEngine::end()
delete _audioEngineImpl; delete _audioEngineImpl;
_audioEngineImpl = nullptr; _audioEngineImpl = nullptr;
delete _defaultProfileManage; delete _defaultProfileHelper;
_defaultProfileManage = nullptr; _defaultProfileHelper = nullptr;
} }
bool AudioEngine::lazyInit() bool AudioEngine::lazyInit()
@ -84,10 +84,10 @@ int AudioEngine::play2d(const std::string& filePath, bool loop, float volume, co
break; break;
} }
ProfileManage* manage = _defaultProfileManage; ProfileHelper* manage = _defaultProfileHelper;
if (profile && profile != &manage->profile){ if (profile && profile != &manage->profile){
CC_ASSERT(!profile->name.empty()); CC_ASSERT(!profile->name.empty());
manage = &_audioPathProfileManageMap[profile->name]; manage = &_audioPathProfileHelperMap[profile->name];
manage->profile = *profile; manage->profile = *profile;
} }
@ -133,7 +133,7 @@ int AudioEngine::play2d(const std::string& filePath, bool loop, float volume, co
manage->lastPlayTime = utils::gettime(); manage->lastPlayTime = utils::gettime();
manage->audioIDs.push_back(ret); manage->audioIDs.push_back(ret);
} }
audioRef.profileManage = manage; audioRef.profileHelper = manage;
} }
} while (0); } while (0);
@ -152,15 +152,18 @@ void AudioEngine::setLoop(int audioID, bool loop)
void AudioEngine::setVolume(int audioID, float volume) void AudioEngine::setVolume(int audioID, float volume)
{ {
auto it = _audioIDInfoMap.find(audioID); auto it = _audioIDInfoMap.find(audioID);
if (it != _audioIDInfoMap.end() && it->second.volume != volume){ if (it != _audioIDInfoMap.end()){
if (volume < 0.0f) { if (volume < 0.0f) {
volume = 0.0f; volume = 0.0f;
} }
else if (volume > 1.0f){ else if (volume > 1.0f){
volume = 1.0f; volume = 1.0f;
} }
_audioEngineImpl->setVolume(audioID, volume);
it->second.volume = volume; if (it->second.volume != volume){
_audioEngineImpl->setVolume(audioID, volume);
it->second.volume = volume;
}
} }
} }
@ -222,8 +225,8 @@ void AudioEngine::remove(int audioID)
{ {
auto it = _audioIDInfoMap.find(audioID); auto it = _audioIDInfoMap.find(audioID);
if (it != _audioIDInfoMap.end()){ if (it != _audioIDInfoMap.end()){
if (it->second.profileManage) { if (it->second.profileHelper) {
it->second.profileManage->audioIDs.remove(audioID); it->second.profileHelper->audioIDs.remove(audioID);
} }
_audioPathIDMap[*it->second.filePath].remove(audioID); _audioPathIDMap[*it->second.filePath].remove(audioID);
_audioIDInfoMap.erase(audioID); _audioIDInfoMap.erase(audioID);
@ -236,8 +239,8 @@ void AudioEngine::stopAll()
auto itEnd = _audioIDInfoMap.end(); auto itEnd = _audioIDInfoMap.end();
for (auto it = _audioIDInfoMap.begin(); it != itEnd; ++it) for (auto it = _audioIDInfoMap.begin(); it != itEnd; ++it)
{ {
if (it->second.profileManage){ if (it->second.profileHelper){
it->second.profileManage->audioIDs.remove(it->first); it->second.profileHelper->audioIDs.remove(it->first);
} }
} }
_audioPathIDMap.clear(); _audioPathIDMap.clear();
@ -254,8 +257,8 @@ void AudioEngine::uncache(const std::string &filePath)
auto itInfo = _audioIDInfoMap.find(audioID); auto itInfo = _audioIDInfoMap.find(audioID);
if (itInfo != _audioIDInfoMap.end()){ if (itInfo != _audioIDInfoMap.end()){
if (itInfo->second.profileManage) { if (itInfo->second.profileHelper) {
itInfo->second.profileManage->audioIDs.remove(audioID); itInfo->second.profileHelper->audioIDs.remove(audioID);
} }
_audioIDInfoMap.erase(audioID); _audioIDInfoMap.erase(audioID);
} }
@ -363,7 +366,7 @@ AudioProfile* AudioEngine::getProfile(int audioID)
auto it = _audioIDInfoMap.find(audioID); auto it = _audioIDInfoMap.find(audioID);
if (it != _audioIDInfoMap.end()) if (it != _audioIDInfoMap.end())
{ {
return &it->second.profileManage->profile; return &it->second.profileHelper->profile;
} }
return nullptr; return nullptr;
@ -371,18 +374,18 @@ AudioProfile* AudioEngine::getProfile(int audioID)
AudioProfile* AudioEngine::getDefaultProfile() AudioProfile* AudioEngine::getDefaultProfile()
{ {
if (_defaultProfileManage == nullptr) if (_defaultProfileHelper == nullptr)
{ {
_defaultProfileManage = new (std::nothrow) ProfileManage(); _defaultProfileHelper = new (std::nothrow) ProfileHelper();
} }
return &_defaultProfileManage->profile; return &_defaultProfileHelper->profile;
} }
AudioProfile* AudioEngine::getProfile(const std::string &name) AudioProfile* AudioEngine::getProfile(const std::string &name)
{ {
auto it = _audioPathProfileManageMap.find(name); auto it = _audioPathProfileHelperMap.find(name);
if (it != _audioPathProfileManageMap.end()) { if (it != _audioPathProfileHelperMap.end()) {
return &it->second.profile; return &it->second.profile;
} else { } else {
return nullptr; return nullptr;

View File

@ -211,35 +211,25 @@ protected:
static void remove(int audioID); static void remove(int audioID);
class ProfileManage struct ProfileHelper
{ {
public:
AudioProfile profile; AudioProfile profile;
ProfileManage()
: lastPlayTime(0.0)
{
}
std::list<int> audioIDs; std::list<int> audioIDs;
double lastPlayTime; double lastPlayTime;
};
class AudioInfo ProfileHelper()
{ : lastPlayTime(0.0)
public:
AudioInfo()
: profileManage(nullptr)
, duration(TIME_UNKNOWN)
, state(AudioState::INITIALZING)
{ {
} }
};
struct AudioInfo
{
const std::string* filePath; const std::string* filePath;
ProfileManage* profileManage; ProfileHelper* profileHelper;
float volume; float volume;
bool loop; bool loop;
@ -247,6 +237,14 @@ protected:
AudioState state; AudioState state;
bool is3dAudio; bool is3dAudio;
AudioInfo()
: profileHelper(nullptr)
, duration(TIME_UNKNOWN)
, state(AudioState::INITIALZING)
{
}
}; };
//audioID,audioAttribute //audioID,audioAttribute
@ -255,12 +253,12 @@ protected:
//audio file path,audio IDs //audio file path,audio IDs
static std::unordered_map<std::string,std::list<int>> _audioPathIDMap; static std::unordered_map<std::string,std::list<int>> _audioPathIDMap;
//profileName,ProfileManage //profileName,ProfileHelper
static std::unordered_map<std::string, ProfileManage> _audioPathProfileManageMap; static std::unordered_map<std::string, ProfileHelper> _audioPathProfileHelperMap;
static int _maxInstances; static int _maxInstances;
static ProfileManage* _defaultProfileManage; static ProfileHelper* _defaultProfileHelper;
static AudioEngineImpl* _audioEngineImpl; static AudioEngineImpl* _audioEngineImpl;

View File

@ -33,7 +33,6 @@
#include "AudioPlayer.h" #include "AudioPlayer.h"
NS_CC_BEGIN NS_CC_BEGIN
class AudioProfile;
#define kMaxSources 32 #define kMaxSources 32