[ Pobierz całość w formacie PDF ]
.cksize;/*** Allocate enough data to hold the wav file data*/pbWavData = new uchar[ nWaveFileSize ];if( NULL == pbWavData ){delete [] pbWavData;throw cGameError("Out of memory!");}hr = m_pWaveSoundRead->Read(nWaveFileSize,pbWavData,&cbWavSize );if( FAILED( hr ) ){delete [] pbWavData;throw cGameError("m_pWaveSoundRead->Read failed");}/*** Reset the file to the beginning*/m_pWaveSoundRead->Reset();/*** Lock the buffer so we can copy the data over*/hr = m_pBuffer->Lock(158 0, m_bufferSize, &pbData, &dwLength,&pbData2, &dwLength2, 0L );if( FAILED( hr ) ){delete [] pbWavData;throw cGameError("m_pBuffer->Lock failed");}/*** Copy said data over, unlocking afterwards*/memcpy( pbData, pbWavData, m_bufferSize );m_pBuffer->Unlock( pbData, m_bufferSize, NULL, 0 );/*** We're done with the wav data memory.*/delete [] pbWavData;}bool cSound::IsPlaying(){DWORD dwStatus = 0;m_pBuffer->GetStatus( &dwStatus );if( dwStatus & DSBSTATUS_PLAYING )return true;elsereturn false;}void cSound::Play( bool bLoop ){159 HRESULT hr;if( NULL == m_pBuffer )return;// Restore the buffers if they are lostRestore();// Play bufferDWORD dwLooped = bLoop ? DSBPLAY_LOOPING : 0L;if( FAILED( hr = m_pBuffer->Play( 0, 0, dwLooped))){throw cGameError("m_pBuffer->Play failed");}}Additions to cApplicationThe only addition to cApplication is the InitSound call, which initializes the sound layer.After the callcompletes you can freely create cSound objects and play them.If this is not the behavior you would likein your application, the function is overloadable.The code is in the following:void cApplication::InitSound(){cSoundLayer::Create( MainWindow()->GetHWnd() );}Application: DirectSound SampleAdrian, the lead author of the DirectX 7.0 version of this book, has a few interesting hobbies.As part ofan ongoing effort, he does extracurricular activities that actually have nothing to do with programming.One of them is an a cappella group that he sings bass in.One of his jobs in the a cappella group is totake care of some of the vocal percussion.A cappella music can't have any sort of accompaniment, so any percussion needs to be done with thehuman voice.This has spawned an entire subculture of vocal percussionists, each trying to make that160perfect snare sound or cymbal crash using only their mouths.The DirectSound sample for this chapterwas created using Adrian's unique vocal abilities.When you load the file DSSAMPLE from the companion files, you're presented with a small window thatlists six different vocal percussion sounds.The keys 1 through 6 play each of the sounds, and you canpress multiple keys simultaneously to play multiple sounds.You'll note that I didn't show you a DirectInput sample, because I figured it would be better to rollDirectSound and DirectInput into one sample.DirectInput is used to capture the keystrokes.With somepractice you can get a pretty swank beat going.The code behind the sample appears in Listing 4.7.Listing 4.7: The vocal percussion DirectSound sample app/******************************************************************** Advanced 3D Game Programming using DirectX 9.0* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** Desc: Sample application for Direct3D** copyright (c) 2002 by Peter A Walsh and Adrian Perez* See license.txt for modification and distribution information******************************************************************/#include "stdafx.h"#include#includeusing namespace std;class cDSSampleApp : public cApplication, public iKeyboardReceiver{vector m_sounds[6];string m_names[6];int m_states[6]; // states of the keys 1-6161 public:void PlaySound( int num );//==========-------------------------- cApplicationvirtual void DoFrame( float timeDelta );virtual void SceneInit();cDSSampleApp() :cApplication(){m_title = string( "DirectSound Sample" );m_width = 320;m_height = 200;for( int i=0; iPlay();}void cDSSampleApp::DoFrame( float timeDelta ){// Clear the previous contents of the back bufferGraphics()->GetDevice()->Clear( 0, 0, D3DCLEAR_TARGET |D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB( 0,0,200), 1.0f, 0 );// Set up the stringsstring help;help += "DirectSound Sample application\n";help += "Vocal Percussion with Adrian Perez\n";164 help += " [1]: Keg drum\n";help += " [2]: Crash 1\n";help += " [3]: Crash 2\n";help += " [4]: Bass drum\n";help += " [5]: Snare drum\n";help += " [6]: Hi-Hat\n";// Tell Direct3D we are about to start renderingGraphics()->GetDevice()->BeginScene();// Output the textGraphics()->DrawTextString( 1, 1, D3DCOLOR_XRGB( 0, 255, 0),help [ Pobierz całość w formacie PDF ]