2013-05-20 93 views
-1

我有一個程序,我從我的主窗口創建了一個子窗口。問題在於它總是在那裏,我的目標是按下一個鍵,然後將該子窗口「打開」,然後再次按下時,使其消失。我設法用glutDistroyWindow摧毀它,但後來我不知道如何讓它再次出現。這是我的代碼:Glut子窗口切換

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; 
} 

好的,所以這些是重要的功能。在我的鍵盤功能中,我想切換damageWindow。我怎麼做 ?我知道如何摧毀它,但我似乎無法再重複它。 LE:我不斷收到downvotes,因爲人們並不真正理解這個問題。鍵盤功能是多餘的,因爲什麼也沒有,那是什麼IM要求you.But爲你們在這裏它的緣故:

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); 
      } 
    } 
} 
+1

請修復格式。爲什麼你使用兩個重塑功能?我沒有看到「鍵盤」功能,你忘了發佈嗎? –

+0

-1,代碼不完整。 – genpfault

回答

0

我不認爲你應該創建或銷燬任何窗口你叫glutMainLoop後。在初始化期間,你應該創建子窗口,然後你應該隱藏它,使用glutHideWindow,可能在調用glutSetWindow使它成爲當前窗口。要再次顯示,請致電glutShowWindow

此外,您不需要多次調用諸如glutReshapeFuncglutKeyboardFunc之類的函數,只需在初始化期間執行一次即可。如果您需要他們根據條件做不同的事情,請在傳遞給他們的功能中使用if。