/* ************************************************************************* *
OldSkoolGravityGame (OSGG) Lunar Lander-like game for linux.
Copyright (C) 2008 Jimmy Christensen ( dusted at dusted dot dk )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
* ************************************************************************* */
#include "sound.hpp"
bool soundClass::init() {
#ifndef NOSOUND
m=0;
int audio_rate = 44100;
Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
int audio_channels = 2;
int audio_buffers = 1024;
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
cout << "Error: Could not open audio device."< pl;
vector::iterator plIt;
bool same=0;
int freeChannel = -1; //The channel we will use for this sample
//Loop through queue and find samples thare are the same,and put in a new vector
for(vector::iterator it = q.begin(); it != q.end(); ++it)
{
//Loop thrugh the playlist to see find out if this allready exist
same=0;
for(plIt=pl.begin(); plIt != pl.end(); ++plIt)
{
if(plIt->s == it->s)
{
same=1;
plIt->num++;
}
}
//this sample is not yet in the playlist
if(!same)
{
pl.push_back( *it );
plIt = pl.end();
--plIt;
plIt->num=1;
}
}
q.clear();
//Play the actual samples :)
nozzleOn=0;
for(plIt = pl.begin(); plIt != pl.end(); ++plIt)
{
//Find a free channel:
for(int i=0; i < MIX_CHANNELS; i++)
{
if(!Mix_Playing(i))
{
freeChannel=i;
break;
}
}
if(plIt->s == sfxNozzle)
{
nozzleOn = 1;
//Only play if not allready playing
if(nozzleCh == -1)
{
nozzleCh = Mix_PlayChannel(freeChannel, sample[plIt->s], -1);
}
} else {
if(Mix_PlayChannel(freeChannel, sample[plIt->s], 0) == -1)
{
printf("Sample %i: %s\n",plIt->s, Mix_GetError());
}
}
}
//Check to see if the nozzle is on
if(!nozzleOn)
{
//Kill the sound
if(nozzleCh > -1)
Mix_FadeOutChannel(nozzleCh,32);
nozzleCh = -1;
}
#endif
}
soundClass::~soundClass()
{
#ifndef NOSOUND
for(int i=0; i < SNDSAMPLES; i++)
{
Mix_FreeChunk(sample[i]);
}
Mix_CloseAudio();
#endif
}