[ Pobierz całość w formacie PDF ]
.If you use the default constructor to create a CWave object,you must call Create() yourself in order to properly initialize the object.This is a commonobject initialization approach in MFC and is referred to as two-phase construction.Thecode for these Create() methods is shown in Listing 7.3.LISTING 7.3 The Create() Methods for the CWave Class1: BOOL CWave::Create(const char* pszFileName)2: {3: // Free any previous wave image data4: Free();5:6: // Flag as regular memory7: m_bResource = FALSE;8:9: // Open the wave file10: ifstream fileWave(pszFileName);11:12: // Get the file length13: fileWave.seekg(0, ios::end);14: m_dwImageLen = (DWORD)fileWave.tellg();15:16: // Allocate and lock memory for the image data17: m_pImageData = (BYTE*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE |18: GMEM_SHARE, m_dwImageLen));19: if (!m_pImageData)20: return FALSE;21:22: // Read the image data from the file23: fileWave.seekg(0, ios::beg);724: fileWave.read(m_pImageData, m_dwImageLen);25:26: return TRUE;27: }28:continues 11 1634xCH07 11/13/99 11:02 AM Page 120120 Hour 7LISTING 7.3 continued29: BOOL CWave::Create(UINT uiResID, HMODULE hmod)30: {31: // Free any previous wave image data32: Free();33:34: // Flag as resource memory35: m_bResource = TRUE;36:37: // Find the wave resource38: HRSRC hresInfo;39: hresInfo = FindResource(hmod, MAKEINTRESOURCE(uiResID),40:  WAVE );41: if (!hresInfo)42: return FALSE;43:44: // Load the wave resource45: HGLOBAL hgmemWave = LoadResource(hmod, hresInfo);46:47: if (hgmemWave)48: {49: // Get pointer to and length of the wave image data50: m_pImageData= (BYTE*)LockResource(hgmemWave);51: m_dwImageLen = SizeofResource(hmod, hresInfo);52: }53:54: return (m_pImageData ? TRUE : FALSE);55: }You might notice that both of the Create() methods call the Free() method to free any oldwave data before creating a new wave.Listing 7.4 contains the code for the Free() method.LISTING 7.4 The GetData() Method for the CWave Class1: BOOL CWave::Free()2: {3: // Free any previous wave data4: if (m_pImageData) {5: HGLOBAL hgmemWave = GlobalHandle(m_pImageData);6:7: if (hgmemWave) {8: if (m_bResource)9: // Free resource10: FreeResource(hgmemWave);11: else {12: // Unlock and free memory13: GlobalUnlock(hgmemWave);14: GlobalFree(hgmemWave);15: }16: 11 1634xCH07 11/13/99 11:02 AM Page 121Applying DirectSound 12117: m_pImageData = NULL;18: m_dwImageLen = 0;19: return TRUE;20: }21: }22: return FALSE;23: }The implementation of the Play() method shows how the PlaySound()Win32 API functionis used to provide a high-level means of playing waves using the CWave class (Listing 7.5).LISTING 7.5 The Play() Method for the CWave Class1: BOOL CWave::Play(BOOL bAsync, BOOL bLooped) const2: {3: // Check validity4: if (!IsValid())5: return FALSE;6:7: // Play the wave8: return PlaySound((LPCSTR)m_pImageData, NULL, SND_MEMORY |9: SND_NODEFAULT | (bAsync ? SND_ASYNC : SND_SYNC) |10: (bLooped ? (SND_LOOP | SND_ASYNC) : 0));11: }Although the Play() method has its place in making CWave a well-rounded class, thisbook is about DirectX, which means that you re interested in using the CWave class toplay waves using DirectSound.More important to DirectSound are the GetFormat(),GetDataLen(), and GetData() methods (Listings 7.6 7.8).LISTING 7.6 The GetFormat() Method for the CWave Class1: BOOL CWave::GetFormat(WAVEFORMATEX& wfFormat) const2: {3: // Check validity4: if (!IsValid())5: return FALSE;6:7: // Setup and open the MMINFO structure8: CMMMemoryIOInfo mmioInfo((HPSTR)m_pImageData, m_dwImageLen);9: CMMIO mmio(mmioInfo);10:711: // Find the WAVE chunk12: CMMTypeChunk mmckParent( W , A , V , E );13: mmio.Descend(mmckParent, MMIO_FINDRIFF);14:15: // Find and read the format subchunkcontinues 11 1634xCH07 11/13/99 11:02 AM Page 122122 Hour 7LISTING 7.6 continued16: CMMIdChunk mmckSubchunk( f , m , t ,  );17: mmio.Descend(mmckSubchunk, mmckParent, MMIO_FINDCHUNK);18: mmio.Read((HPSTR)&wfFormat, sizeof(WAVEFORMATEX));19: mmio.Ascend(mmckSubchunk);20:21: return TRUE;22: }LISTING 7.7 The GetDataLen() Method for the CWave Class1: DWORD CWave::GetDataLen() const2: {3: // Check validity4: if (!IsValid())5: return (DWORD)0;6:7: // Setup and open the MMINFO structure8: CMMMemoryIOInfo mmioInfo((HPSTR)m_pImageData, m_dwImageLen);9: CMMIO mmio(mmioInfo);10:11: // Find the WAVE chunk12: CMMTypeChunk mmckParent( W , A , V , E );13: mmio.Descend(mmckParent, MMIO_FINDRIFF);14:15: // Find and get the size of the data subchunk16: CMMIdChunk mmckSubchunk( d , a , t , a );17: mmio.Descend(mmckSubchunk, mmckParent, MMIO_FINDCHUNK);18: return mmckSubchunk.cksize;19: }LISTING 7.8 The GetData() Method for the CWave Class1: DWORD CWave::GetData(BYTE*& pWaveData, DWORD dwMaxLen) const2: {3: // Check validity4: if (!IsValid())5: return (DWORD)0;6:7: // Setup and open the MMINFO structure8: CMMMemoryIOInfo mmioInfo((HPSTR)m_pImageData, m_dwImageLen);9: CMMIO mmio(mmioInfo);10:11: // Find the WAVE chunk12: CMMTypeChunk mmckParent( W , A , V , E );13: mmio.Descend(mmckParent, MMIO_FINDRIFF);14:15: // Find and get the size of the data subchunk16: CMMIdChunk mmckSubchunk( d , a , t , a ); 11 1634xCH07 11/13/99 11:02 AM Page 123Applying DirectSound 12317: mmio.Descend(mmckSubchunk, mmckParent, MMIO_FINDCHUNK);18: DWORD dwLenToCopy = mmckSubchunk.cksize;19:20: // Allocate memory if the passed in pWaveData was NULL21: if (pWaveData == NULL)22: pWaveData = (BYTE*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE,23: dwLenToCopy));24: else25: // If we didn t allocate our own memory, honor dwMaxLen26: if (dwMaxLen QueryInterface (IID_IDirect3D7, (void **)&lpD3D);This interface will be used to create the remainder of the Direct3D interfaces.The IDirect3DDevice7 InterfaceAlthough the IDirect3D7 interface initializes Direct3D and associates it withDirectDraw, it does not provide any connection to video hardware.Connecting to the hardware is the job of our second interface: IDirect3DDevice7.Thisis created by using the CreateDevice() member of the IDirect3D7 interface you havecreated.The syntax for this function is shown in the following.The Syntax for IDirect3D7::CreateDeviceHRESULT CreateDevice(REFCLSID rclsid,LPDIRECTDRAWSURFACE7 lpDDS,LPDIRECT3DDEVICE7 *lplpD3DDevice,LPUNKNOWN pUnkOuter);CreateDevice() creates an IDirect3DDevice7 that is bound to the video hardware rep-resented by the provided surface [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • hanula1950.keep.pl