I have an class with instance functions (or methods?). From within an instance, I try to pass pointers to those functions to a library. The library expects static functions.
When I pass my pointers to the callback functions, the compiler complains that my functions are not static. I tried to put make them static, but if I do so, then I can't access the instance fields from within the functions.
How could I go around this?
Similar question is : Using a C++ class member function as a C callback function where they suggest to put the method static. However I can't do that, or I don't see how I could.
GlutController::GlutController (int argc, char **argv) {
// stuff ..
// Register callbacks
glutSpecialFunc( OnSpecialKeys ); // Error, need static functions
glutReshapeFunc( OnChangeSize ); // Error...
glutDisplayFunc( OnRenderScene ); // Error...
// stuff ..
}
GlutController::~GlutController() {
}
void GlutController::OnChangeSize(int aNewWidth, int aNewHeight){
glViewport(0,0,aNewWidth, aNewHeight);
mViewFrustrum.SetPerspective( APP_CAMERA_FOV, // If this function is
float( aNewWidth ) / float( aNewHeight ), // static, this won't
APP_CAMERA_NEAR, // work
APP_CAMERA_FAR );
mProjectionMatrixStack.LoadMatrix( // Same here
mViewFrustrum.GetProjectionMatrix() );
mTransformPipeline.SetMatrixStacks(mModelViewMatrixStack, // Same here
mProjectionMatrixStack);
}
void GlutController::OnRenderScene(void){
mGeometryContainer.draw(); // Won't work if static
}
void GlutController::OnSpecialKeys(int key, int x, int y){
mGeometryContainer.updateKeys(key); // Won't work if static
}
Disclaimer : I just begun C++. I read all of Accelerated C++ and this is my first project to try out the language. My background is in Java.
What you are trying to do is not possible. This is glut
's fault actually.
The thing is this:
glut
wants to call a function, without giving it data,which are conflicting needs. I believe glut
decided you can safely use global variables.
So, one solution is to use static functions, with static data. Or a better solution would be to switch to SDL.