Update MusicManager

This commit is contained in:
Nomango 2018-03-02 23:49:57 +08:00
parent 2e88860b32
commit 49783dbf8d
13 changed files with 206 additions and 108 deletions

View File

@ -45,7 +45,7 @@ void e2d::Animation::_update()
// 重新记录时间 // 重新记录时间
m_fLast += m_fInterval; m_fLast += m_fInterval;
// 加载关键帧 // 加载关键帧
static_cast<Sprite*>(m_pTarget)->loadFrom(m_vFrames[m_nFrameIndex]); static_cast<Sprite*>(m_pTarget)->open(m_vFrames[m_nFrameIndex]);
m_nFrameIndex++; m_nFrameIndex++;
// 判断动作是否结束 // 判断动作是否结束
if (m_nFrameIndex == m_vFrames.size()) if (m_nFrameIndex == m_vFrames.size())

View File

@ -15,12 +15,12 @@ e2d::Image::Image()
e2d::Image::Image(const String & strFileName) e2d::Image::Image(const String & strFileName)
{ {
this->loadFrom(strFileName); this->open(strFileName);
} }
e2d::Image::Image(const String & strFileName, double nClipX, double nClipY, double nClipWidth, double nClipHeight) e2d::Image::Image(const String & strFileName, double nClipX, double nClipY, double nClipWidth, double nClipHeight)
{ {
this->loadFrom(strFileName); this->open(strFileName);
this->clip(nClipX, nClipY, nClipWidth, nClipHeight); this->clip(nClipX, nClipY, nClipWidth, nClipHeight);
} }
@ -28,7 +28,7 @@ e2d::Image::~Image()
{ {
} }
void e2d::Image::loadFrom(const String & strFilePath) void e2d::Image::open(const String & strFilePath)
{ {
WARN_IF(strFilePath.isEmpty(), "Image cannot load bitmap from NULL file name."); WARN_IF(strFilePath.isEmpty(), "Image cannot load bitmap from NULL file name.");
@ -47,9 +47,9 @@ void e2d::Image::loadFrom(const String & strFilePath)
m_fSourceClipHeight = m_pBitmap->GetSize().height; m_fSourceClipHeight = m_pBitmap->GetSize().height;
} }
void e2d::Image::loadFrom(const String & strFilePath, double x, double y, double width, double height) void e2d::Image::open(const String & strFilePath, double x, double y, double width, double height)
{ {
loadFrom(strFilePath); open(strFilePath);
clip(x, y, width, height); clip(x, y, width, height);
} }

View File

@ -5,7 +5,7 @@ e2d::Obj::Obj()
: m_nRefCount(0) : m_nRefCount(0)
, m_bManaged(false) , m_bManaged(false)
{ {
ObjectManager::add(this); // 将该对象放入释放池中 ObjectManager::preload(this); // 将该对象放入释放池中
} }
e2d::Obj::~Obj() e2d::Obj::~Obj()

View File

@ -51,7 +51,7 @@ void e2d::Scene::setAutoUpdate(bool bAutoUpdate)
m_bAutoUpdate = bAutoUpdate; m_bAutoUpdate = bAutoUpdate;
} }
void e2d::Scene::add(Node * child, int order /* = 0 */) void e2d::Scene::preload(Node * child, int order /* = 0 */)
{ {
m_pRoot->addChild(child, order); m_pRoot->addChild(child, order);
} }

View File

@ -15,29 +15,72 @@ static MusicList& getMusicList()
} }
bool e2d::MusicManager::add(const String & strFilePath) bool e2d::MusicManager::preload(const String & strFilePath)
{ {
Music * pPlayer = get(strFilePath); UINT nRet = strFilePath.getHashCode();
if (pPlayer)
if (getMusicList().end() != getMusicList().find(nRet))
{ {
return true; return true;
} }
else else
{ {
UINT nRet = strFilePath.getHashCode(); Music * pPlayer = new Music();
pPlayer = new Music();
if (pPlayer->_open(strFilePath)) if (pPlayer->open(strFilePath))
{ {
getMusicList().insert(MusicPair(nRet, pPlayer)); getMusicList().insert(MusicPair(nRet, pPlayer));
pPlayer->retain();
return true; return true;
} }
else else
{ {
delete pPlayer; pPlayer->release();
return false; pPlayer = nullptr;
} }
} }
return false;
}
bool e2d::MusicManager::play(const String & strFilePath, int nLoopCount)
{
if (MusicManager::preload(strFilePath))
{
UINT nRet = strFilePath.getHashCode();
Music * pMusic = getMusicList()[nRet];
if (pMusic->play(nLoopCount))
{
return true;
}
}
return false;
}
void e2d::MusicManager::pause(const String & strFilePath)
{
auto music = MusicManager::get(strFilePath);
if (music)
{
music->pause();
}
}
void e2d::MusicManager::resume(const String & strFilePath)
{
auto music = MusicManager::get(strFilePath);
if (music)
{
music->resume();
}
}
void e2d::MusicManager::stop(const String & strFilePath)
{
auto music = MusicManager::get(strFilePath);
if (music)
{
music->stop();
}
} }
e2d::Music * e2d::MusicManager::get(const String & strFilePath) e2d::Music * e2d::MusicManager::get(const String & strFilePath)
@ -111,8 +154,8 @@ void e2d::MusicManager::__uninit()
{ {
for (auto iter : getMusicList()) for (auto iter : getMusicList())
{ {
iter.second->_close(); iter.second->close();
delete iter.second; iter.second->release();
} }
getMusicList().clear(); getMusicList().clear();

View File

@ -37,7 +37,7 @@ void e2d::ObjectManager::__flush()
} }
} }
void e2d::ObjectManager::add(e2d::Obj * nptr) void e2d::ObjectManager::preload(e2d::Obj * nptr)
{ {
if (!nptr->m_bManaged) if (!nptr->m_bManaged)
{ {

View File

@ -19,12 +19,12 @@ void e2d::TimerManager::__update()
} }
} }
void e2d::TimerManager::add(Timer * pTimer, Scene * pParentScene) void e2d::TimerManager::preload(Timer * pTimer, Scene * pParentScene)
{ {
TimerManager::add(pTimer, pParentScene->getRoot()); TimerManager::preload(pTimer, pParentScene->getRoot());
} }
void e2d::TimerManager::add(Timer * pTimer, Node * pParentNode) void e2d::TimerManager::preload(Timer * pTimer, Node * pParentNode)
{ {
WARN_IF(pTimer == nullptr, "Timer NULL pointer exception!"); WARN_IF(pTimer == nullptr, "Timer NULL pointer exception!");
WARN_IF(pParentNode == nullptr, "Bind Timer with a NULL Node pointer!"); WARN_IF(pParentNode == nullptr, "Bind Timer with a NULL Node pointer!");

View File

@ -9,19 +9,19 @@ e2d::Sprite::Sprite()
e2d::Sprite::Sprite(Image * image) e2d::Sprite::Sprite(Image * image)
: m_pImage(nullptr) : m_pImage(nullptr)
{ {
loadFrom(image); open(image);
} }
e2d::Sprite::Sprite(const String & imageFileName) e2d::Sprite::Sprite(const String & imageFileName)
: m_pImage(nullptr) : m_pImage(nullptr)
{ {
loadFrom(imageFileName); open(imageFileName);
} }
e2d::Sprite::Sprite(const String & imageFileName, double x, double y, double width, double height) e2d::Sprite::Sprite(const String & imageFileName, double x, double y, double width, double height)
: m_pImage(nullptr) : m_pImage(nullptr)
{ {
loadFrom(imageFileName); open(imageFileName);
clip(x, y, width, height); clip(x, y, width, height);
} }
@ -30,7 +30,7 @@ e2d::Sprite::~Sprite()
SafeRelease(&m_pImage); SafeRelease(&m_pImage);
} }
void e2d::Sprite::loadFrom(Image * image) void e2d::Sprite::open(Image * image)
{ {
if (image) if (image)
{ {
@ -42,9 +42,9 @@ void e2d::Sprite::loadFrom(Image * image)
} }
} }
void e2d::Sprite::loadFrom(const String & imageFileName) void e2d::Sprite::open(const String & imageFileName)
{ {
loadFrom(new Image(imageFileName)); open(new Image(imageFileName));
} }
void e2d::Sprite::clip(double x, double y, double width, double height) void e2d::Sprite::clip(double x, double y, double width, double height)

