Allegro 5 Sound Class

I'm building a class to store sound files and allow them to be played. I am "trying" to store the sounds in a vector and then passing their index to play them. In my mind, I'm visualizing the class acting like a music box (iPod) storing lots of songs instead of creating an individual class per song, thus the class name.

I followed this tutorial below to get sound into my games.. it works following the example, but trying to expand on the tutorial and create the music box isn't working so well. I believe my error is with trying to define the by reference sample(sound) variable in a vector, but i'm not sure.

Tutorial: http://www.youtube.com/watch?v=IAHDroPABk4&list=SP6B459AAE1642C8B4&index=18

My code:
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
class MusicBox{
private:
	vector<string> mList;
	vector<ALLEGRO_SAMPLE> *mSample;
public:
	void AddMusic(string);
	void Play(int);
	void Destroy();
};

void MusicBox::AddMusic(string fName)
{
	mList.push_back(fName);
	mSample.push_back();
	mSample[mSample.size()-1] = al_load_sample(mList[mList.size()-1].c_str());
	al_reserve_samples(mList.size());
}

void MusicBox::Play(int Index)
{
	al_play_sample(mSample[Index],1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
}

void MusicBox::Destroy()
{
	for (int i=0; i<mSample.size(); ++i)
		al_destroy_sample(mSample[i]);
}
I found a work around. If anyone else has a similar issue, my working code is posted below:

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); }
Topic archived. No new replies allowed.