#include <iostream>
#include <math.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
#include <GL/glut.h>
static GLfloat rectFirstPositionX=0.0f,rectFirstPositionY=20.0f,rectSecondPositionX=5.0f,rectSecondPositionY=15.0f;
static GLfloat xDiff=3,yDiff=3;
float xAxis=20.0,xPrimeAxis=-20,yAxis=20,yPrimeAxis=-20,zAxis=10,zPrimeAxis=-10;
int count = 0,direction=0; //direction=0=assending,,,,,,direction=1=dessending
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glShadeModel(GL_FLAT);
glOrtho(xPrimeAxis,xAxis,yPrimeAxis,yAxis,zPrimeAxis,zAxis);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f(1,1.0,1.0);
glBegin(GL_LINES);
glVertex2f(0,yAxis);glVertex2f(0, yPrimeAxis);
glVertex2f(10,yAxis);glVertex2f(10, yPrimeAxis);
glEnd();
glPopMatrix();
glPushMatrix();
//glTranslatef();
glColor3f(0.05,0.5,0.1);
glRectf(rectFirstPositionX,rectFirstPositionY,rectSecondPositionX,rectSecondPositionY);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-20.0,20.0,-20.0,20.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void spindisplay(void)
{
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
count++;
if(rectSecondPositionX <=10 && rectSecondPositionY >= -20 && direction==0)
{
if(rectSecondPositionX==10 && rectSecondPositionY <=-19)
{
direction=1;
cout<<"do direction=1 "<<endl;
}
if(count%2==1)
{
cout<<"in 1st if"<<endl;
rectFirstPositionX=rectFirstPositionX+5;
rectFirstPositionY=rectFirstPositionY-5;
rectSecondPositionX=rectSecondPositionX+5;
rectSecondPositionY=rectSecondPositionY-5;
}
else if(count%2==0)
{
cout<<"in 1st else if"<<endl;
rectFirstPositionX=rectFirstPositionX-5;
rectFirstPositionY=rectFirstPositionY-5;
rectSecondPositionX=rectSecondPositionX-5;
rectSecondPositionY=rectSecondPositionY-5;
}
}
else if(rectFirstPositionX >=0 && rectFirstPositionY <=20 && direction==1 )
{
if(rectFirstPositionX==0 && rectFirstPositionY >= 20)
{
direction=0;
cout<<"do direction=0 "<<endl;
}
if(count%2==0)
{
cout<<"if count="<<count<<endl;
rectFirstPositionX=rectFirstPositionX-5;
rectFirstPositionY=rectFirstPositionY+5;
rectSecondPositionX=rectSecondPositionX-5;
rectSecondPositionY=rectSecondPositionY+5;
}
else if(count%2==1)
{
cout<<"---else if------- count="<<count<<endl;
rectFirstPositionX=rectFirstPositionX+5;
rectFirstPositionY=rectFirstPositionY+5;
rectSecondPositionX=rectSecondPositionX+5;
rectSecondPositionY=rectSecondPositionY+5;
cout<<"rectFirstPositionX="<<rectFirstPositionX<<" "<<"rectFirstPositionY="<<rectFirstPositionY<<endl<<
"rectSecondPositionX="<<rectSecondPositionX<<" "<<"rectSecondPositionY="<<rectSecondPositionY<<endl;
}
}
}
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Moving squares");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutIdleFunc(spindisplay);
glutMainLoop();
}