MoustacheSpy
MoustacheSpy

Reputation: 763

Making SDL call a pure virtual member function as a event callback?

I am currently developing a small application class. It is supposed to make certain steps in using SDL2 less of a pain. For one I want a function called handleEvent to be overloaded when implementing the derived class (the application class is a base interface) to be passed the current SDL_Event automatically so that the user can do stuff with it.

I figured that using the inbuilt SDL Function SDL_AddEventWatch to create a callback would be better than launching a thread and have it manually check for new events and call the member function.

However, the SDL Documentation for the function doesnt specify using a member function as the "filter". And I am even using a pure virtual member function!

Please note that the only parameter that this function needs (from my perspective) is a pointer to the even to be processed. However the this pointer would still be required to be accessed so that the user can simply change stuff in the class directly.

What is the proper method of implementing this?

Upvotes: 0

Views: 278

Answers (1)

SoronelHaetir
SoronelHaetir

Reputation: 15162

I would use libffi's closures to create a wrapper around the instance that matches the expected signature. The function used to initialize the closure would take that saved instance parameter and whatever SDL passes and forward them into the virtual function.

Upvotes: 1

Related Questions