Search code examples
c++glut

C++ update OpenGL/Glut window


I've tried some OpenGL C++ training. But I have a logic problem, how can I update my OpenGL Windows window. It should draw text one, then delay 1-2sec, then draw text 2, but now it draws same time. Can anyone help or give a hint.

void text () {  
    wait(1);
    Sleep(1000);
    std::string text_one;                                                                    
    text_one = "Text 1";                                                           
    glColor3f(1,01, 0);                                                                
    drawText(text_one.data(), text_one.size(), 050, 150);

    glutPostRedisplay();

    wait (1)
    std::string text_two;                                                                    
    text_two = "Text 2";                                                           
    glColor3f(1,0, 0);                                                                
    drawText(text_two.data(), text_two.size(), 250, 150);
}

and here the main

int main(int argc, char **argv) {

    // init GLUT and create Window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(640,640);
    glutCreateWindow("Test 001");

    // register callbacks

    glutDisplayFunc(renderScene);
    glutIdleFunc(text);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}

Solution

  • You should render in renderScene callback. It will be called automatically in you screen refresh rate. If you want some delay you need to implement it inside this callback (functions called from this callback).

    So basically you need to re-render everything every 1/60 second.

    If you want to implement easy delay you can do something like this:

    void renderScene() {
        time += deltaTime;
        RenderText1();
        if (time > delayTime) 
            RenderText2();
        glutSwapBuffers();
    }