/* ************************************************************************* * SDL-Ball - DX-Ball/Breakout remake with openGL and SDL 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 struct sampleQueuedItem { int s,p,num; }; class soundClass { private: #ifndef NOSOUND //The actual sound samples Mix_Chunk *sample[SNDSAMPLES]; //List of queued samples vector q; void loadSample(const char *SampleName, int sampleNum); int m; //Secret undocumented variable #endif public: bool init(); void play(); void add(int i, GLfloat x); void loadsounds(); ~soundClass(); }; 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, average their position 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++; plIt->p += it->p; } } //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 :) 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(setting.stereo) { plIt->p /=plIt->num; Mix_SetPanning(freeChannel, 255-plIt->p, plIt->p); } if(plIt->s == SND_NORM_BRICK_BREAK) { switch(m) { //Case 0 = SND_NORM_BREAK(a) case 1: plIt->s = SND_NORM_BRICK_BREAKB; break; case 2: plIt->s = SND_NORM_BRICK_BREAKC; break; case 3: plIt->s = SND_NORM_BRICK_BREAKD; break; case 4: plIt->s = SND_NORM_BRICK_BREAKE; break; } m++; if(m==5) m=0; } if(Mix_PlayChannel(freeChannel, sample[plIt->s], 0) == -1) { printf("Sample %i: %s\n",plIt->s, Mix_GetError()); } } #endif } soundClass::~soundClass() { #ifndef NOSOUND for(int i=0; i < SNDSAMPLES; i++) { Mix_FreeChunk(sample[i]); } Mix_CloseAudio(); #endif }