I am trying to create a window container for sfml windows. If you are not familiar with sfml, a sfml window is a class which is non-copyable so cannot be put inside a std::vector for example.
I’ve done most of the work, but I am now trying to implement some sort of callback function which will do the open-gl drawing work before window.Display() is called to update the window.
I cannot get my head around the syntax of a callback function (either c or c++ version). Would someone be willing to explain an example to me?
Even better, would someone be willing to explain an example of how to implement a callback function in a class, so that a function (which the user defines) can be passed into a function of the class which will remember what that function was so it can be called by the class later.
To make that above sentence clearer: I think there are three things I need:
1): A function which the user defines, for example:
void doDrawing(){
// do some open gl stuff
}
2:) A function inside a class which takes the argument of the above, user defined function:
void setDisplayFunc( ?argument of a function? argument ){
function_pointer pointer = argument;
}
I have no idea what the type of ‘argument’ should be, or what ‘function_pointer’ has to be… I think this is where I am most stuck!
3:) A way in which the class can call the function which was assigned above.
// Do the function which is pointed at by pointer, which is of the type 'function_pointer' ... Whatever function_pointer happens to be!
If anyone has used glut before, then I guess I am trying to implement the glut callback functions which look like:
void someDisplayFunction(){
// do open gl stuff
}
glutDisplayFunc( someDisplayFunction );
The above function takes the argument of a user-defined function and then does it when we call ‘glutPostRedisplay’ …
I hope this makes sense, I have tried to include as much information as I can to help. Any help is appreciated. (:
You want a function pointer. In your case:
With that you can write:
To call it you simply do: