简体   繁体   English

指向SDL音频回调的成员函数的指针?

[英]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 . 我使用SDL 1.2.15打从libav(ffmpeg的)声音就像这个例子在这里

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 ). 但是,现在我必须在SDL_AudioSpec::callback存储用于回调函数的指针(在示例wanted_spec.callback )。 But my callback is a member of my class. 但是我的回调是我班的成员。

The callback pointer in SDL is: SDL中的回调指针是:

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

where SDLCALL is __cdecl . 其中SDLCALL__cdecl

How can I store a pointer of a member-function in my wanted_spec_.callback ? 如何在我的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. 如果要使用非静态成员函数来处理回调,则需要提供转发功能并将userdata设置为指向目标对象的指针。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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