void CompositeBehavior::doAction(bool directlyExecuted) { if (agent->isDead() || agent->isIncapacitated() || (agent->getZone() == NULL)) { agent->setFollowObject(NULL); return; } if (!started()) this->start(); else if (!checkConditions()) endWithFailure(); if (finished()) { Behavior::doAction(directlyExecuted); return; } Behavior* currentChild; do { currentChild = children.get(currentPos); if (currentChild == NULL) { agent->error("NULL child or empty children list in CompositeBehavior"); endWithError(); Behavior::doAction(directlyExecuted); return; } if (!currentChild->started()) currentChild->start(); else if (!currentChild->checkConditions()) // if this isn't here, I can get a single recursion where the child will call the parent's (this) doAction() endWithFailure(); if (!currentChild->finished()) currentChild->doAction(); if (currentChild->finished()) { if (currentChild->succeeded()) this->childSucceeded(); else if (currentChild->failed()) this->childFailed(); currentChild->end(); } } while (currentChild != NULL && currentChild->finished() && !this->finished() && currentPos < children.size()); if (currentChild->finished()) Behavior::doAction(directlyExecuted); }
void ParallelBehavior::doAction(bool directlyExecuted) { if (finished()) { Behavior::doAction(directlyExecuted); return; } if (!started()) this->start(); for (int i = 0; i < children.size(); i++) { Behavior* currentChild = children.get(i); if (currentChild == NULL) { // this shouldn't happen. Bail. agent->error("NULL child or empty children list in ParallelBehavior"); endWithError(); Behavior::doAction(directlyExecuted); return; } if (!unfinishedChildren.contains(currentChild)) // we don't want to process a child after it has already ended continue; if (!currentChild->started()) currentChild->start(); else if (currentChild->finished()) { if (currentChild->succeeded()) this->childSucceeded(); else if (currentChild->failed()) this->childFailed(); currentChild->end(); unfinishedChildren.removeElement(currentChild); } else { currentChild->doAction(); } } if (unfinishedChildren.isEmpty()) this->finish(); Behavior::doAction(directlyExecuted); }
Sound::Sound(char* filePath){ path = filePath; //Loading of the WAVE file fp=fopen(filePath,"rb"); //Open the WAVE file if (!fp) endWithError(errors[0]); //Could not open file //Check that the WAVE file is OK fread(&riff_header, sizeof(RIFF_Header), 1, fp); if(riff_header.chunkID[0]!='R' || riff_header.chunkID[1]!='I' || riff_header.chunkID[2]!='F' || riff_header.chunkID[3]!='F') //Should be "RIFF" endWithError (errors[1]); //Not RIFF if (riff_header.format[0]!='W' || riff_header.format[1]!='A' || riff_header.format[2]!='V' || riff_header.format[3]!='E') //This part should be "WAVE" endWithError(errors[2]); //Not WAVE fread(&wave_format, sizeof(WAVE_Format), 1, fp); if (wave_format.subChunkID[0]!='f' || wave_format.subChunkID[1]!='m' || wave_format.subChunkID[2]!='t' || wave_format.subChunkID[3]!=' ') //This part should be "fmt " endWithError(errors[3]); //Not fmt fread(type,sizeof(char),4,fp); if (type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a') //This part should be "data" endWithError(errors[4]); //not data fread(&dataSize,sizeof(int),1,fp); //The size of the sound data is read buf= new unsigned char[dataSize]; //Allocate memory for the sound data cout << fread(buf,1,dataSize,fp) << " bytes loaded\n"; //Read the sound data and display the //number of bytes loaded. //Should be the same as the Data Size if OK device = alcOpenDevice(NULL); //Open the device if(!device) endWithError(errors[5]); //Error during device oening context = alcCreateContext(device, NULL); //Give the device a context alcMakeContextCurrent(context); //Make the context the current if(!context) endWithError(errors[6]); //Error during context handeling //Stores the sound data frequency=wave_format.sampleRate;; //The Sample Rate of the WAVE file alGenBuffers(1, &buffer); //Generate one OpenAL Buffer and link to "buffer" alGenSources(1, &source); //Generate one OpenAL Source and link to "source" if(alGetError() != AL_NO_ERROR) endWithError("Error GenSource"); //Error during buffer/source generation //Figure out the format of the WAVE file std::cout<<wave_format.numChannels; if(wave_format.bitsPerSample == 8) { if(wave_format.numChannels == 1) format = AL_FORMAT_MONO8; else if(wave_format.numChannels == 2) format = AL_FORMAT_STEREO8; } else if(wave_format.bitsPerSample == 16) { if(wave_format.numChannels == 1) format = AL_FORMAT_MONO16; else if(wave_format.numChannels == 2) format = AL_FORMAT_STEREO16; } if(!format) endWithError(errors[7]); //Not valid format alBufferData(buffer, format, buf, dataSize, frequency); //Store the sound data in the OpenAL Buffer if(alGetError() != AL_NO_ERROR) endWithError(errors[8]); //Error during buffer loading //Sound setting variables ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; //First direction vector, then vector pointing up) //Listener alListenerfv(AL_POSITION, ListenerPos); alListenerfv(AL_VELOCITY, ListenerVel); alListenerfv(AL_ORIENTATION, ListenerOri); //Source alSourcei (source, AL_BUFFER, buffer); alSourcef (source, AL_PITCH, 1.0f ); alSourcef (source, AL_GAIN, 1.0f ); alSourcefv(source, AL_POSITION, SourcePos); alSourcefv(source, AL_VELOCITY, SourceVel); alSourcei (source, AL_LOOPING, AL_FALSE ); }
void Sound::PlaySound(){ alSourcePlay(source); //Play the sound buffer linked to the source if(alGetError() != AL_NO_ERROR) endWithError(errors[9]); //Error when playing sound }
int SoundSource::LoadFile(const char* file) { //Variables to store info about the WAVE file (all of them is not needed for OpenAL) char type[4]; DWORD size,chunkSize; short formatType,channels; DWORD sampleRate,avgBytesPerSec; short bytesPerSample,bitsPerSample; DWORD dataSize; //Loading of the WAVE file FILE *fp = NULL; //Create FILE pointer for the WAVE file fp=fopen(file,"rb"); //Open the WAVE file if (!fp) return endWithError("Failed to open file"); //Could not open file //Check that the WAVE file is OK fread(type,sizeof(char),4,fp); //Reads the first bytes in the file if(type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F') //Should be "RIFF" return endWithError ("No RIFF"); //Not RIFF fread(&size, sizeof(DWORD),1,fp); //Continue to read the file fread(type, sizeof(char),4,fp); //Continue to read the file if (type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E') //This part should be "WAVE" return endWithError("not WAVE"); //Not WAVE fread(type,sizeof(char),4,fp); //Continue to read the file if (type[0]!='f' || type[1]!='m' || type[2]!='t' || type[3]!=' ') //This part should be "fmt " return endWithError("not fmt "); //Not fmt //Now we know that the file is a acceptable WAVE file //Info about the WAVE data is now read and stored fread(&chunkSize,sizeof(DWORD),1,fp); fread(&formatType,sizeof(short),1,fp); fread(&channels,sizeof(short),1,fp); fread(&sampleRate,sizeof(DWORD),1,fp); fread(&avgBytesPerSec,sizeof(DWORD),1,fp); fread(&bytesPerSample,sizeof(short),1,fp); fread(&bitsPerSample,sizeof(short),1,fp); fread(type,sizeof(char),4,fp); if (type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a') //This part should be "data" return endWithError("Missing DATA"); //not data fread(&dataSize,sizeof(DWORD),1,fp); //The size of the sound data is read //Display the info about the WAVE file cout << "Chunk Size: " << chunkSize << "\n"; cout << "Format Type: " << formatType << "\n"; cout << "Channels: " << channels << "\n"; cout << "Sample Rate: " << sampleRate << "\n"; cout << "Average Bytes Per Second: " << avgBytesPerSec << "\n"; cout << "Bytes Per Sample: " << bytesPerSample << "\n"; cout << "Bits Per Sample: " << bitsPerSample << "\n"; cout << "Data Size: " << dataSize << "\n"; soundData = new unsigned char[dataSize]; //Allocate memory for the sound data. kalla denna soundData cout << fread(soundData,sizeof(BYTE),dataSize,fp) << " bytes loaded\n"; //Read the sound data and display the //number of bytes loaded. //Should be the same as the Data Size if OK ALuint frequency = sampleRate; //The Sample Rate of the WAVE file ALenum format=0; //The audio format (bits per sample, number of channels) //Figure out the format of the WAVE file if(bitsPerSample == 8) { if(channels == 1) format = AL_FORMAT_MONO8; else if(channels == 2) format = AL_FORMAT_STEREO8; } else if(bitsPerSample == 16) { if(channels == 1) format = AL_FORMAT_MONO16; else if(channels == 2) format = AL_FORMAT_STEREO16; } if(!format) return endWithError("Wrong BitPerSample"); //Not valid format alGenBuffers(1, &mBuffer); //Generate one OpenAL Buffer and link to "buffer" alGenSources(1, &mSource); //Generate one OpenAL Source and link to "source" if(alGetError() != AL_NO_ERROR) return endWithError("Error GenSource"); //Error during buffer/source generation alBufferData(mBuffer, format, soundData, dataSize, frequency); //Store the sound data in the OpenAL Buffer if(alGetError() != AL_NO_ERROR) return endWithError("Error loading ALBuffer"); //Error during buffer loading fclose(fp); }