diff --git a/core/Base/Audio.cpp b/core/Base/Audio.cpp new file mode 100644 index 00000000..2629608e --- /dev/null +++ b/core/Base/Audio.cpp @@ -0,0 +1,60 @@ +#include "..\e2dbase.h" + + +e2d::Audio * e2d::Audio::_instance = nullptr; + +e2d::Audio * e2d::Audio::getInstance() +{ + if (!_instance) + { + _instance = new (std::nothrow) Audio; + } + return _instance; +} + +void e2d::Audio::destroyInstance() +{ + if (_instance) + { + delete _instance; + _instance = nullptr; + } +} + +IXAudio2 * e2d::Audio::getXAudio2() +{ + return _xAudio2; +} + +IXAudio2MasteringVoice * e2d::Audio::getMasteringVoice() +{ + return _masteringVoice; +} + +e2d::Audio::Audio() + : _xAudio2(nullptr) + , _masteringVoice(nullptr) +{ + ::CoInitialize(nullptr); + + ThrowIfFailed( + XAudio2Create(&_xAudio2, 0) + ); + + ThrowIfFailed( + _xAudio2->CreateMasteringVoice(&_masteringVoice) + ); +} + +e2d::Audio::~Audio() +{ + if (_masteringVoice) + { + _masteringVoice->DestroyVoice(); + _masteringVoice = nullptr; + } + + SafeRelease(_xAudio2); + + ::CoUninitialize(); +} diff --git a/core/Base/GC.cpp b/core/Base/GC.cpp index 0d47648c..d601d3fb 100644 --- a/core/Base/GC.cpp +++ b/core/Base/GC.cpp @@ -22,6 +22,12 @@ void operator delete(void * block, e2d::autorelease_t const &) E2D_NOEXCEPT } +e2d::GC * e2d::GC::getInstance() +{ + static GC _instance; + return &_instance; +} + e2d::GC::GC() : _notifyed(false) , _cleanup(false) @@ -32,12 +38,29 @@ e2d::GC::GC() e2d::GC::~GC() { // 删除所有对象 - this->clear(); + Game::getInstance()->clearAllScenes(); + Timer::getInstance()->clearAllTasks(); + ActionManager::getInstance()->clearAll(); - // 清除图片缓存 + _cleanup = true; + for (const auto& ref : _pool) + { + delete ref; + } + _pool.clear(); + _cleanup = false; + + // 清除缓存 Image::clearCache(); -} + // 清除单例 + Player::destroyInstance(); + Audio::destroyInstance(); + Renderer::destroyInstance(); + Input::destroyInstance(); + Window::destroyInstance(); + Game::destroyInstance(); +} void e2d::GC::flush() { @@ -59,37 +82,6 @@ void e2d::GC::flush() } } -void e2d::GC::clear() -{ - _cleanup = true; - - Game::getInstance()->clearAllScenes(); - Timer::getInstance()->clearAllTasks(); - ActionManager::getInstance()->clearAll(); - - for (const auto& ref : _pool) - { - delete ref; - } - _pool.clear(); - _cleanup = false; - - // 清除缓存 - Image::clearCache(); - - // 清除单例 - Game::destroyInstance(); - Renderer::destroyInstance(); - Input::destroyInstance(); - Window::destroyInstance(); -} - -e2d::GC * e2d::GC::getInstance() -{ - static GC _instance; - return &_instance; -} - void e2d::GC::autorelease(Ref * ref) { if (ref) diff --git a/core/Tool/Music.cpp b/core/Tool/Music.cpp index ea966e66..1a4c310f 100644 --- a/core/Tool/Music.cpp +++ b/core/Tool/Music.cpp @@ -22,44 +22,6 @@ inline bool TraceError(wchar_t* sPrompt, HRESULT hr) } -e2d::Music::XAudio2Tool::XAudio2Tool() -{ - ::CoInitialize(nullptr); - - ThrowIfFailed( - XAudio2Create(&_xAudio2, 0) - ); - - ThrowIfFailed( - _xAudio2->CreateMasteringVoice(&_masteringVoice) - ); -} - -e2d::Music::XAudio2Tool::~XAudio2Tool() -{ - _masteringVoice->DestroyVoice(); - _xAudio2->Release(); - - ::CoUninitialize(); -} - -e2d::Music::XAudio2Tool* e2d::Music::XAudio2Tool::getInstance() -{ - static XAudio2Tool instance; - return &instance; -} - -IXAudio2 * e2d::Music::XAudio2Tool::getXAudio2() -{ - return _xAudio2; -} - -IXAudio2MasteringVoice * e2d::Music::XAudio2Tool::getMasteringVoice() -{ - return _masteringVoice; -} - - e2d::Music::Music() : _opened(false) , _wfx(nullptr) @@ -70,8 +32,6 @@ e2d::Music::Music() , _voice(nullptr) , _voiceCallback() { - auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2(); - xAudio2->AddRef(); } e2d::Music::Music(const e2d::String & filePath) @@ -84,9 +44,6 @@ e2d::Music::Music(const e2d::String & filePath) , _voice(nullptr) , _voiceCallback() { - auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2(); - xAudio2->AddRef(); - this->open(filePath); } @@ -100,18 +57,12 @@ e2d::Music::Music(const Resource& res) , _voice(nullptr) , _voiceCallback() { - auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2(); - xAudio2->AddRef(); - this->open(res); } e2d::Music::~Music() { close(); - - auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2(); - xAudio2->Release(); } bool e2d::Music::open(const e2d::String & filePath) @@ -173,7 +124,7 @@ bool e2d::Music::open(const e2d::String & filePath) } // 创建音源 - auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2(); + auto xAudio2 = Audio::getInstance()->getXAudio2(); HRESULT hr = xAudio2->CreateSourceVoice(&_voice, _wfx, 0, XAUDIO2_DEFAULT_FREQ_RATIO, &_voiceCallback); if (FAILED(hr)) @@ -251,7 +202,7 @@ bool e2d::Music::open(const Resource& res) } // 创建音源 - auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2(); + auto xAudio2 = Audio::getInstance()->getXAudio2(); HRESULT hr = xAudio2->CreateSourceVoice(&_voice, _wfx, 0, XAUDIO2_DEFAULT_FREQ_RATIO, &_voiceCallback); if (FAILED(hr)) diff --git a/core/Tool/Player.cpp b/core/Tool/Player.cpp index bcd89b40..7c655652 100644 --- a/core/Tool/Player.cpp +++ b/core/Tool/Player.cpp @@ -1,6 +1,26 @@ #include "..\e2dtool.h" +e2d::Player * e2d::Player::_instance = nullptr; + +e2d::Player * e2d::Player::getInstance() +{ + if (!_instance) + { + _instance = new (std::nothrow) Player; + } + return _instance; +} + +void e2d::Player::destroyInstance() +{ + if (_instance) + { + delete _instance; + _instance = nullptr; + } +} + e2d::Player::Player() : _volume(1.f) { @@ -15,8 +35,6 @@ e2d::Player::~Player() delete pair.second; } } - - CoUninitialize(); } bool e2d::Player::preload(const String & filePath) diff --git a/core/e2dbase.h b/core/e2dbase.h index 9114d023..d713f4bc 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -139,65 +139,6 @@ private: }; -// 输入设备 -class Input -{ -public: - // 获取输入设备实例 - static Input * getInstance(); - - // 销毁输入设备实例 - static void destroyInstance(); - - // 检测键盘某按键是否正被按下 - bool isDown( - KeyCode key - ); - - // 检测鼠标按键是否正被按下 - bool isDown( - MouseCode code - ); - - // 获得鼠标X轴坐标值 - float getMouseX(); - - // 获得鼠标Y轴坐标值 - float getMouseY(); - - // 获得鼠标坐标值 - Point getMousePos(); - - // 获得鼠标X轴坐标增量 - float getMouseDeltaX(); - - // 获得鼠标Y轴坐标增量 - float getMouseDeltaY(); - - // 获得鼠标Z轴(鼠标滚轮)坐标增量 - float getMouseDeltaZ(); - - // 刷新输入设备状态 - void update(); - -protected: - Input(); - - ~Input(); - - E2D_DISABLE_COPY(Input); - -protected: - IDirectInput8W* _directInput; - IDirectInputDevice8W* _keyboardDevice; - IDirectInputDevice8W* _mouseDevice; - DIMOUSESTATE _mouseState; - char _keyBuffer[256]; - - static Input * _instance; -}; - - // 渲染器 class Renderer { @@ -283,6 +224,96 @@ protected: }; +// 输入设备 +class Input +{ +public: + // 获取输入设备实例 + static Input * getInstance(); + + // 销毁输入设备实例 + static void destroyInstance(); + + // 检测键盘某按键是否正被按下 + bool isDown( + KeyCode key + ); + + // 检测鼠标按键是否正被按下 + bool isDown( + MouseCode code + ); + + // 获得鼠标X轴坐标值 + float getMouseX(); + + // 获得鼠标Y轴坐标值 + float getMouseY(); + + // 获得鼠标坐标值 + Point getMousePos(); + + // 获得鼠标X轴坐标增量 + float getMouseDeltaX(); + + // 获得鼠标Y轴坐标增量 + float getMouseDeltaY(); + + // 获得鼠标Z轴(鼠标滚轮)坐标增量 + float getMouseDeltaZ(); + + // 刷新输入设备状态 + void update(); + +protected: + Input(); + + ~Input(); + + E2D_DISABLE_COPY(Input); + +protected: + IDirectInput8W * _directInput; + IDirectInputDevice8W* _keyboardDevice; + IDirectInputDevice8W* _mouseDevice; + DIMOUSESTATE _mouseState; + char _keyBuffer[256]; + + static Input * _instance; +}; + + +// 音频设备 +class Audio +{ +public: + // 获取音频设备实例 + static Audio * getInstance(); + + // 销毁实例 + static void destroyInstance(); + + // 获取 XAudio2 实例对象 + IXAudio2 * getXAudio2(); + + // 获取 MasteringVoice 实例对象 + IXAudio2MasteringVoice* getMasteringVoice(); + +protected: + Audio(); + + virtual ~Audio(); + + E2D_DISABLE_COPY(Audio); + +protected: + IXAudio2 * _xAudio2; + IXAudio2MasteringVoice* _masteringVoice; + + static Audio * _instance; +}; + + class Timer; class ActionManager; class Scene; @@ -395,9 +426,6 @@ public: // 刷新内存池 void flush(); - // 回收内存池中的所有对象 - void clear(); - private: GC(); diff --git a/core/e2dtool.h b/core/e2dtool.h index b2c53f7b..6f127daa 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -115,27 +115,6 @@ public: // 获取 IXAudio2SourceVoice 对象 IXAudio2SourceVoice * getIXAudio2SourceVoice() const; -public: - class XAudio2Tool - { - public: - XAudio2Tool(); - - ~XAudio2Tool(); - - static XAudio2Tool* getInstance(); - - // 获取 XAudio2 实例对象 - IXAudio2 * getXAudio2(); - - // 获取 MasteringVoice 实例对象 - IXAudio2MasteringVoice* getMasteringVoice(); - - protected: - IXAudio2 * _xAudio2; - IXAudio2MasteringVoice* _masteringVoice; - }; - protected: bool _readMMIO(); @@ -169,12 +148,12 @@ protected: // 音乐播放器 class Player { - friend class Game; - public: - Player(); + // 获取播放器实例 + static Player * getInstance(); - ~Player(); + // 销毁实例 + static void destroyInstance(); // 预加载音乐资源 bool preload( @@ -258,9 +237,18 @@ public: // 清空音乐缓存 void clearCache(); -private: +protected: + Player(); + + ~Player(); + + E2D_DISABLE_COPY(Player); + +protected: float _volume; std::map _musicList; + + static Player * _instance; }; diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index 7424e7bc..19f6b7c2 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -51,6 +51,7 @@ + diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index ced3f30e..614b698b 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -238,5 +238,8 @@ Node + + Base + \ No newline at end of file diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index d9b81f0b..0c351db4 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -195,6 +195,7 @@ + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 03a44d54..e7000d78 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -238,5 +238,8 @@ Node + + Base + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index e29b7f68..87b87b36 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -215,6 +215,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index fe4a33d1..4c27e902 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -231,6 +231,9 @@ Node + + Base +