I'm trying to add different camera models to my graphics framework and I'm running into some difficulties with function pointers. I have created a BaseWindow class to which you can add event handlers. The expected function pointer that you have to pass to this function looks like this:
void (*handler)(const Event& e);
So far so good, I can pass global functions as argument and have them handle all kind of events. However, part of the beauty of being able to have multiple event handlers listening for the same event is that any object that needs to be notified on user input can register for receiving those events. This is where I'm getting problems...
I have created a base camera class (Camera) which specifies a pure virtual regEventHandlers function, which takes only a BaseWindow object as argument.
virtual void regEventHandlers( BaseWindow* wnd ) = 0;
Derived Camera classes that implement certain camera behaviours (like FPSCamera, or RPGCamera, or anything really) ought to implement this function so they can register for the events that are relevant to their functioning (for example, a flightsim camera would probably want to be notified of joystick events, whereas the mouse is an important event for FPS cameras...). So I tried implementing this for my FPSCamera and ran into the problem that the calling convention of class member functions was different than the calling convention of the expected function pointer in the addEventHandler function. I thought I'd solve this by explicitly setting the calling conventions for the input handling functions in the FPSCamera class to be __cdecl, but this causes an other error. I'll list the implementation of the regEventHandlers function of the FPSCamera class below and also show the error it generates...
/**
* Function: regEventHandlers
* Return: void
* Arguments: A pointer to a BaseWindow object
* Description: Registers the appropriate event handlers to receive all necessary
* user input to make this a true FPS camera.
*/
void FPSCamera::regEventHandlers( BaseWindow* wnd )
{
wnd->addEventHandler( WINDOW_KEYDOWN, &FPSCamera::onKeyDown );
wnd->addEventHandler( WINDOW_KEYUP, &FPSCamera::onKeyUp );
wnd->addEventHandler( WINDOW_MOUSEDOWN, &FPSCamera::onMouseDown );
wnd->addEventHandler( WINDOW_MOUSEUP, &FPSCamera::onMouseUp );
}
These are the errors I get:
cannot convert parameter 2 from 'void (__cdecl FPSCamera::* )(const Event &)' to 'void (__cdecl *)(const Event &)'
Does anyone know a way of doing this or am I really going to have to change my design?
Regards,
Rogier