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

📄 ch14p1_3dopenalsound.cpp

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

  Ch14p1_3DOpenALSound.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 "DXUtil.h"
#include "D3DHelperFuncs.h"
#include "Ch14p1_resource.h"
#include "Camera.h"
#include "InputManager.h"
#include "AnimSequence.h"
#include "Sprite.h"
#include "SkyBox.h"

#include "audioEngine/OpenALManager.h"

using namespace std;
using namespace AudioEngine;

COpenALManager g_OpenALMgr;
COpenALManager *AudioEngine::GetOpenALManager() { return(&g_OpenALMgr); }

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;

  COpenALBufferPtr m_BeepBuffer;
  COpenALSourcePtr m_BeepSource;

  COpenALBufferPtr m_EngineBuffer;
  COpenALSourcePtr m_EngineSource;

  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("Ch14p1_3DOpenALSound");
  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 {
    GetOpenALManager()->Init();

    m_BeepBuffer = GetOpenALManager()->CreateBufferAndLoadWave("beep.wav");
    m_EngineBuffer = GetOpenALManager()->CreateBufferAndLoadWave("engine.wav");
    
    m_BeepSource = GetOpenALManager()->CreateSource(m_BeepBuffer);
    m_EngineSource = GetOpenALManager()->CreateSource(m_EngineBuffer);

    m_EngineSource->SetLooping(1);

    m_BeepSource->SetMaxDistance(100);
    m_EngineSource->SetMaxDistance(80);
  }
  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_BeepSource->PlayFromStart();
    m_BeepTimer = 0;
  }

  // set beep position to spaceship position
  m_BeepSource->SetPosition(0,4,0);

  // set listener position to camera position
  COpenALListener &listener = GetOpenALManager()->GetListener();
  listener.SetPosition(m_Camera.GetPosition().x,m_Camera.GetPosition().y,m_Camera.GetPosition().z);
  
  /////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.SetOrientation(nlookat.x, nlookat.y, nlookat.z, nup.x, nup.y, nup.z);
  
  // 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.x,m_ListenerVelocity.y,m_ListenerVelocity.z);

  m_LastListenerPosition = m_Camera.GetPosition();
  
  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);

  // play with the divisor constants to change the mouselook sensitivity.
  // I've found that these values most accurately simulate my beloved Q3A setup. :)
  m_Camera.AddToYawPitchRoll((float)dims2.lX/0.8f, (float)dims2.lY/0.8f, 0.0f);
  
}

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
//       rendering. This function sets up render states, clears the
//       viewport, and renders the scene.

⌨️ 快捷键说明

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