/*wersja na windows 7 */
#include <GL/glut.h>
#include <stdlib.h>
/*
C:\TDM-GCC-32>g++ -o test.exe test.cpp -lopengl32 -lglut32
C:\TDM-GCC-32>test
sprawdz instalacje glut for windows
*/
GLfloat d = 0.1;
int refreshMillis = 30;
struct kulka
{
GLfloat x;
GLfloat y ;
} kulka={0.0, -0.5} , wall= {1.0 , 1.0} , speed= {0.03, 0.04}, quad;
GLfloat x2 , y2=-0.7;
int menu = 0;
void drawBall2(GLfloat x2, GLfloat y2)
{
glColor3f(1.0,0.0,.0);
glPushMatrix();
glTranslatef(x2, y2,0.0);
glutSolidSphere(d,20,20);
glPopMatrix();
}
void keyboard (unsigned char key, int x, int y )
{
switch (key) {
case 'd':
x2=x2+0.1;
glutPostRedisplay();
break;
case 'a':
x2=x2-0.1;
glutPostRedisplay();
break;
case 'w':
y2=y2+0.1;
glutPostRedisplay();
break;
case 's':
y2=y2-0.1;
glutPostRedisplay();
break;
}
}
void drawBall1()
{
glColor3f(0.0,1.0,.0);
glPushMatrix();
glTranslatef(kulka.x,kulka.y,0.0);
glutSolidSphere(d,20,20);
glPopMatrix();
}
void update()
{
kulka.x+=speed.x;
kulka.y+=speed.y;
if (kulka.x>wall.x)
{
speed.x=-speed.x;
}
if ( kulka.y>wall.y)
{
speed.y=-speed.y;
}
if(kulka.x<-wall.x)
{
speed.x=-speed.x;
}
if(( kulka.y<d + y2) && (kulka.x <d +x2 )&& (x2-kulka.x<d) )
{
speed.y=-speed.y;
}
}
void Display()
{
glClearColor( 1.0, 1.0, 1.0, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 1.0, 0.0, 0.0 );
int w, h;
drawBall2(x2, y2);
drawBall1();
update();
glFlush();
glutSwapBuffers();
}
void Reshape( int width, int height )
{
Display();
}
void Timer(int value)
{
glutPostRedisplay();
glutTimerFunc(refreshMillis, Timer, 0);
}
int main( int argc, char * argv[] )
{
glutInit( & argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "Kwadrat 1" );
glutDisplayFunc( Display );
glutReshapeFunc( Reshape );
glutKeyboardFunc(keyboard);
glutTimerFunc(0, Timer, 0);
glutMainLoop();
return 0;
}
No comments:
Post a Comment