View File

@ -35,12 +35,25 @@ Music::Music()
{ {
} }
Music::~Music() e2d::Music::Music(const String & strFileName)
: m_bOpened(false)
, m_bPlaying(false)
, m_pwfx(nullptr)
, m_hmmio(nullptr)
, m_pResourceBuffer(nullptr)
, m_pbWaveData(nullptr)
, m_dwSize(0)
, m_pSourceVoice(nullptr)
{ {
_close(); this->open(strFileName);
} }
bool Music::_open(const String & strFileName) Music::~Music()
{
close();
}
bool Music::open(const String & strFileName)
{ {
if (m_bOpened) if (m_bOpened)
{ {
@ -50,7 +63,7 @@ bool Music::_open(const String & strFileName)
if (strFileName.isEmpty()) if (strFileName.isEmpty())
{ {
WARN_IF(true, L"Music::_open Invalid file name."); WARN_IF(true, L"Music::open Invalid file name.");
return false; return false;
} }
@ -115,7 +128,17 @@ bool Music::_open(const String & strFileName)
bool Music::play(int nLoopCount) bool Music::play(int nLoopCount)
{ {
HRESULT hr; if (!m_bOpened)
{
WARN_IF(true, "Music::play Failed: Music must be opened first!");
return false;
}
if (m_pSourceVoice == nullptr)
{
WARN_IF(true, "Music::play Failed: IXAudio2SourceVoice Null pointer exception!");
return false;
}
if (m_bPlaying) if (m_bPlaying)
{ {
@ -132,6 +155,7 @@ bool Music::play(int nLoopCount)
buffer.AudioBytes = m_dwSize; buffer.AudioBytes = m_dwSize;
buffer.LoopCount = nLoopCount; buffer.LoopCount = nLoopCount;
HRESULT hr;
if (FAILED(hr = m_pSourceVoice->SubmitSourceBuffer(&buffer))) if (FAILED(hr = m_pSourceVoice->SubmitSourceBuffer(&buffer)))
{ {
TraceError(L"Error %#X submitting source buffer", hr); TraceError(L"Error %#X submitting source buffer", hr);
@ -148,33 +172,29 @@ bool Music::play(int nLoopCount)
return SUCCEEDED(hr); return SUCCEEDED(hr);
} }
bool Music::pause() void Music::pause()
{ {
if (m_pSourceVoice) if (m_pSourceVoice)
{ {
if (SUCCEEDED(m_pSourceVoice->Stop())) if (SUCCEEDED(m_pSourceVoice->Stop()))
{ {
m_bPlaying = false; m_bPlaying = false;
return true;
} }
} }
return false;
} }
bool Music::resume() void Music::resume()
{ {
if (m_pSourceVoice) if (m_pSourceVoice)
{ {
if (SUCCEEDED(m_pSourceVoice->Start())) if (SUCCEEDED(m_pSourceVoice->Start()))
{ {
m_bPlaying = true; m_bPlaying = true;
return true;
} }
} }
return false;
} }
bool Music::stop() void Music::stop()
{ {
if (m_pSourceVoice) if (m_pSourceVoice)
{ {
@ -183,15 +203,37 @@ bool Music::stop()
m_pSourceVoice->ExitLoop(); m_pSourceVoice->ExitLoop();
m_pSourceVoice->FlushSourceBuffers(); m_pSourceVoice->FlushSourceBuffers();
m_bPlaying = false; m_bPlaying = false;
return true;
} }
} }
return false; }
void Music::close()
{
if (m_pSourceVoice)
{
m_pSourceVoice->Stop();
m_pSourceVoice->FlushSourceBuffers();
m_pSourceVoice->DestroyVoice();
m_pSourceVoice = nullptr;
}
if (m_hmmio != nullptr)
{
mmioClose(m_hmmio, 0);
m_hmmio = nullptr;
}
SAFE_DELETE_ARRAY(m_pResourceBuffer);
SAFE_DELETE_ARRAY(m_pbWaveData);
SAFE_DELETE_ARRAY(m_pwfx);
m_bOpened = false;
m_bPlaying = false;
} }
bool Music::isPlaying() bool Music::isPlaying()
{ {
if (m_pSourceVoice) if (m_bOpened && m_pSourceVoice)
{ {
XAUDIO2_VOICE_STATE state; XAUDIO2_VOICE_STATE state;
m_pSourceVoice->GetState(&state); m_pSourceVoice->GetState(&state);
@ -252,30 +294,6 @@ IXAudio2SourceVoice * Music::getIXAudio2SourceVoice() const
return m_pSourceVoice; return m_pSourceVoice;
} }
void Music::_close()
{
if (m_pSourceVoice)
{
m_pSourceVoice->Stop();
m_pSourceVoice->FlushSourceBuffers();
m_pSourceVoice->DestroyVoice();
m_pSourceVoice = nullptr;
}
if (m_hmmio != nullptr)
{
mmioClose(m_hmmio, 0);
m_hmmio = nullptr;
}
SAFE_DELETE_ARRAY(m_pResourceBuffer);
SAFE_DELETE_ARRAY(m_pbWaveData);
SAFE_DELETE_ARRAY(m_pwfx);
m_bOpened = false;
m_bPlaying = false;
}
bool Music::_readMMIO() bool Music::_readMMIO()
{ {
MMCKINFO ckIn; MMCKINFO ckIn;
@ -399,11 +417,11 @@ bool Music::_read(BYTE* pBuffer, DWORD dwSizeToRead)
return true; return true;
} }
bool Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const String & strFilename) bool Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const wchar_t * strFilename)
{ {
bool bFound = false; bool bFound = false;
if (strFilename.isEmpty() || nullptr == strDestPath || cchDest < 10) if (nullptr == strFilename || nullptr == strDestPath || cchDest < 10)
return false; return false;
// Get the exe name, and exe path // Get the exe name, and exe path

View File

@ -508,6 +508,20 @@ public:
virtual ~Image(); virtual ~Image();
// 从本地文件中读取图片
void open(
const String & strFilePath
);
// 从本地文件中读取图片并裁剪
void open(
const String & strFilePath,/* 图片文件路径 */
double nClipX, /* 裁剪位置 X 坐标 */
double nClipY, /* 裁剪位置 Y 坐标 */
double nClipWidth, /* 裁剪宽度 */
double nClipHeight /* 裁剪高度 */
);
// 裁剪图片 // 裁剪图片
void clip( void clip(
double nClipX, /* 裁剪位置 X 坐标 */ double nClipX, /* 裁剪位置 X 坐标 */
@ -516,20 +530,6 @@ public:
double nClipHeight /* 裁剪高度 */ double nClipHeight /* 裁剪高度 */
); );
// 从本地文件中读取图片
void loadFrom(
const String & strFilePath
);
// 从本地文件中读取图片并裁剪
void loadFrom(
const String & strFilePath,/* 图片文件路径 */
double nClipX, /* 裁剪位置 X 坐标 */
double nClipY, /* 裁剪位置 Y 坐标 */
double nClipWidth, /* 裁剪宽度 */
double nClipHeight /* 裁剪高度 */
);
// 获取宽度 // 获取宽度
virtual double getWidth() const; virtual double getWidth() const;
@ -619,7 +619,7 @@ public:
); );
// 添加节点到场景 // 添加节点到场景
void add( void preload(
Node * child, Node * child,
int zOrder = 0 int zOrder = 0
); );

View File

@ -23,7 +23,7 @@ class ObjectManager
public: public:
// 将一个节点放入内存池 // 将一个节点放入内存池
static void add( static void preload(
e2d::Obj * nptr e2d::Obj * nptr
); );
@ -87,13 +87,13 @@ class TimerManager
public: public:
// 绑定定时器到场景 // 绑定定时器到场景
static void add( static void preload(
Timer * pTimer, Timer * pTimer,
Scene * pParentScene Scene * pParentScene
); );
// 绑定定时器到节点 // 绑定定时器到节点
static void add( static void preload(
Timer * pTimer, Timer * pTimer,
Node * pParentNode Node * pParentNode
); );
@ -211,8 +211,29 @@ class MusicManager
friend Game; friend Game;
public: public:
// 添加音乐文件 // 预加载音乐资源
static bool add( static bool preload(
const String & strFilePath /* 音乐文件路径 */
);
// 播放音乐
static bool play(
const String & strFilePath, /* 音乐文件路径 */
int nLoopCount = 0 /* 重复播放次数,设置 -1 为循环播放 */
);
// 暂停音乐
static void pause(
const String & strFilePath /* 音乐文件路径 */
);
// 继续播放音乐
static void resume(
const String & strFilePath /* 音乐文件路径 */
);
// 停止音乐
static void stop(
const String & strFilePath /* 音乐文件路径 */ const String & strFilePath /* 音乐文件路径 */
); );

View File

@ -452,12 +452,12 @@ public:
virtual ~Sprite(); virtual ~Sprite();
// 加载精灵图片 // 加载精灵图片
virtual void loadFrom( virtual void open(
Image * texture Image * texture
); );
// 从本地文件加载图片 // 从本地文件加载图片
virtual void loadFrom( virtual void open(
const String & imageFileName const String & imageFileName
); );

View File

@ -209,24 +209,41 @@ public:
// 音乐播放器 // 音乐播放器
class Music class Music :
public Obj
{ {
friend MusicManager; friend MusicManager;
public: public:
Music();
Music(
const String & strFileName /* 音乐文件路径 */
);
virtual ~Music();
// 打开音乐文件
bool open(
const String & strFileName /* 音乐文件路径 */
);
// 播放 // 播放
bool play( bool play(
int nLoopCount = 0 /* 重复播放次数,设置 -1 为循环播放 */ int nLoopCount = 0 /* 重复播放次数,设置 -1 为循环播放 */
); );
// 暂停 // 暂停
bool pause(); void pause();
// 继续 // 继续
bool resume(); void resume();
// 停止 // 停止
bool stop(); void stop();
// 关闭音乐文件
void close();
// 获取音乐播放状态 // 获取音乐播放状态
bool isPlaying(); bool isPlaying();
@ -234,14 +251,14 @@ public:
// 获取音量 // 获取音量
double getVolume() const; double getVolume() const;
// 获取频率比
double getFrequencyRatio() const;
// 设置音量 // 设置音量
bool setVolume( bool setVolume(
double fVolume /* 音量范围为 -224 ~ 224其中 0 是静音1 是正常音量 */ double fVolume /* 音量范围为 -224 ~ 224其中 0 是静音1 是正常音量 */
); );
// »ñȡƵÂʱÈ
double getFrequencyRatio() const;
// 设置频率比 // 设置频率比
bool setFrequencyRatio( bool setFrequencyRatio(
double fFrequencyRatio /* 频率比范围为 1/1024.0f ~ 1024.0f,其中 1.0 为正常声调 */ double fFrequencyRatio /* 频率比范围为 1/1024.0f ~ 1024.0f,其中 1.0 为正常声调 */
@ -251,21 +268,20 @@ public:
IXAudio2SourceVoice* getIXAudio2SourceVoice() const; IXAudio2SourceVoice* getIXAudio2SourceVoice() const;
protected: protected:
Music();
virtual ~Music();
bool _open(const String & strFileName);
void _close();
bool _readMMIO(); bool _readMMIO();
bool _resetFile(); bool _resetFile();
bool _read(BYTE* pBuffer, DWORD dwSizeToRead); bool _read(
BYTE* pBuffer,
DWORD dwSizeToRead
);
bool _findMediaFileCch(wchar_t* strDestPath, int cchDest, const String & strFilename); bool _findMediaFileCch(
wchar_t* strDestPath,
int cchDest,
const wchar_t * strFilename
);
protected: protected:
bool m_bOpened; bool m_bOpened;