here is all of my code
#include
<glut.h>
using
namespace std;
GLfloat
angle=0.0f;
GLfloat
up=0.5f;
GLfloat
up_two=0.0f;
GLfloat
ship_01_POSITION_X=0.0f;
GLfloat
ship_01_POSITION_Y=0.0f;
GLfloat
ship_01_POSITION_Z=0.0f;
GLfloat
ship_01_ROTATION=0.0f;
GLfloat
ship_02_POSITION_X=1.5f;
GLfloat
ship_02_POSITION_Y=0.0f;
GLfloat
ship_02_POSITION_Z=0.0f;
GLfloat
ship_02_ROTATION=0.0f;
GLfloat
ship_01_COLOR=1.0f;
GLfloat
ship_02_COLOR=1.0f;
GLfloat
ship_01_bullet_POSITION_X=0.0f;
void
SetupRC()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glOrtho(-10.0f,10.0f,-10.0f,10.0f,-1.0f,1.0f);
}
void
ship_left_two()
{
ship_01_ROTATION+=2.5f;
}
void
ship_right_two()
{
ship_01_ROTATION-=2.5f;
}
void
ship_up_two()
{
ship_01_POSITION_Y+=0.1f;
}
void
ship_down_two()
{
ship_01_POSITION_Y-=0.1f;
}
void
ship_up()
{
ship_02_POSITION_Y+=0.1f;
}
void
ship_down()
{
ship_02_POSITION_Y-=0.1f;
}
void
ship_left()
{
ship_02_ROTATION+=2.5f;
}
void
ship_right()
{
ship_02_ROTATION-=2.5f;
}
void
bullet()
{
up+=0.1f;
if(up >= 10.0f)
{
up=0.5f;
}
}
void
bullet_two()
{
up_two+=0.1f;
if(up_two >= 10.0f)
{
up_two=0.5f;
}
}
void
update(int value)
{
angle+=0.1f;
if(angle > 360.0f)
{
angle-=360.0f;
}
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
void
DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
// glTranslatef(-position_X,-position_Y, -position_Z);
glRotatef(
rotation,0.0f,0.0f,1.0f);
glTranslatef(
position_X, position_Y, position_Z);
glColor3f(
color,0.0f,0.0f);
glBegin(
GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}
void
DrawBullet(GLfloat position_X,GLfloat position_Y,GLfloat position_Z,GLfloat rotation)
{
glPushMatrix();
glColor3f(1.0f,0.0f,0.0f);
glRotatef(
rotation,0.0f,0.0f,1.0f);
glTranslatef(
position_X,position_Y,position_Z);
glPointSize(2.0f);
glBegin(
GL_POINTS);
glVertex3f(0.0f,up++,0.0f);
glEnd();
glPopMatrix();
}
void
Render(void)
{
glClear(
GL_COLOR_BUFFER_BIT);
glPushMatrix();
DrawShip(ship_01_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z,ship_01_ROTATION,ship_01_COLOR);
glPopMatrix();
glPushMatrix();
DrawShip(ship_02_POSITION_X,ship_02_POSITION_Y,ship_02_POSITION_Z,ship_02_ROTATION,ship_02_COLOR);
glPopMatrix();
glPushMatrix();
DrawBullet(ship_01_bullet_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z, ship_01_ROTATION);
glPopMatrix();
glutSwapBuffers();
}
?
?
void
handleKeypress(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 32:
bullet();
break;
case 'w':
ship_up();
break;
case 'x':
ship_down();
break;
case 'a':
ship_left();
break;
case 'd':
ship_right();
break;
case 's':
bullet_two();
break;
}
}
void
mySpecialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_LEFT:
{
ship_left_two();
break;
}
case GLUT_KEY_RIGHT:
{
ship_right_two();
break;
}
case GLUT_KEY_UP:
{
ship_up_two();
break;
}
case GLUT_KEY_DOWN:
{
ship_down_two();
break;
}
}
}
void
main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(
GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(300,100);
glutCreateWindow("Asteroids");
glutDisplayFunc(Render);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(mySpecialKeys);
SetupRC();
glutTimerFunc(25,update,0);
glutMainLoop();
}