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

📄 ch13p1_3dsound.cpp

📁 游戏音频程序设计-Beginning.Game.Audio.Programming
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
#############################################################################

  Ch13p1_3DSound.cpp: a program that demonstrates the fire algorithm,
  without any annoying bells and/or whistles.
  
#############################################################################
*/

// include files ////////////////////////////////////////////////////////////
#define STRICT
#define DIRECTINPUT_VERSION 0x0800
#pragma warning(disable: 4786)
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "AudioEngine/DXUtil.h"
#include "D3DHelperFuncs.h"
#include "Ch13p1_resource.h"
#include "Camera.h"
#include "InputManager.h"
#include "AnimSequence.h"
#include "Sprite.h"
#include "SkyBox.h"

#include "AudioEngine/AudioEngine.h"

using namespace std;
using namespace AudioEngine;

CAudioManager g_AudioMgr;
CAudioManager *AudioEngine::GetAudioManager() { return(&g_AudioMgr); }

const int BEEPINTERVAL = 1; // beep every # seconds

//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the 
//       generic functionality needed in all Direct3D samples. CMyD3DApplication 
//       adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
  // Font for drawing text
  CD3DFont* m_pFont;
  CD3DFont* m_pFontSmall;

  // Scene
  CUserControlledCamera m_Camera;

  // Mouse Input
  CInputManager m_InputManager;

  // object mesh
  CD3DMesh* m_pObject;
  
  // skybox
  CSkyBox m_SkyBox;

protected:
  HRESULT OneTimeSceneInit();
  HRESULT InitDeviceObjects();
  HRESULT RestoreDeviceObjects();
  HRESULT InvalidateDeviceObjects();
  HRESULT DeleteDeviceObjects();
  HRESULT FinalCleanup();
  HRESULT Render();
  HRESULT FrameMove();
  HRESULT ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format );
  LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

  void ProcessInput();

  float m_rAngleX;
  float m_rAngleY;
  float m_rAngleZ;

  CSoundPtr m_Beep;
  CSoundPtr m_Engine;
  float m_BeepTimer;

  D3DVECTOR m_LastListenerPosition;
  D3DVECTOR m_ListenerVelocity;
  bool m_WarpSpeed;
  bool m_BeepActive;
public:
  CMyD3DApplication();
};

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
//       message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
  CMyD3DApplication d3dApp;

  if( FAILED( d3dApp.Create( hInst ) ) )
    return 0;

  return d3dApp.Run();
}

//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
  m_strWindowTitle    = _T("Ch13p1_3DSound");
  m_bUseDepthBuffer   = TRUE;
  m_pObject           = new CD3DMesh;
  m_pFont            = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  m_pFontSmall       = new CD3DFont( _T("Arial"),  9, D3DFONT_BOLD );
  m_rAngleX = m_rAngleY = m_rAngleZ = 0;
  m_BeepTimer = 0;
  m_WarpSpeed = false;
  m_BeepActive = true;
}

//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
//       permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
  try {
    GetAudioManager()->Init(m_hWnd);
    // full volume on performance
    GetAudioManager()->SetVolume(CVolume(0));
    m_Beep = GetAudioManager()->Load3DSound("beep.wav");
    m_Engine = GetAudioManager()->Load3DSound("engine.wav");
    CDirectMusicSegment *engineseg = dynamic_cast<CDirectMusicSegment *>(m_Engine.Get());
    engineseg->SetRepeats(DMUS_SEG_REPEAT_INFINITE);

    // set range on our engine and our beep
    C3DSoundEffect *beep = dynamic_cast<C3DSoundEffect *>(m_Beep.Get());
    C3DSoundEffect *engine = dynamic_cast<C3DSoundEffect *>(m_Engine.Get());

    if (beep) {
      beep->SetMaxDistance(100);
      beep->SetMinDistance(10);
    }

    if (engine) {
      engine->SetMaxDistance(80);
      engine->SetMinDistance(10);

    }
    
    
    GetAudioManager()->Commit3DSoundParameters();

  }
  catch(...) {
    return E_FAIL;
  }
    
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
//       the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
	if (m_fElapsedTime < 0.0001f) {
		m_fElapsedTime = 0.0001f;
	}
  CTimer::UpdateAll(m_fElapsedTime);
  
  // these are random values to make it seem like our spaceship is
  // drifting in space.
  m_rAngleX += 0.02f * m_fElapsedTime; 
  m_rAngleY += 0.03f * m_fElapsedTime;
  m_rAngleZ += 0.01f * m_fElapsedTime;

  m_BeepTimer += m_fElapsedTime;
  if (m_BeepActive && m_BeepTimer > BEEPINTERVAL) {
    // generate a beep!
    m_Beep->Play();
    m_BeepTimer = 0;
  }

  // set beep position to spaceship position
  C3DSoundEffect *beep = dynamic_cast<C3DSoundEffect *>(m_Beep.Get());
  if (beep) {
    D3DVECTOR v;
    v.x = 0;
    v.y = 4;
    v.z = 0;
    beep->SetPosition(v); 
  }

  // set listener position to camera position
  C3DSoundListener &listener = GetAudioManager()->GetListener();
  listener.SetPosition(m_Camera.GetPosition());
  listener.SetDopplerFactor(DS3D_MAXDOPPLERFACTOR);
  listener.SetDistanceFactor(10);
  
  // set listener orientation
  D3DXVECTOR3 lookat = D3DXVECTOR3(m_Camera.GetViewMatrix()._13, m_Camera.GetViewMatrix()._23, m_Camera.GetViewMatrix()._33);
  D3DXVECTOR3 up = D3DXVECTOR3(m_Camera.GetViewMatrix()._12, m_Camera.GetViewMatrix()._22, m_Camera.GetViewMatrix()._32);
  D3DXVECTOR3 nlookat, nup;

  D3DXVec3Normalize(&nlookat, &lookat);
  D3DXVec3Normalize(&nup, &up);

  if (nup == nlookat) { nup = D3DXVECTOR3(0,1,0); }
  listener.SetOrient(nup, nlookat);

  
  // set listener velocity based on previous frame position and current position
  m_ListenerVelocity.x = m_Camera.GetPosition().x - m_LastListenerPosition.x;
  m_ListenerVelocity.y = m_Camera.GetPosition().y - m_LastListenerPosition.y;
  m_ListenerVelocity.z = m_Camera.GetPosition().z - m_LastListenerPosition.z;
  listener.SetVelocity(m_ListenerVelocity);

  m_LastListenerPosition = m_Camera.GetPosition();
  GetAudioManager()->Commit3DSoundParameters();


  return S_OK;
}

void CMyD3DApplication::ProcessInput()
{
  float fSpeed = 0.5f;
  unsigned char m_bKey[256];
  ZeroMemory( m_bKey, 256 );
  GetKeyboardState(m_bKey);
  // Process keyboard input

  if (m_WarpSpeed) fSpeed = 2;
  if(m_bKey['D'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(fSpeed, 0.0f, 0.0f)); // Slide Right
  if(m_bKey['A'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(-fSpeed, 0.0f, 0.0f));// Slide Left
  if(m_bKey['Q'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, fSpeed, 0.0f)); // Slide Up
  if(m_bKey['Z'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, -fSpeed, 0.0f));// Slide Down
  if(m_bKey['W'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, 0.0f, fSpeed)); // Slide Foward
  if(m_bKey['S'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, 0.0f, -fSpeed));// Slide Back
  if(m_bKey['L'] & 128) m_Camera.AddToYawPitchRoll(fSpeed, 0.0f, 0.0f);  // Turn Right
  if(m_bKey['J'] & 128) m_Camera.AddToYawPitchRoll(-fSpeed, 0.0f, 0.0f); // Turn Left
  if(m_bKey['K'] & 128) m_Camera.AddToYawPitchRoll(0.0f, fSpeed, 0.0f); // Turn Down
  if(m_bKey['I'] & 128) m_Camera.AddToYawPitchRoll(0.0f, -fSpeed, 0.0f);// Turn Up

  // mouse look
  DIMOUSESTATE2 dims2;
  m_InputManager.ReadMouse(dims2);

⌨️ 快捷键说明

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