I am aware that I don't need to use a loop to get multiple sources, and that I should probably do some error checking in my sound loading method, I will fix that soon. However, do I need to explicitly create a device and context or not? When I do create the device and context, the sound plays very slow and choppy.
Anyway, does my code look right or do you guys see a misconception somewhere?
Thanks!
Scott
ALboolean InitAL()
{
//device = alcOpenDevice(NULL);
//context = alcCreateContext(device, NULL);
//alcMakeContextCurrent(context);
alutInit(NULL, 0);
if(alGetError() != AL_NO_ERROR)
return false;
return true;
}
vector<ALuint> FileManager::loadSound(int num, bool l, string id)
{
//Get Full Path
std::stringstream ss;
ss.str("");
ss << soundPath << id;
vector<ALuint> sources;
ALuint buffer(alutCreateBufferFromFile(ss.str().c_str()));
for(int i = 0; i < num; i++){
ALuint tempSource;
alGenSources(1, &tempSource);
alSourcei(tempSource, AL_BUFFER, buffer);
alSourcef(tempSource, AL_PITCH, 1.0);
alSourcef(tempSource, AL_GAIN, 1.0);
if(l) alSourcef(tempSource, AL_LOOPING, AL_TRUE);
sources.push_back(tempSource);
}
return sources;
}
bool Game::start()
{
userShip = shared_ptr<Actor>(new Ship());
shared_ptr<class Ship> tempShip(dynamic_pointer_cast<class Ship>(userShip));
tempShip->addThrusterSound(
fileManager.loadSound(tempShip->getNumThrusters(), true, "Thruster.wav"),
fileManager.loadSound(tempShip->getNumThrusters(), false, "ThrusterEnd.wav"));
return true;
}
void Thruster::control(Controller& input, GLfloat dt)
{
for each(int i in key){
if(input.wasPressed(i)){
isActive = true;
alSourcePlay(thrusterLoop);
alSourceStop(thrusterEnd);
break;
}else if(input.wasReleased(i)){
bool stillActive = false;
for each(int x in key){
if(input.isDown(i)){
stillActive = true;
break;
}
}
if(!stillActive){
isActive = false;
alSourcePlay(thrusterEnd);
alSourceStop(thrusterLoop);
}
}
}
}