diff --git a/core/Base/Config.cpp b/core/Base/Config.cpp index 8853a31d..0a59dcb4 100644 --- a/core/Base/Config.cpp +++ b/core/Base/Config.cpp @@ -3,7 +3,6 @@ e2d::Config::Config() : _gameName() - , _soundEnabled(true) , _showFps(false) , _outlineVisible(false) , _collisionEnabled(false) @@ -30,11 +29,6 @@ void e2d::Config::setOutlineVisible(bool visible) _outlineVisible = visible; } -void e2d::Config::setSoundEnabled(bool enabled) -{ - _soundEnabled = enabled; -} - void e2d::Config::setCollisionEnabled(bool enabled) { _collisionEnabled = enabled; @@ -50,11 +44,6 @@ e2d::String e2d::Config::getGameName() const return _gameName; } -bool e2d::Config::isSoundEnabled() const -{ - return _soundEnabled; -} - bool e2d::Config::isFpsShow() const { return _showFps; diff --git a/core/Base/GC.cpp b/core/Base/GC.cpp index 9bd214f8..c9c1970b 100644 --- a/core/Base/GC.cpp +++ b/core/Base/GC.cpp @@ -36,15 +36,16 @@ e2d::GC::~GC() { // 删除所有对象 this->clear(); + // 清除图片缓存 Image::clearCache(); + // 删除所有单例 Game::destroyInstance(); Renderer::destroyInstance(); Input::destroyInstance(); Window::destroyInstance(); Timer::destroyInstance(); - Player::destroyInstance(); SceneManager::destroyInstance(); ActionManager::destroyInstance(); CollisionManager::destroyInstance(); diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 4c7d80ca..88056f9c 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -100,14 +100,6 @@ bool e2d::Game::isPaused() void e2d::Game::setConfig(const Config& config) { - if (_config.isSoundEnabled() != config.isSoundEnabled()) - { - if (config.isSoundEnabled()) - Player::getInstance()->getXAudio2()->StartEngine(); - else - Player::getInstance()->getXAudio2()->StopEngine(); - } - _config = config; } @@ -125,5 +117,4 @@ void e2d::Game::cleanup() { GC::getInstance()->clear(); Image::clearCache(); - Player::getInstance()->clearCache(); } diff --git a/core/Tool/Music.cpp b/core/Tool/Music.cpp index ab807425..2f6e73af 100644 --- a/core/Tool/Music.cpp +++ b/core/Tool/Music.cpp @@ -32,21 +32,10 @@ e2d::Music::Music() , _dwSize(0) , _voice(nullptr) , _voiceCallback(this) + , _xAudio2(nullptr) + , _masteringVoice(nullptr) { -} - -e2d::Music::Music(const e2d::String & filePath) - : _opened(false) - , _playing(false) - , _wfx(nullptr) - , _hmmio(nullptr) - , _resBuffer(nullptr) - , _waveData(nullptr) - , _dwSize(0) - , _voice(nullptr) - , _voiceCallback(this) -{ - this->open(filePath); + CoInitialize(nullptr); } e2d::Music::Music(const Resource& res) @@ -59,26 +48,70 @@ e2d::Music::Music(const Resource& res) , _dwSize(0) , _voice(nullptr) , _voiceCallback(this) + , _xAudio2(nullptr) + , _masteringVoice(nullptr) { + CoInitialize(nullptr); + this->open(res); } +e2d::Music::Music(IXAudio2 * xAudio2) + : _opened(false) + , _playing(false) + , _wfx(nullptr) + , _hmmio(nullptr) + , _resBuffer(nullptr) + , _waveData(nullptr) + , _dwSize(0) + , _voice(nullptr) + , _voiceCallback(this) + , _xAudio2(xAudio2) + , _masteringVoice(nullptr) +{ + CoInitialize(nullptr); + + if (_xAudio2) + { + _xAudio2->AddRef(); + } +} + e2d::Music::~Music() { close(); -} -bool e2d::Music::open(const e2d::String& filePath) -{ - return open(Resource(filePath)); + if (_masteringVoice) + { + _masteringVoice->DestroyVoice(); + _masteringVoice = nullptr; + } + + SafeRelease(_xAudio2); + + CoUninitialize(); } bool e2d::Music::open(const Resource& res) { if (_opened) { - WARN("MusicInfo can be opened only once!"); - return false; + close(); + } + + if (!_xAudio2) + { + if (FAILED(XAudio2Create(&_xAudio2, 0))) + { + TraceError(L"Create IXAudio2 error"); + return false; + } + + if (FAILED(_xAudio2->CreateMasteringVoice(&_masteringVoice))) + { + TraceError(L"Create IXAudio2MasteringVoice error"); + return false; + } } if (!res.isFile()) @@ -167,13 +200,7 @@ bool e2d::Music::open(const Resource& res) } // 创建音源 - HRESULT hr = Player::getInstance()->getXAudio2()->CreateSourceVoice( - &_voice, - _wfx, - 0, - XAUDIO2_DEFAULT_FREQ_RATIO, - &this->_voiceCallback - ); + HRESULT hr = _xAudio2->CreateSourceVoice(&_voice, _wfx, 0, XAUDIO2_DEFAULT_FREQ_RATIO, &_voiceCallback); if (FAILED(hr)) { @@ -191,13 +218,13 @@ bool e2d::Music::play(int nLoopCount) { if (!_opened) { - WARN("MusicInfo::play Failed: MusicInfo must be opened first!"); + WARN("Music::play Failed: Music must be opened first!"); return false; } if (_voice == nullptr) { - WARN("MusicInfo::play Failed: IXAudio2SourceVoice Null pointer exception!"); + WARN("Music::play Failed: IXAudio2SourceVoice Null pointer exception!"); return false; } diff --git a/core/Tool/Player.cpp b/core/Tool/Player.cpp index 880a9cf1..7ac615f0 100644 --- a/core/Tool/Player.cpp +++ b/core/Tool/Player.cpp @@ -1,14 +1,23 @@ #include "..\e2dtool.h" -e2d::Player * e2d::Player::_instance = nullptr; - e2d::Player::Player() : _volume(1.f) + , _enabled(true) , _xAudio2(nullptr) , _masteringVoice(nullptr) { CoInitialize(nullptr); + + if (FAILED(XAudio2Create(&_xAudio2, 0))) + { + WARN("初始化 XAudio2 组件失败"); + } + + if (FAILED(_xAudio2->CreateMasteringVoice(&_masteringVoice))) + { + WARN("初始化 MasteringVoice 组件失败"); + } } e2d::Player::~Player() @@ -22,48 +31,24 @@ e2d::Player::~Player() } if (_masteringVoice) + { _masteringVoice->DestroyVoice(); + _masteringVoice = nullptr; + } SafeRelease(_xAudio2); CoUninitialize(); } -e2d::Player * e2d::Player::getInstance() -{ - if (!_instance) - { - _instance = new (std::nothrow) Player; - - if (FAILED(XAudio2Create(&_instance->_xAudio2, 0)) || - FAILED(_instance->_xAudio2->CreateMasteringVoice(&_instance->_masteringVoice))) - { - throw SystemException(L"初始化 XAudio2 组件失败"); - } - } - return _instance; -} - -void e2d::Player::destroyInstance() -{ - if (_instance) - { - delete _instance; - _instance = nullptr; - } -} - IXAudio2 * e2d::Player::getXAudio2() { return _xAudio2; } -bool e2d::Player::preload(const String& filePath) +IXAudio2MasteringVoice * e2d::Player::getMasteringVoice() { - if (filePath.isEmpty()) - return false; - - return preload(Resource(filePath)); + return _masteringVoice; } bool e2d::Player::preload(const Resource& res) @@ -71,9 +56,9 @@ bool e2d::Player::preload(const Resource& res) if (_musicList.end() != _musicList.find(res)) return true; - Music * music = new Music(); + Music * music = new (std::nothrow) Music(); - if (music->open(res)) + if (music && music->open(res)) { music->setVolume(_volume); _musicList.insert(std::make_pair(res, music)); @@ -82,14 +67,6 @@ bool e2d::Player::preload(const Resource& res) return false; } -bool e2d::Player::play(const String& filePath, int nLoopCount) -{ - if (filePath.isEmpty()) - return false; - - return play(Resource(filePath), nLoopCount); -} - bool e2d::Player::play(const Resource& res, int nLoopCount) { if (Player::preload(res)) @@ -103,56 +80,24 @@ bool e2d::Player::play(const Resource& res, int nLoopCount) return false; } -void e2d::Player::pause(const String& filePath) -{ - if (filePath.isEmpty()) - return; - - pause(Resource(filePath)); -} - void e2d::Player::pause(const Resource& res) { if (_musicList.end() != _musicList.find(res)) _musicList[res]->pause(); } -void e2d::Player::resume(const String& filePath) -{ - if (filePath.isEmpty()) - return; - - resume(Resource(filePath)); -} - void e2d::Player::resume(const Resource& res) { if (_musicList.end() != _musicList.find(res)) _musicList[res]->pause(); } -void e2d::Player::stop(const String& filePath) -{ - if (filePath.isEmpty()) - return; - - stop(Resource(filePath)); -} - void e2d::Player::stop(const Resource& res) { if (_musicList.end() != _musicList.find(res)) _musicList[res]->stop(); } -bool e2d::Player::isPlaying(const String& filePath) -{ - if (filePath.isEmpty()) - return false; - - return isPlaying(Resource(filePath)); -} - bool e2d::Player::isPlaying(const Resource& res) { if (_musicList.end() != _musicList.find(res)) @@ -198,6 +143,15 @@ void e2d::Player::stopAll() } } +void e2d::Player::setEnabled(bool enabled) +{ + if (_enabled == enabled) + return; + + _enabled = enabled; + _enabled ? _xAudio2->StartEngine() : _xAudio2->StopEngine(); +} + void e2d::Player::clearCache() { for (auto pair : _musicList) diff --git a/core/e2dbase.h b/core/e2dbase.h index 2764b0b6..72e4ba2b 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -36,12 +36,6 @@ public: bool visible ); - // 打开或关闭声音 - // 默认:打开 - void setSoundEnabled( - bool enabled - ); - // 打开或关闭碰撞监听 // 默认:关闭 void setCollisionEnabled( @@ -57,9 +51,6 @@ public: // 获取游戏名称 String getGameName() const; - // 获取声音打开状态 - bool isSoundEnabled() const; - // 获取 FPS 显示状态 bool isFpsShow() const; @@ -74,7 +65,6 @@ public: protected: bool _showFps; - bool _soundEnabled; bool _outlineVisible; bool _collisionEnabled; bool _colliderVisible; diff --git a/core/e2dcustom.h b/core/e2dcustom.h index a1f91e87..5a7ffce8 100644 --- a/core/e2dcustom.h +++ b/core/e2dcustom.h @@ -24,7 +24,10 @@ class VoiceCallback : public IXAudio2VoiceCallback { public: - VoiceCallback(Music * music); + explicit VoiceCallback( + Music * music + ); + ~VoiceCallback(); void __stdcall OnStreamEnd(); diff --git a/core/e2dtool.h b/core/e2dtool.h index 9c8b52be..686f3997 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -58,20 +58,15 @@ public: Music(); explicit Music( - const e2d::String& filePath /* 音乐文件路径 */ + const Resource& res /* 音乐资源 */ ); explicit Music( - const Resource& res + IXAudio2* xAudio2 ); virtual ~Music(); - // 打开音乐文件 - bool open( - const e2d::String& filePath /* 音乐文件路径 */ - ); - // 打开音乐资源 bool open( const Resource& res @@ -142,7 +137,9 @@ protected: MMCKINFO _ckRiff; WAVEFORMATEX* _wfx; VoiceCallback _voiceCallback; + IXAudio2* _xAudio2; IXAudio2SourceVoice* _voice; + IXAudio2MasteringVoice* _masteringVoice; }; @@ -150,45 +147,11 @@ protected: class Player { friend class Game; - typedef std::map MusicMap; public: - // 获取播放器实例 - static Player * getInstance(); + Player(); - // 销毁实例 - static void destroyInstance(); - - // 预加载音乐资源 - bool preload( - const String& filePath /* 音乐文件路径 */ - ); - - // 播放音乐 - bool play( - const String& filePath, /* 音乐文件路径 */ - int nLoopCount = 0 /* 重复播放次数,设置 -1 为循环播放 */ - ); - - // 暂停音乐 - void pause( - const String& filePath /* 音乐文件路径 */ - ); - - // 继续播放音乐 - void resume( - const String& filePath /* 音乐文件路径 */ - ); - - // 停止音乐 - void stop( - const String& filePath /* 音乐文件路径 */ - ); - - // 获取音乐播放状态 - bool isPlaying( - const String& filePath /* 音乐文件路径 */ - ); + ~Player(); // 预加载音乐资源 bool preload( @@ -226,7 +189,7 @@ public: // 设置音量 void setVolume( - float volume /* 音量范围为 -224 ~ 224,0 是静音,1 是正常音量 */ + float volume /* 音量范围为 -224 ~ 224,0 是静音,1 是正常音量 */ ); // 暂停所有音乐 @@ -238,26 +201,26 @@ public: // 停止所有音乐 void stopAll(); + // 打开或关闭播放器 + void setEnabled( + bool enabled + ); + // 清空音乐缓存 void clearCache(); - // 获取 IXAudio2 对象 + // 获取 XAudio2 实例对象 IXAudio2 * getXAudio2(); -private: - Player(); - - ~Player(); - - E2D_DISABLE_COPY(Player); + // 获取 MasteringVoice 实例对象 + IXAudio2MasteringVoice* getMasteringVoice(); private: - float _volume; - MusicMap _musicList; - IXAudio2* _xAudio2; - IXAudio2MasteringVoice* _masteringVoice; - - static Player * _instance; + bool _enabled; + float _volume; + IXAudio2* _xAudio2; + IXAudio2MasteringVoice* _masteringVoice; + std::map _musicList; }; diff --git a/core/easy2d.h b/core/easy2d.h index ea6c6991..f4fc06ff 100644 --- a/core/easy2d.h +++ b/core/easy2d.h @@ -22,7 +22,6 @@ #include "e2dbase.h" #include "e2dmanager.h" #include "e2dnode.h" -#include "e2dshape.h" #include "e2dtool.h" #include "e2daction.h" #include "e2dtransition.h"