使用过剩创建窗口,但程序不一致

时间:2010-09-15 14:49:48

标签: c glut freeglut

我只是想制作一个程序,显示一个带有框的窗口,但是当我运行该程序时,我只能在5次运行中获得一次窗口显示。每次执行命令行都会给出相应的响应,我会在屏幕底部的gnome面板上看到窗口的标题,但窗口本身大部分时间都是不可见的。

我完全不知道原因可能在哪里,我想开始寻找。有什么建议吗?

我使用gcc main.c -lglut编译并运行:

#include <stdio.h>
#include <GL/freeglut.h>

void keyb(unsigned char key, int x, int y);

//Window identifier
static int win;

main(int argc, char **argv){
    //Initialization
    glutInit(&argc, argv);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    win = glutCreateWindow("Test Window");

    //Callback
    glutKeyboardFunc(keyb);

    //OpenGL
    glClearColor(0.0,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);
    glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
    glBegin(GL_POLYGON);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();

    //Enter main loop
    glutMainLoop();

    return 0;
}

void keyb(unsigned char key, int x, int y){
    printf("Pressed key %c on coordinates %d, %d\n", key, x, y);
    if(key == 'q'){
        printf("Got q, so qutting\n");
        glutDestroyWindow(win);
        exit(0);
    }
}

1 个答案:

答案 0 :(得分:2)

您需要在致电glutMainLoop之前注册显示功能回调:

glutDisplayFunc(display);

将渲染函数放在display函数中:

void display(){
  glClearColor(1,1,1,1);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  /*
    Draw here
  */
  glutSwapBuffers();
}