PDA

View Full Version : Interaction managed in separated class



thibault
03-31-2009, 04:53 PM
Hello everybody !
I would like to know if I can call the callback functions (keyboard, mouse, etc.) from separate classes.

For example I have a main renderer (handled by a GLFW window in my case), and I would like to call for mouse events :


glfwSetMouseButtonCallback(interaction->mouseButtonCallback);

given that the renderer has an attribute :

Interaction* interaction;
which is the class that manages all the inputs, and has a function :

void Interaction::mouseButtonCallback(int button, int action) {...}
This leads me to an error like :
error: argument of type ‘void (Interaction::)(int, int)’ does not match ‘void (*)(int, int)’

Is there a way to do it or am I condemned to handle the inputs in the same class as my renderer ?

Any advice is really welcome !

Thibault.

thibault
03-31-2009, 05:45 PM
Ok, I'm replying to my owm post.. Silly thing, I just should have created my callback functions static..

thibault
04-01-2009, 04:20 PM
Well, at least now I know I should not reply so fast... Obviously making a function static implies that all the data it handles must be static... Which I have to avoid.
So does anyone have an idea about how to use a mouseButtonCallback function that belongs to an external class ?

One thing that works is creating a "mouseInteraction()" function that belongs to the external class which is called in the callback function, but it's not so clean...

Stephen A
04-11-2009, 02:48 AM
Instance methods cannot be used as function pointers. There are several reasons for this, including issues with inheritance (virtual methods resolved through v-tables) and the hidden 'this' parameter.

Now, it's been a long time since I last used C++, but IIRC you can solve this issue using 'functors' (google is your friend).

Stuart McDonald
04-12-2009, 01:27 AM
How about....(probably typos)



class InteractionCallback {
static Interaction* pInteraction;
public:
static void setInteraction(Interation* pI) { pInteraction = pI; }
static void mouseButton(int button, int action)
{
pInteraction->mouseButtonCallback(button, action);
}
};
InteractionCallback::setInteraction(&myInteraction );
glfwSetMouseButtonCallback(InteractionCallback::mo useButton);

thibault
04-21-2009, 05:54 PM
Hi, sorry for the late reply I had not seen your posts.
What I eventually did is wrapping the functions like :


void mouseButtonCallback(int button, int action)
{
r->mouseButtonInteraction(button, action);
}

Where r is the class managing the interaction. It works, and doesn't make such a mess finally...
I haven't tried what you're suggesting Stuart McDonald, but in some other context where I'm not too afraid of using static functions, I will give it a try !
Thanks for your answers guys.