⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3dsound.cpp

📁 是《3D游戏编程》的代码 买了这本书
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// 名称: RenderSene.cpp
// 
// 功能: 进行3D场景的渲染——使用显示内存的顶点缓冲区。
//       
//-----------------------------------------------------------------------------

// 包含Direct3D头文件
#include <d3d8.h>
#include <d3dx8.h>
#include <dmusicc.h>
#include <dmusici.h>

// Windows类的名称宏定义
#define  MY_WINCLASS_NAME      "Direct3D"

// 定义个表示顶点的结构
struct CUSTOMVERTEX
{
    FLOAT x, y, z; // x,y,z表示点的三维坐标值,
    DWORD color;        // 顶点颜色
};

struct RECOBJECT
{
    FLOAT x, y, z; // x,y,z表示点的三维坐标值,
};

// 自定义的顶点格式
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

// 定义发出声音物体的移动速率
#define MY_3D_VELOCITY    (0.01f)

// 定义全局变量
LPDIRECT3D8             g_pMyD3D          = NULL; // 定义Direct3D对象的指针
LPDIRECT3DDEVICE8       g_pMyd3dDevice    = NULL; // 定义Direct3D设备指针
LPDIRECT3DVERTEXBUFFER8 g_pMyVxBuffer     = NULL; // 定义顶点缓冲区对象指针

// 定义包含三个顶点的顶点数组
CUSTOMVERTEX g_Vertices[] =
{
        { -0.5f,-0.5f, 0.0f, 0xffff0000, },  
        { -0.5f, 0.5f, 0.0f, 0xffff0000, },
		{  0.5f,-0.5f, 0.0f, 0xffff0000, },
		{  0.5f, 0.5f, 0.0f, 0xffff0000, },  
  
};

RECOBJECT g_RecObject;
int       g_Time;

// 定义全局声音变量
IDirectMusicLoader8*      g_pLoader         = NULL;
IDirectMusicPerformance8* g_pPerformance    = NULL;
IDirectMusicSegment8*     g_pSegment1_1        = NULL;
IDirectMusicSegment8*     g_pSegment1_2        = NULL;
IDirectMusicSegment8*     g_pSegment2        = NULL;
IDirectMusicAudioPath8*   g_p3DAudioPath1    = NULL;
IDirectMusicAudioPath8*   g_p3DAudioPath2    = NULL;
IDirectSound3DBuffer*     g_pSound3DBuffer  = NULL;
IDirectSound3DListener8*  g_pSoundListener  = NULL;
DS3DLISTENER              g_dsListenerParams;                

//-----------------------------------------------------------------------------
// 名称: SoundInit()
// 功能: 初始化声音播放环境的函数,函数中创建了DirectMusic的载入器对象和
//       DirectMusic的演奏对象
//       函数返回E_FAIL表示失败,返回E_OK表示成功。
//-----------------------------------------------------------------------------
HRESULT SoundInit()
{
	HRESULT hr;

    // 初始化COM
    CoInitialize(NULL);
    
    // 创建一个载入器对象
    CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
                      IID_IDirectMusicLoader8, (void**)&g_pLoader );

    // 创建一个演奏对象
    CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, 
                      IID_IDirectMusicPerformance8, (void**)&g_pPerformance );

    // 初始化演奏对象
    g_pPerformance->InitAudio( NULL, NULL, NULL, 
                               DMUS_APATH_DYNAMIC_STEREO, 64,
                               DMUS_AUDIOF_ALL, NULL );

	
	g_pPerformance->CreateStandardAudioPath( DMUS_APATH_DYNAMIC_3D, 
                                             64, TRUE, &g_p3DAudioPath1 );
    // 得到 IDirectSound3DBuffer8 接口
    g_p3DAudioPath1->GetObjectInPath( DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, 
                                 GUID_NULL, 0, IID_IDirectSound3DBuffer, 
                                 (LPVOID*) &g_pSound3DBuffer );

	// 得到 IID_IDirectSound3DListener8 接口
	hr = g_p3DAudioPath1->GetObjectInPath( 0, DMUS_PATH_PRIMARY_BUFFER, 0,
    							 GUID_NULL, 0, IID_IDirectSound3DListener8, 
    							 (LPVOID*) &g_pSoundListener);

	g_pPerformance->CreateStandardAudioPath( DMUS_APATH_DYNAMIC_3D, 
                                             64, TRUE, &g_p3DAudioPath2 );

	return S_OK;
}


