简体   繁体   中英

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 ?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM