增加Audio类

This commit is contained in:
Nomango 2018-09-02 14:30:48 +08:00
parent 2e161ff6a8
commit 637fe89e37
12 changed files with 224 additions and 175 deletions

60
core/Base/Audio.cpp Normal file
View File

@ -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();
}

View File

@ -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() e2d::GC::GC()
: _notifyed(false) : _notifyed(false)
, _cleanup(false) , _cleanup(false)
@ -32,12 +38,29 @@ e2d::GC::GC()
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(); Image::clearCache();
}
// 헌뇜데절
Player::destroyInstance();
Audio::destroyInstance();
Renderer::destroyInstance();
Input::destroyInstance();
Window::destroyInstance();
Game::destroyInstance();
}
void e2d::GC::flush() 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) void e2d::GC::autorelease(Ref * ref)
{ {
if (ref) if (ref)

View File

@ -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() e2d::Music::Music()
: _opened(false) : _opened(false)
, _wfx(nullptr) , _wfx(nullptr)
@ -70,8 +32,6 @@ e2d::Music::Music()
, _voice(nullptr) , _voice(nullptr)
, _voiceCallback() , _voiceCallback()
{ {
auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2();
xAudio2->AddRef();
} }
e2d::Music::Music(const e2d::String & filePath) e2d::Music::Music(const e2d::String & filePath)
@ -84,9 +44,6 @@ e2d::Music::Music(const e2d::String & filePath)
, _voice(nullptr) , _voice(nullptr)
, _voiceCallback() , _voiceCallback()
{ {
auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2();
xAudio2->AddRef();
this->open(filePath); this->open(filePath);
} }
@ -100,18 +57,12 @@ e2d::Music::Music(const Resource& res)
, _voice(nullptr) , _voice(nullptr)
, _voiceCallback() , _voiceCallback()
{ {
auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2();
xAudio2->AddRef();
this->open(res); this->open(res);
} }
e2d::Music::~Music() e2d::Music::~Music()
{ {
close(); close();
auto xAudio2 = XAudio2Tool::getInstance()->getXAudio2();
xAudio2->Release();
} }
bool e2d::Music::open(const e2d::String & filePath) 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); HRESULT hr = xAudio2->CreateSourceVoice(&_voice, _wfx, 0, XAUDIO2_DEFAULT_FREQ_RATIO, &_voiceCallback);
if (FAILED(hr)) 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); HRESULT hr = xAudio2->CreateSourceVoice(&_voice, _wfx, 0, XAUDIO2_DEFAULT_FREQ_RATIO, &_voiceCallback);
if (FAILED(hr)) if (FAILED(hr))

View File

@ -1,6 +1,26 @@
#include "..\e2dtool.h" #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() e2d::Player::Player()
: _volume(1.f) : _volume(1.f)
{ {
@ -15,8 +35,6 @@ e2d::Player::~Player()
delete pair.second; delete pair.second;
} }
} }
CoUninitialize();
} }
bool e2d::Player::preload(const String & filePath) bool e2d::Player::preload(const String & filePath)

View File

@ -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 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 Timer;
class ActionManager; class ActionManager;
class Scene; class Scene;
@ -395,9 +426,6 @@ public:
// 刷新内存池 // 刷新内存池
void flush(); void flush();
// 回收内存池中的所有对象
void clear();
private: private:
GC(); GC();

View File

@ -115,27 +115,6 @@ public:
// 삿혤 IXAudio2SourceVoice 뚤蹶 // 삿혤 IXAudio2SourceVoice 뚤蹶
IXAudio2SourceVoice * getIXAudio2SourceVoice() const; IXAudio2SourceVoice * getIXAudio2SourceVoice() const;
public:
class XAudio2Tool
{
public:
XAudio2Tool();
~XAudio2Tool();
static XAudio2Tool* getInstance();
// 获取 XAudio2 实例对象
IXAudio2 * getXAudio2();
// 获取 MasteringVoice 实例对象
IXAudio2MasteringVoice* getMasteringVoice();
protected:
IXAudio2 * _xAudio2;
IXAudio2MasteringVoice* _masteringVoice;
};
protected: protected:
bool _readMMIO(); bool _readMMIO();
@ -169,12 +148,12 @@ protected:
// 稜있꺄렴포 // 稜있꺄렴포
class Player class Player
{ {
friend class Game;
public: public:
Player(); // 获取播放器实例
static Player * getInstance();
~Player(); // 销毁实例
static void destroyInstance();
// 渡속潼稜있栗都 // 渡속潼稜있栗都
bool preload( bool preload(
@ -258,9 +237,18 @@ public:
// 헌왕稜있뻠닸 // 헌왕稜있뻠닸
void clearCache(); void clearCache();
private: protected:
Player();
~Player();
E2D_DISABLE_COPY(Player);
protected:
float _volume; float _volume;
std::map<size_t, Music*> _musicList; std::map<size_t, Music*> _musicList;
static Player * _instance;
}; };

View File

@ -51,6 +51,7 @@
<ClCompile Include="..\..\core\Action\ScaleTo.cpp" /> <ClCompile Include="..\..\core\Action\ScaleTo.cpp" />
<ClCompile Include="..\..\core\Action\Sequence.cpp" /> <ClCompile Include="..\..\core\Action\Sequence.cpp" />
<ClCompile Include="..\..\core\Action\Spawn.cpp" /> <ClCompile Include="..\..\core\Action\Spawn.cpp" />
<ClCompile Include="..\..\core\Base\Audio.cpp" />
<ClCompile Include="..\..\core\Base\Game.cpp" /> <ClCompile Include="..\..\core\Base\Game.cpp" />
<ClCompile Include="..\..\core\Base\GC.cpp" /> <ClCompile Include="..\..\core\Base\GC.cpp" />
<ClCompile Include="..\..\core\Base\Input.cpp" /> <ClCompile Include="..\..\core\Base\Input.cpp" />

View File

@ -238,5 +238,8 @@
<ClCompile Include="..\..\core\Node\Canvas.cpp"> <ClCompile Include="..\..\core\Node\Canvas.cpp">
<Filter>Node</Filter> <Filter>Node</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Base\Audio.cpp">
<Filter>Base</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -195,6 +195,7 @@
<ClCompile Include="..\..\core\Action\ScaleTo.cpp" /> <ClCompile Include="..\..\core\Action\ScaleTo.cpp" />
<ClCompile Include="..\..\core\Action\Sequence.cpp" /> <ClCompile Include="..\..\core\Action\Sequence.cpp" />
<ClCompile Include="..\..\core\Action\Spawn.cpp" /> <ClCompile Include="..\..\core\Action\Spawn.cpp" />
<ClCompile Include="..\..\core\Base\Audio.cpp" />
<ClCompile Include="..\..\core\Base\Game.cpp" /> <ClCompile Include="..\..\core\Base\Game.cpp" />
<ClCompile Include="..\..\core\Base\GC.cpp" /> <ClCompile Include="..\..\core\Base\GC.cpp" />
<ClCompile Include="..\..\core\Base\Input.cpp" /> <ClCompile Include="..\..\core\Base\Input.cpp" />

View File

@ -238,5 +238,8 @@
<ClCompile Include="..\..\core\Node\Canvas.cpp"> <ClCompile Include="..\..\core\Node\Canvas.cpp">
<Filter>Node</Filter> <Filter>Node</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Base\Audio.cpp">
<Filter>Base</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -215,6 +215,7 @@
<ClCompile Include="..\..\core\Action\Sequence.cpp" /> <ClCompile Include="..\..\core\Action\Sequence.cpp" />
<ClCompile Include="..\..\core\Action\FiniteTimeAction.cpp" /> <ClCompile Include="..\..\core\Action\FiniteTimeAction.cpp" />
<ClCompile Include="..\..\core\Action\Spawn.cpp" /> <ClCompile Include="..\..\core\Action\Spawn.cpp" />
<ClCompile Include="..\..\core\Base\Audio.cpp" />
<ClCompile Include="..\..\core\Base\Game.cpp" /> <ClCompile Include="..\..\core\Base\Game.cpp" />
<ClCompile Include="..\..\core\Base\GC.cpp" /> <ClCompile Include="..\..\core\Base\GC.cpp" />
<ClCompile Include="..\..\core\Base\Input.cpp" /> <ClCompile Include="..\..\core\Base\Input.cpp" />

View File

@ -231,6 +231,9 @@
<ClCompile Include="..\..\core\Node\Canvas.cpp"> <ClCompile Include="..\..\core\Node\Canvas.cpp">
<Filter>Node</Filter> <Filter>Node</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Base\Audio.cpp">
<Filter>Base</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" /> <ClInclude Include="..\..\core\easy2d.h" />