1
\$\begingroup\$

I'm developing a minesweeper game. I'm having a problem with reshaping(resize) the window. I'm using Ubuntu Linux. Following are the code snippets and the entire code is here:

This is my initializing code that I write at the starting of main()

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (400, 300);
glutCreateWindow ("MineSweeper");
glClearColor (1.0, 1.0, 1.0, 0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

This is my display() func that I pass into glutDisplayFunc() which actually draws the grid requires for minesweeper game.

glClear (GL_COLOR_BUFFER_BIT);

glColor3f (0.0, 0.0, 0.0);
glLineWidth (3);

float i;
for(i = step; i< 1; i+=step) {
    glBegin(GL_LINES);
        glVertex3f(i,0,0);
        glVertex3f(i,1.0,0);
    glEnd();
    glBegin(GL_LINES);
        glVertex3f(0,i,0);
        glVertex3f(1.0,i,0);
    glEnd();
}

glFlush ();

I searched many forum and finally tried this code for reshape() function in the glutReshapeFunc()

void reshape (int width, int height) {
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0, width, 0, height, -1.0, 1.0);
   glFlush();
}

But it didn't work and the output was a plane window.

\$\endgroup\$
4
  • \$\begingroup\$ You're asking several questions at the same time, so it's hard to give you a good answer. I suggest you try to narrow down your question to the problem that is the most important for you at the moment. What exactly is blocking you? \$\endgroup\$ Sep 26, 2013 at 20:19
  • \$\begingroup\$ Is it OK @LaurentCouvidou. Can you now please help me atleast with the resizing window problem? \$\endgroup\$
    – meteors
    Sep 27, 2013 at 2:03
  • \$\begingroup\$ Could you tell us what exactly is the problem with the resizing? Is it impossible to resize? \$\endgroup\$
    – MiJyn
    Sep 27, 2013 at 3:30
  • \$\begingroup\$ @MiJyn No when I resize the window everthing that I've drawn turns blank into white. \$\endgroup\$
    – meteors
    Sep 27, 2013 at 4:53

1 Answer 1

2
\$\begingroup\$
glViewport(0, 0, width, height); // reset the viewport
glMatrixMode(GL_PROJECTION); // modify the projection matrix
glLoadIdentity();            // load an identity matrix into the projection matrix
glOrtho(0, width, 0, height, -1.0, 1.0); // create new projection matrix

/// Important!!! You need to switch back to the model-view matrix
/// or else your OpenGL calls are modifying the projection matrix!
glMatrixMode(GL_MODELVIEW); // return to the model matrix
glLoadIdentity();           // load an identity matrix into the model-view matrix

// OpenGL has now compensated for the resized window, and is ready to draw again.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .