[SDL] Tryig to use SDL_audio in a class structure
Lilith Calbridge
lilith at dcccd.edu
Sat Jul 22 05:43:43 PDT 2006
Can you not take the Windows tactic of declaring the callback function
as a static member? That leaves one "shared" instance of the function
for all objects of the class.
Please excuse me if this is a bit naive. I'd been out of programming
for years before I finally got back into learning programming Windows
and in the rush I haven't had much time to dwell on the more minute
mechanics of it.
--
Lilith
>>> On 7/22/2006 at 12:45 AM, in message
<44C1BB64.6010205 at freeuk.com>,
darkmatter at freeuk.com wrote:
> Dhruv Thukral wrote:
>> Hello All,
>>
>> I am using the SDL library for sound in my QT application (since
somehow my
>> native platform does not suppot sound using QT). I have created a
seperate
>> class for sound and am trying to compile my project but I keep
getting the
>> following errors:
>>
>> Error 1: argument of type 'void MyClass::)(void*, Uint8*, int) does
not
>> match 'void (*)(void *, Uint8*, int)'
>> Error 2: argument of type 'void (MyProgram::)(int) does not match
void (*)
>> (int)'
>>
>> My program structure looks like this:
> <snip>
>
> You can't use C++ class member functions as callbacks, at least not
> directly.
>
> If the callback prototype has a "User data" pointer, you can do it
with
> some trickery. This rules out signal handlers as there is no such
> pointer passed. You can however do it with the SDL audio callback as
> there is such a field. You would do something like this
>
> class MyClass
> {
> SDL_AudioSpec spec;
>
> void callback(Uint8 *, int);
> void play (char *);
> };
>
>
> void MixCallback(void *class, Uint8 *stream, int len)
> {
> MyClass *ptr = (MyClass *) class;
>
> ptr->callback(stream, len);
> }
>
> void MyClass::callback(Uint8 *stream, int len)
> {
> // whatever here
> }
>
> void MyClass::play(char *file)
> {
> //some code
>
> spec.userdata = (void *) this;
> spec.callback = MixCallback;
> }
>
>
> What happens here is that the C function MixCallback() is called by
SDL,
> and then the user data field is used to retrieve the "this" pointer
of
> the class. This is then used to call the real callback function
inside
> the class.
>
> Bear in mind that you will only be able to have ONE instance of this
class.
>
> Pete.
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
More information about the SDL
mailing list