四个函数搞定游戏背景音乐
不能出声的哑巴游戏总是不太爽。花了一点时间大致把游戏背景音乐封装了一下。这个封装基本是参照了DMUtil.h的几个类。原来的打算是自己做一个MySound的类,封装到自己的MySound.h和MySound.cpp里。但是我的Demo用到了SDK的FrameWork,不知道为什么,我封装好的简易音乐类无法和原来的Demo正常编译连接。于是只好老老实实按照FrameWork的封装好的DMUtil.h在进一步封装。 #include "DMUtil.h"先在类里面申明
CMusicManager* g_pMusicManager;
CMusicSegment* g_pMusicSegment;
构造初始
g_pMusicManager = NULL;
g_pMusicSegment = NULL;
封装的四个函数
void InitMusic(HWND hWnd); // 初始化
HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName); // 载入音乐文件
void PlayMusic(HWND hDlg, BOOL bLooped); // 播放,可控制是否循环播放
void StopMusic(); // 停止播放
HRESULT CMyD3DApplication::LoadSegmentFile( HWND hDlg, TCHAR* strFileName )
{
HRESULT hr;
SAFE_DELETE( g_pMusicSegment );
// Have the loader collect any garbage now that the old
// segment has been released
g_pMusicManager->CollectGarbage();
// Set the media path based on the file name (something like C:\MEDIA)
// to be used as the search directory for finding DirectMusic content
// related to this file.
TCHAR strMediaPath;
GetCurrentDirectory(MAX_PATH, strMediaPath);
g_pMusicManager->SetSearchDirectory(strMediaPath);
BOOL bMidiFile = FALSE;
// Load the file into a DirectMusic segment
if( FAILED( g_pMusicManager->CreateSegmentFromFile( &g_pMusicSegment, strFileName,TRUE, bMidiFile ) ) )
{
MessageBox( NULL, "No find Music sound.", "", MB_OK | MB_ICONERROR );
return S_FALSE;
}
return S_OK;
}
void CMyD3DApplication::InitMusic(HWND hWnd)
{
g_pMusicManager = new CMusicManager();
g_pMusicManager->Initialize(m_hWnd );
TCHAR strFileName;
strcpy( strFileName, "sample.sgt" );
LoadSegmentFile(m_hWnd, strFileName ) ;
}
void CMyD3DApplication::PlayMusic( HWND hDlg, BOOL bLooped )
{
if( bLooped )
{
// Set the segment to repeat many times
g_pMusicSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE );
}
else
{
// Set the segment to not repeat
g_pMusicSegment->SetRepeats( 0 );
}
g_pMusicSegment->Play( DMUS_SEGF_BEAT );
}
void CMyD3DApplication::StopMusic()
{
g_pMusicSegment->Stop( DMUS_SEGF_BEAT );
}
就这么简单。四个函数就搞定游戏背景音乐了。 游戏没有音乐一点都不好玩,我们做的游戏里面的音乐很多,
有时间玩玩
www.novoking.com
页:
[1]