/*wersja na windows 7 */
#include <fstream>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
/*
C:\TDM-GCC-32>g++ -o test.exe test.cpp -lopengl32 -lglut32 -lglu32
C:\TDM-GCC-32>test
sprawdz instalacje glut for windows
*/
GLfloat d = 0.1;
int refreshMillis = 30;
struct kulka
{
GLfloat x;
GLfloat y ;
unsigned char key;
} kulka={0.0, -0.5} , wall= {1.0 , 1.0} , speed= {0.03, 0.04}, key={0.0, -0.7}, Tfrom;
struct point
{
GLfloat x;
GLfloat y;
} point[20];
GLuint list[8];
void CreateList(void)
{
int i =0;
{
i++;
list[1] = glGenLists (1);
glNewList(list[1], GL_COMPILE);
{
glColor3f(1.0,1.0,0.0);
glutSolidSphere(d,6,6);
glFlush ();
}
glEndList();
}
list[2] = glGenLists (2);
glNewList(list[2], GL_COMPILE);
{
glColor3f(1.0,1.0,0.0);
glutSolidSphere(d,4,4);
glFlush ();
}
glEndList();
list[3] = glGenLists (3);
glNewList(list[3], GL_COMPILE);
{
glColor3f(1.0,1.0,.0);
glutSolidSphere(d,4,4);
glFlush ();
}
glEndList();
list[4] = glGenLists (4);
glNewList(list[4], GL_COMPILE);
{
glColor3f(1.0,1.0,.0);
glutSolidSphere(d,4,4);
glFlush ();
}
glEndList();
list[5] = glGenLists (5);
glNewList(list[5], GL_COMPILE);
{
glColor3f(1.0,1.0,.0);
glutSolidSphere(d,4,4);
glFlush ();
}
glEndList();
list[6] = glGenLists (6);
glNewList(list[6], GL_COMPILE);
{
glColor3f(1.0,1.0,.0);
glutSolidSphere(d,4,4);
glFlush ();
}
glEndList();
list[7] = glGenLists (7);
glNewList(list[7], GL_COMPILE);
{
glColor3f(1.0,1.0,.0);
glutSolidSphere(d,4,4);
glFlush ();
}
glEndList();
list[8] = glGenLists (8);
glNewList(list[8], GL_COMPILE);
{
glColor3f(1.0,1.0,.0);
glutSolidSphere(d,4,4);
glFlush ();
}
glEndList();
}
GLfloat x2 , y2=-0.7;
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 ) //core dumpted p.
{
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<-wall.y)
{
speed.y=-speed.y;
}
if(( kulka.y-y2<d ) && (kulka.x-x2<d )&& (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();
int i=0;
point[0].x=-0.7;
point[1].x=-0.5;
point[2].x=-0.3;
point[3].x=-0.1;
point[4].x=0.1;
point[5].x=0.3;
point[6].x=0.5;
point[7].x=0.7;
point[8].x=0.9;
glPushMatrix();
glTranslatef(point[0].x, 0.7,0.0);
glCallList(list[1]);
glPopMatrix();
glPushMatrix();
glTranslatef(point[1].x, 0.7,0.0);
glCallList(list[2]);
glPopMatrix();
glPushMatrix();
glTranslatef(point[2].x, 0.7,0.0);
glCallList(list[3]);
glPopMatrix();
glPushMatrix();
glTranslatef(point[3].x, 0.7,0.0);
glCallList(list[4]);
glPopMatrix();
glPushMatrix();
glTranslatef(point[4].x, 0.7,0.0);
glCallList(list[5]);
glPopMatrix();
glPushMatrix();
glTranslatef(point[5].x, 0.7,0.0);
glCallList(list[6]);
glPopMatrix();
glPushMatrix();
glTranslatef(point[6].x, 0.7,0.0);
glCallList(list[7]);
glPopMatrix();
glPushMatrix();
glTranslatef(point[7].x, 0.7,0.0);
glCallList(list[8]);
glPopMatrix();
if ((0.7-kulka.y<d) && (kulka.x-point[0].x<d )&& (point[0].x-kulka.x<d) )
{
glDeleteLists(list[1], 1);
}
if ((0.7-kulka.y<d) && (kulka.x-point[1].x<d )&& (point[1].x-kulka.x<d) )
{
glDeleteLists(list[2], 1);
}
if ((0.7-kulka.y<d) && (kulka.x-point[2].x<d )&& (point[2].x-kulka.x<d) )
{
glDeleteLists(list[3], 1);
}
if ((0.7-kulka.y<d) && (kulka.x-point[3].x<d )&& (point[3].x-kulka.x<d) )
{
glDeleteLists(list[4], 1);
}
if ((0.7-kulka.y<d) && (kulka.x-point[4].x<d )&& (point[4].x-kulka.x<d) )
{
glDeleteLists(list[5], 1);
}
if ((0.7-kulka.y<d) && (kulka.x-point[5].x<d )&& (point[5].x-kulka.x<d) )
{
glDeleteLists(list[6], 1);
}
if ((0.7-kulka.y<d) && (kulka.x-point[6].x<d )&& (point[6].x-kulka.x<d) )
{
glDeleteLists(list[7], 1);
}
if ((0.7-kulka.y<d) && (kulka.x-point[7].x<d )&& (point[7].x-kulka.x<d) )
{
glDeleteLists(list[8], 1);
}
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
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( "colision" );
CreateList();
glutDisplayFunc( Display );
glutReshapeFunc( reshape );
glutKeyboardFunc(keyboard);
glutTimerFunc(0, Timer, 0);
glutMainLoop();
return 0;
}
No comments:
Post a Comment