//-----------------------------------------------------------------------------
// 名称: SoundLoad()
// 功能: 载入三个声音文件。
//-----------------------------------------------------------------------------
VOID SoundLoad()
{
	TCHAR strPath[MAX_PATH];
    GetCurrentDirectory( MAX_PATH, strPath );

    // 下面的代码设置查找目录
	WCHAR wstrSearchPath[MAX_PATH];
    MultiByteToWideChar( CP_ACP, 0, strPath, -1, 
                         wstrSearchPath, MAX_PATH );

    g_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes,
		                           wstrSearchPath,
								   FALSE);
    
    // 从文件载入声音到一个声音段对象中
	
    WCHAR wstrFileName1_1[MAX_PATH] = L"do.wav";   

    if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
                                               IID_IDirectMusicSegment8,
                                               wstrFileName1_1,
                                               (LPVOID*) &g_pSegment1_1 ) ) )
    {
        MessageBox( NULL, "Media not found, sample will now quit", 
                          "DirectMusic Tutorial", MB_OK );
        return;
    }

    WCHAR wstrFileName1_2[MAX_PATH] = L"back.wav";   

    if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
                                               IID_IDirectMusicSegment8,
                                               wstrFileName1_2,
                                               (LPVOID*) &g_pSegment1_2 ) ) )
    {
        MessageBox( NULL, "Media not found, sample will now quit", 
                          "DirectMusic Tutorial", MB_OK );
        return;
    }

	WCHAR wstrFileName2[MAX_PATH] = L"gun.wav";   

    if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
                                               IID_IDirectMusicSegment8,
                                               wstrFileName2,
                                               (LPVOID*) &g_pSegment2 ) ) )
    {
        MessageBox( NULL, "Media not found, sample will now quit", 
                          "DirectMusic Tutorial", MB_OK );
        return;
    }
}


//-----------------------------------------------------------------------------
// 名称: SoundPlay()
// 功能: 播放声音文件
//-----------------------------------------------------------------------------
VOID SoundPlay()
{
       // 载入声音段到演奏中
    g_pSegment2->Download( g_p3DAudioPath1 );

	// 设置这个声音段为循环播放
	g_pSegment2->SetRepeats(DMUS_SEG_REPEAT_INFINITE);
 
    // 播放声音段
    g_pPerformance->PlaySegmentEx( g_pSegment2, NULL, NULL, DMUS_SEGF_DEFAULT, 
                                   0, NULL, NULL, g_p3DAudioPath1 );
	
	g_dsListenerParams.dwSize = sizeof(DS3DLISTENER);
    g_pSoundListener->GetAllParameters( &g_dsListenerParams );

    g_dsListenerParams.flDopplerFactor = 1.0f;
    g_dsListenerParams.flRolloffFactor = 0.2f;

	// 设置声音接收者的初始位置 
	g_dsListenerParams.vPosition.x = 0.0f;
    g_dsListenerParams.vPosition.y = 0.0f;
	g_dsListenerParams.vPosition.z = 1.0f;

    if( g_pSoundListener )
        if( FAILED(g_pSoundListener->SetAllParameters( &g_dsListenerParams, DS3D_IMMEDIATE )))
		{
			MessageBox( NULL, "g_pSoundListener2->SetAllParameters failed",
						 "3DListener", MB_OK );
			return;
		}

	// 载入声音段到演奏中
    g_pSegment1_1->Download( g_p3DAudioPath2 );

	// 设置这个声音段为循环播放
	g_pSegment1_1->SetRepeats(DMUS_SEG_REPEAT_INFINITE);
 
    // 播放声音段
    g_pPerformance->PlaySegmentEx( g_pSegment1_1, NULL, NULL, DMUS_SEGF_DEFAULT|DMUS_SEGF_SECONDARY, 
                                   0, NULL, NULL, g_p3DAudioPath2 );
	// 载入声音段到演奏中
    g_pSegment1_2->Download( g_p3DAudioPath2 );

	// 设置这个声音段为循环播放
	g_pSegment1_2->SetRepeats(DMUS_SEG_REPEAT_INFINITE);
 
    // 播放声音段
    g_pPerformance->PlaySegmentEx( g_pSegment1_2, NULL, NULL, DMUS_SEGF_DEFAULT | DMUS_SEGF_SECONDARY, 
                                   0, NULL, NULL, g_p3DAudioPath2 );
}


//-----------------------------------------------------------------------------
// 名称: SoundMove()
// 功能: 移动声音源
//-----------------------------------------------------------------------------
VOID SoundMove()
{
    D3DVECTOR vPosition;
    vPosition.x = g_RecObject.x;
    vPosition.y = 0.0f;
    vPosition.z = 0.0f;

    D3DVECTOR vVelocity;
    vVelocity.x = MY_3D_VELOCITY;
    vVelocity.y = 0.0f;
    vVelocity.z = 0.0f;

    // 设置声音缓冲的位置和速度
    if( g_pSound3DBuffer )
    {
        g_pSound3DBuffer->SetPosition( vPosition.x,
                                       vPosition.y,
                                       vPosition.z, 
									   DS3D_IMMEDIATE );

        g_pSound3DBuffer->SetVelocity( vVelocity.x,
                                       vVelocity.y,
                                       vVelocity.z, 
									   DS3D_IMMEDIATE );
    }

}

//-----------------------------------------------------------------------------
// 名称: SoundClean()
// 功能: 释放所有的DirectMusic资源
//-----------------------------------------------------------------------------
VOID SoundClean()
{
    // 清除audiopath 和 声音缓冲区
    g_pSound3DBuffer->Release();
    g_p3DAudioPath1->Release();
    g_p3DAudioPath2->Release();

    // 清除载入器、声音段对象
    g_pLoader->Release(); 
    g_pSegment1_1->Release();
    g_pSegment1_2->Release();
    g_pSegment2->Release();

    // 停止声音播放,并关闭演奏
    g_pPerformance->Stop( NULL, NULL, 0, 0 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -