以下は、c++ から ac コールバック関数を登録する方法を示しています。これは一般的に有用ですが、glut に固有のものではありません。
クライアントの C++ プログラムは次のとおりです。
int main(int argc, char *argv[]) {
std::cout << "launching Camera ..." << std::endl;
Camera * camera = new Camera();
// ------ glut new window boilerplate ------- //
int WindowHandle = 0;
glutInit(&argc, argv);
WindowHandle = glutCreateWindow("hello there");
if(WindowHandle < 1) {
std::cerr << "ERROR: Could not create a new rendering window" << std::endl;
exit(EXIT_FAILURE);
}
// ------------------------------------------ //
camera->setup_callback();
glutMainLoop();
return 0;
}
これがCamera.cppです
Camera * ptr_global_instance = NULL;
extern "C" void ReshapeCamera_callback(int width, int height) {
// c function call which calls your c++ class method
ptr_global_instance->ReshapeCamera_cb(width, height);
}
void Camera::ReshapeCamera_cb(int width, int height) {
std::cout << "width " << width << " height " << height << std::endl;
}
void Camera::setup_callback() {
// c++ method which registers c function callback
::ptr_global_instance = this;
::glutReshapeFunc(::ReshapeCamera_callback);
}
およびそのヘッダー Camera.h
class Camera {
public:
void ReshapeCamera_cb(int width, int height);
void setup_callback();
};
C++ グローバル クラス ポインター ptr_global_instance の使用に注意してください。