user1810087
user1810087

Reputation: 5334

Pointer to member-function for SDL audio callback?

I'm using SDL 1.2.15 to play audio from libav (ffmpeg) like in this example here.

I have declared a class for playing the sound. But now I have to store a pointer for the callback function in the SDL_AudioSpec::callback (in the example wanted_spec.callback). But my callback is a member of my class.

The callback pointer in SDL is:

void (SDLCALL *callback)(void *userdata, Uint8 *stream, int len);

where SDLCALL is __cdecl.

How can I store a pointer of a member-function in my wanted_spec_.callback?

Upvotes: 2

Views: 1553

Answers (1)

Captain Obvlious
Captain Obvlious

Reputation: 20063

If you want to use a non-static member function to handle the callbacks you will need to provide a forwarding function and set userdata to the pointer to the target object.

struct CallbackObject
{
    void onCallback(Uint8 *stream, int len)
    {
        // ....
    }

    static void forwardCallback(void *userdata, Uint8 *stream, int len)
    {
        static_cast<CallbackObject*>(userdata)->onCallback(stream, len);
    }
};



SDL_AudioSpec audio;
CallbackObject callbackObject;

audio.callback = CallbackObject::forwardCallback;
audio.userdata = &callbackObject;

Upvotes: 8

Related Questions