I need help loading a wave file for playing........
Well I have Isometric Game Programming with Directx 7.........this book has been "ok" so far, not great, just "ok", but I must tell you that it did a terrible job on showing people how to load a freaking wave file for playing......I tried, I even basically copied the entire source code from the tutorial.......it compiled good but then it just crashed the small program.
Code:
HANDLE hfile=CreateFile("boom.wave",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
//set up buffer description
DSBUFFERDESC dsbd;
memset(&dsbd,0,sizeof(DSBUFFERDESC));
//size
dsbd.dwSize=sizeof(DSBUFFERDESC);
//flags
dsbd.dwFlags=DSBCAPS_LOCSOFTWARE;
char Buffer[5];
Buffer[4]=0;
//read length
DWORD dwNumRead;
//length variable
DWORD dwLength;
DWORD dwDataLength;
//data buffer
UCHAR* ucTemp;
UCHAR* ucData;
LPWAVEFORMATEX lpWfx;
/*
BOOL ReadFile(
HANDLE hFile, // handle of file to read
LPVOID lpBuffer, // address of buffer that receives data
DWORD nNumberOfBytesToRead, // number of bytes to read
LPDWORD lpNumberOfBytesRead, // address of number of bytes read
LPOVERLAPPED lpOverlapped // address of structure for data
);
*/
ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//"RIFF"
ReadFile(hfile,&dwLength,sizeof(dwLength),&dwNumRead,NULL);//length of file
ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//"WAVE"
bool done=false;
while(!done)
{
ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//chunk header
ReadFile(hfile,&dwLength,sizeof(dwLength),&dwNumRead,NULL);//length of chunk
ucTemp=new UCHAR[dwLength];
ReadFile(hfile,ucTemp,dwLength,&dwNumRead,NULL);
//dependin on the chunk header, do something
if(strcmp("fmt ",Buffer)==0)// if the chunk contains the world "fmt "
{
//clear out format
memset(lpWfx,0,sizeof(WAVEFORMATEX));
//memcpy (&destination, &source, lenghtofbytes)
//copy from buffer
memcpy(lpWfx,ucTemp,dwLength);
}
if(strcmp("data",Buffer)==0)
{
//data chunk
//allocate data buffer
//copy length
dwDataLength=dwLength;
//copy buffer
memcpy(ucData,ucTemp,dwDataLength);
//we are done, and need no more chunks
done=true;
}
delete ucTemp;
}
//close the file
CloseHandle(hfile);
dsbd.dwBufferBytes=dwLength;
bullets->CreateSoundBuffer(&dsbd,&lpdsb, NULL);
DWORD buflen,buflen2;
void* bufptr;
lpdsb->Lock(0,0,&bufptr,&buflen,NULL,&buflen2,DSBLOCK_ENTIREBUFFER);
//copy from wave loader to sound buffer
memcpy(bufptr,ucData,dwLength);
lpdsb->Unlock(bufptr,buflen,NULL,buflen2);
Don't know what's wrong with it, I can't really check my mistakes on it, because as I said before I am not really up to date on knowing how to do that.......does anybody know what's wrong with this? Or knows a GOOD tutorial for this? I looked at a tutorial on gamedev.net and the guy says that doesn't even know if his tutorial compiles, so for someone like me who's having problems with it, maybe this isn't the best way to go. Or does anybody know how to do this an easier way? Please I need help people.