网页功能: 加入收藏 设为首页 网站搜索  
游戏背景音乐
发表日期:2006-08-23作者:[转贴] 出处:  

  不能出声的哑巴游戏总是不太爽。今天下午花了一点时间大致把游戏背景音乐封装了一下。这个封装基本是参照了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[MAX_PATH];
    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[MAX_PATH];
    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 );
}

就这么简单。四个函数就搞定游戏背景音乐了。

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 游戏背景音乐
本类热点文章
  DirectSound
  DirectSound 无题
  WAVE文件格式剖析
  游戏背景音乐
  DirectX Audio的强大功能
  使用DIRECTX 优化声音特性
  利用MCI播放MIDI、WAVE
  标准MIDI文件格式
  游戏音乐与音效的播放
  SEAL声音函数手册
  VC++5.0下MIDI、WAV及CD的播放
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00393