Hello guys. This is my first time here, and as a noob in OpenGl I have a simple ,but hard for me, problem. I’m making a tetris-like program, but each time I want to display the shapes to the position they end up, the window clears itself. I cannot explain it better, so here is the code:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
//-------------------------GLOBAL VARIABLES------------------------------
int score=0;
char scoreview[7]=" ";
float myX=80, myY=-40;
float width=40, height=40;
GLsizei winWidth = 240, winHeight = 550;
GLuint regcube;
//-------------------------FUNCTIONS---------------------------
void reshape(GLint newWidth, GLint newHeight)
{
glViewport (0, 240, 0, 520);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
glOrtho (0, 240, 520, 0, 1,-1);
glMatrixMode (GL_MODELVIEW); // κατασκευή πίνακα για τους γεωμετρικούς μετασχηματισμούς
glLoadIdentity ();
glClear (GL_COLOR_BUFFER_BIT);
}
void drawBitmapText(char *string,float x,float y)
{
char *c;
glColor3f(0.0, 0.0, 0.0);
glRasterPos2f(x, y);
for (c=string; *c != '\0'; c++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
}
}
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
case 115:
if(myX!=0)
{
myX-=40;
}
break;
case 102:
if(myX!=200)
{
myX+=40;
}
break;
}
}
void display (void)
{
glutKeyboardFunc(keyboard);
if(myY!=480){
myY+=40;
score++; //checking if score changes without problems
}
else{
myY=-40;
}
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
//---------------------------------------printing------------------------
if(myY == 480){
printf("entered if
");
glColor3f (0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(myX, myY);
glVertex2f(myX+width, myY);
glColor3f(0.8,0.0,0.8);
glVertex2f(myX+width, myY+height);
glVertex2f(myX, myY+height);
glEnd();
glBegin(GL_LINES);
glVertex2f(0, 500);
glVertex2f(240, 500);
glEnd( );
}
//-----------------------------------------------------------------------
glColor3f (0.0, 0.0, 0.0);
//rendering line to separate space for score------------------------
glBegin(GL_LINES);
glVertex2f(0, 521);
glVertex2f(240, 521);
glEnd( );
//rendering score---------------------------------------------------
sprintf(scoreview, "SCORE:%d", score);
drawBitmapText(scoreview, 10,545);
//setting matrix mode for object movement---------------------------
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//object movement code----------------------------------------------
glPushMatrix();
// glTranslatef(0.0f, 40.0f, 0.0f);
// glColor3f (0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(myX, myY); //katw aristerh
glVertex2f(myX+width, myY); //katw deksia
glColor3f(0.8,0.0,0.8);
glVertex2f(myX+width, myY+height); //panw deksia
glVertex2f(myX, myY+height); //panw aristera
glEnd();
glutPostRedisplay ( );
glPopMatrix();
// glLoadIdentity();
glutSwapBuffers();
sleep(1);
glFlush ( ); // Process all OpenGL routines as quickly as possible.
}
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to black.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
glLoadIdentity();
glOrtho (0, 240, 550, 0, 1,-1); //gluOrtho2d did not work properly, this one sets the ortho
}
//-------------------------MAIN--------------------------------
int main (int argc, char** argv)
{
int playwindow;
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (100, 100); // Set top-left display-window position.
glutInitWindowSize (240, 550); // Set display-window width and height.
glutCreateWindow ("VraXaPsa"); // Create display window.
glViewport(0, 0, 240, 550);
glutKeyboardFunc(keyboard); //edw
init ( ); // Execute initialization procedure.
glutDisplayFunc (display); // Send graphics to display window.
glutMainLoop(); // Display everything and wait.
return 0;
}
If anyone can help, it would be much much appreciated. Not just because it is a minor problem, but because I think the problem lies in me not understanding OpenGl clearly, and I think I might have messed up the initialisation of the program.