1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
class MusicBox{
private:
vector<string> mList;
ALLEGRO_SAMPLE *mSample;
int mLoaded;
public:
void AddMusic(string);
void Play(int,bool);
void Destroy();
};
void MusicBox::AddMusic(string fName)
{
//Add file name to the playlist
mList.push_back(fName);
//If this is the first file added, load
//this file as default sample(song)
if (mList.size() == 1)
{
mSample = al_load_sample(mList[0].c_str());
mLoaded = 0;
}
//Increase the number of max simultaenous files that
//can be played overlapping
al_reserve_samples(mList.size()+1);
}
void MusicBox::Play(int Index = -1, bool loop = false)
{
//If out of bounds, exit function
if (Index > mList.size()-1 || Index < -1) return;
//If Index is -1 then play the current loaded file
//(This allows x.Play() to just replay the same song without passing arguments)
if (Index == -1) Index = mLoaded;
//If the Index to be played is different than the last file played,
//load the new file, otherwise don't reload the same file again.
if (mLoaded != Index) mSample = al_load_sample(mList[Index].c_str());
//Set the currently loaded file = to the file we just loaded
mLoaded = Index;
//Play the file
if (loop == false) al_play_sample(mSample,1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
if (loop == true) al_play_sample(mSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, 0);
}
void MusicBox::Destroy() { al_destroy_sample(mSample); }
|