tags:

views:

19

answers:

1

I'm just trying to make a program that displays a window with a box in it, but when I run the program I only get the window display maybe once out of 5 runs. Every time I execute the command line gives appropriate responses and i see the window's title on the gnome panel at the bottom of the screen, but the window itself is invisible most of the time.

I have absolutely no idea where the cause could be and i want to start looking. Any advice?

I compile and run using 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  A: 

You need to register a display function callback before call glutMainLoop:

glutDisplayFunc(display);

And put your rendering functions inside display function:

void display(){
  glClearColor(1,1,1,1);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  /*
    Draw here
  */
  glutSwapBuffers();
}
tibur
thankyou so much!
alphomega