Search code examples
c++openglglutfreeglut

using a complex display function prevents glutKeyboardFunc from working


Using glutKeyboardFunc in a very simple exemple, i could get it to work very easily :

void special(int key, int x, int y) {
    printf("key %d\n", key);
}

void keyboard(unsigned char key, int x, int y) {
    printf("key %d\n", key);
}

void display() {}

int main(int argc, char** argv) {
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(640, 480);

    glutCreateWindow("GLUT Program");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard); 
    glutSpecialFunc(special); 

    glutMainLoop();
    return EXIT_SUCCESS;
}

but using it with a very complex display function, the keyboard routine is never called, I am not sure about the part that prevents this call from working so i am just asking here for ideas about what could be the cause, i can't really post the code because that would be the entire project... i am using freeglut with an opengl context and glm for math computation. Nevertheless here is the new call :

static void loop_function() {
    glutSetWindow(win);
    Scene::unique_scene->mainloop();
}

I am a bit lost about what to do to find the error, if someone could enlighten me i would be grateful.


Solution

  • Having an infinite loop in the glutDisplayFunc is not the way to go, it is called automatically at the end of the glutKeyboardFunc if you added the call

    glutPostRedisplay();