Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


[SDL] Array of Mix_Chunk and Mix_Music


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Jakez   Members   -  Reputation: 103

Like
0Likes
Like

Posted 01 March 2011 - 09:57 AM

Hey Guys

So far I have a list of Mix_Chunk declared on one line each like:

Mix_Chunk *one = NULL;
Mix_Chunk *two = NULL;
etc

This makes using them, for example a random of them, a bit weird.

Isnt there a way to declare them in a array. so that I can just use them with for example: miscsounds[i]? or a vector?

I have already looked it up in the documentation and searched for it, but couldn't find anything.

I tried different ways of doing which one seem logic for me, but that only gave my errors. So my next try, would be to make an "wrapper object" where I would just play a chunk thru a method like play_chunk(i).
Only I dont really know how tomake an object like that work dynamic. Maybe not even static without having it to be some "poor" level of coding.

I hope you understand what it is im trying to do.

Maybe there is a good way im overlooking you guys know of? Or should I give that wrapper a try?

Thanks in advance!
- Jakez

Sponsor:

#2 scgames   Members   -  Reputation: 2012

Like
0Likes
Like

Posted 01 March 2011 - 12:08 PM

Sure, you can store objects of those types in a container, write wrappers for them, etc.

There are various ways to do it though, so if you're having problems, perhaps you could post some of your code, along with any errors you're getting (or, if you're not getting errors, along with a description of how it's not working).

#3 Jakez   Members   -  Reputation: 103

Like
0Likes
Like

Posted 01 March 2011 - 05:39 PM

Alright so I tried doing that :)

Here is the interface:
class Sound
{
	private:
		Mix_Music *music;
		Mix_Chunk *chunk;
		bool musicfile;

	public:
		Sound(string,bool); //string is filename, bool is default false, if true, its music otherwise it will load as a chunk
		void Play(int,int); //first int, is loops. second int is channel default its -1 (random channel)
		void Quit();
};
Here is the implementation
Sound::Sound(string filename, bool ismusicfile = false)
{
	chunk = NULL;
	music = NULL;

	musicfile = ismusicfile;

	if(!musicfile)
		chunk = Mix_LoadWAV( filename.c_str() );
	else
		music = Mix_LoadMUS( filename.c_str() );
}


void Sound::Play(int loop, int channel = -1)
{
	if(musicfile)
		Mix_PlayMusic(music,loop);
	else
		Mix_PlayChannel(channel,chunk,loop);
}

void Sound::Quit()
{
     Mix_FreeChunk(chunk);
 	Mix_FreeMusic(music);
}

It seems to work as long as I declare it in a vector like this:
vector<Sound> sounds;
sounds.push_back("misc.wav"); //chunk declared
sounds[i].play(0);

So I guees it ended up working dynamically :) It does have problems if you declare it as a single object in a class. but dunno why anyone would wanna wrap a single music file anyway, so no big deal for me :)
Other functions as pausing and stopping seems not too need a method, since that is done not to the file, but the channel, which can be reached from anywhere.

Any comment on it? :)

#4 scgames   Members   -  Reputation: 2012

Like
0Likes
Like

Posted 01 March 2011 - 10:11 PM

It's a little hard to evaluate the code out of context, but I'll just make a couple of quick suggestions (this isn't a comprehensive analysis - it's just a couple of things I noticed):

1. Unless you need to work with a copy, pass objects of type std::string by constant reference (for purposes of clarity and efficiency).

2. Make sure you're cleaning up all your SDL_mixer resources properly. (This can be automated, for example, by using shared_ptr with a custom deleter.)

#5 mrchrismnh   Members   -  Reputation: 82

Like
0Likes
Like

Posted 01 March 2011 - 10:18 PM

I put SDL sounds and soundtracks in std::vectors and it works fine.
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy


#6 Jakez   Members   -  Reputation: 103

Like
0Likes
Like

Posted 03 March 2011 - 07:22 AM

Thanks jyk, I will look into that :)

I also tried it again mrchrismnh. Got it to work this time, dunno why I didn't get it to work before.
But I do have som problems erasing the files from memory once the game have run, I will have too look into that :)

Thanks for your help :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS