-1

I have a program and i have created a subwindow from my main window. The problem is that its always there, my goal is to press a key and then get that subwindow "on" and when i press it again , to make it disappear. I managed to destroy it with glutDistroyWindow but then i dont know how to make it appear again. Here is my code:

void init(void)
{

    // pregatim o scena noua in opengl
    if(glutGetWindow() == mainWindow)
        glClearColor(0.0, 0.0, 0.0, 0.0);   
    else
        glClearColor(1.0, 1.0, 1.0, 1.0); fereastra
    glEnable(GL_DEPTH_TEST);            
    glShadeModel(GL_SMOOTH);            
    glEnable(GL_LIGHTING);              
    glEnable(GL_NORMALIZE);             
}

void reshape2(int w,int h){

    glViewport(0,0,(GLsizei) w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,(float)w/h,1.0,40.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    init();

}

void reshape(int w, int h)
{
    // Main Window
    glViewport(0,0, (GLsizei) w, (GLsizei) h);
    // calculare aspect ratio ( Width/ Height )
    GLfloat aspect = (GLfloat) w / (GLfloat) h;


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45, aspect, 1.0, 100);

    // init context
    init();

    if(damageWindow != -1)
        glutDestroyWindow(damageWindow);

    damageWindow=glutCreateSubWindow(mainWindow,0,0,w/5,h/5);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape2);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(keyboard);
    glutKeyboardUpFunc(keyboardup);
    glutMouseFunc(mouse);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    int w = 800, h= 600;
    glutInitWindowSize(w,h);
    glutInitWindowPosition(100,100);

    // Main window
    mainWindow=glutCreateWindow("Tema4 - Asteroid Attack!");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutKeyboardUpFunc(keyboardup);
    glutReshapeFunc(reshape);
    glutSpecialFunc(keyboard);
    glutMouseFunc(mouse);


    // Initializeaza scena 3D
    initScene();

    glutMainLoop();
    return 0;
}

Ok so these are the functions that matters. In my keyboard function i want to toggle damageWindow. How do i do that ? I know how to destroy it but i can't seem to make it again.

LE: I keep getting downvotes because people dont really understand the question. The keyboard function is redundant because there is nothing there , thats what im asking you.But for the sake of you guys here it is:

void keyboard(unsigned char ch,int x,int y){
    switch(ch){
        case 27: exit(0);break;
        case 'n':
            view_subwindow=!view_subwindow;
            if(view_subwindow == false)
                glutDestroyWindow(damageWindow);
            else{
                //here i want to recreate my window DONT KNOW HOW
                damageWindow=glutCreateSubWindow(mainWindow,0,0,w/5,h/5);
                glutDisplayFunc(display);
                glutReshapeFunc(reshape2);
                glutKeyboardFunc(keyboard);
                glutSpecialFunc(keyboard);
                glutKeyboardUpFunc(keyboardup);
                glutMouseFunc(mouse);
            }
    }
}
4

1 回答 1

0

我认为您不应该在致电后创建或销毁任何窗口glutMainLoop。在初始化期间,您应该创建子窗口,然后您应该使用 隐藏它glutHideWindow,可能在调用glutSetWindow使其成为当前窗口之后。要再次显示,请调用glutShowWindow

此外,您不需要多次调用函数,只需在初始化期间执行一次glutReshapeFuncglutKeyboardFunc如果您需要他们根据条件执行不同的操作,请在传递给他们的函数中使用 if's。

于 2013-05-22T06:07:48.133 回答