Mix_ChannelFinished and OO

Hi everyone,

I am new to SDL (SDL_mixer) and relatively new to C++ so this is both an
SDL and C++ question. I search the archives and could not find an
answer.

I created a wrapper class called SoundEffect to make the SDL_mixer calls
OO. The issue I am having is trying to get the Mix_ChannelFinished
callback working with my class. I would like to pass my “void
SoundEffect::onDone(int val)” member function to Mix_ChannelFinished.

This is what I currently have :

	void (SoundEffect::*done)(int); 
	done = &SoundEffect::onDone;
	Mix_ChannelFinished(done);

Clearly this does not work as the parameter to the Mix_ChannelFisished
is : void (*channel_finished)(int channel))

Any thoughts and suggestions would be gladly appreciated.

Byron Wright

  void (SoundEffect::*done)(int);
  done = &SoundEffect::onDone;
  Mix_ChannelFinished(done);

Clearly this does not work as the parameter to the Mix_ChannelFisished
is : void (*channel_finished)(int channel))

Any thoughts and suggestions would be gladly appreciated.

You need to have a static method, so the hidden “this” pointer isn’t part
of the function signature.

You’ll probably need to do something like:

#define MAX_CHANNELS 16 // you can make this dynamic if you want.

class SoundEffect
{
static SoundEffect *handlers[MAX_CHANNELS];
static void catchChannelDone(int chan);
// etc.
};

SoundEffect *SoundEffect::handlers[MAX_CHANNELS];
void SoundEffect::catchChannelDone(int chan)
{
handlers[chan]->onDone();
handlers[chan] = NULL;
}

…which is lame. A better solution is (ack!) to break backwards
compatibility and add an argumentto Mix_ChannelFinished():

SoundEffect::SoundEffect(blah)
{
Mix_ChannelFinished(SoundEffect::catchChannelDone, this);
}

and then catchChannelDone would take an int and a void *:

void SoundEffect::catchChannelDone(int chan, void *arg)
{
((SoundEffect *) arg)->onDone();
}

Thoughts? I’m more inclined to support backwards compatibility for now,
but tear this up for SDL_mixer 2.0.

–ryan.

Thoughts? I’m more inclined to support backwards compatibility for now,
but tear this up for SDL_mixer 2.0.

Agreed.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment