well I am able to draw and move the plane and shoot bullets and move the enemy plane from the top to the bottom of the screen. here is my code so far.
#include <freeglut.h>
#include <iostream>
#include<SOIL.h>
using namespace std;
GLuint textureBrick[8];
float move_plane = 0.0f;
float up = 0.0f;
float down = 0.0f;
GLuint loadTex(const char* texname)
{
/* load an image file directly as a new OpenGL texture */
GLuint texture = SOIL_load_OGL_texture
(
texname,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
);
return texture;
}
void init()
{
textureBrick[0] = loadTex("C:\\Users\\Owner\\Desktop\\plane.png");
textureBrick[1] = loadTex("C:\\Users\\Owner\\Desktop\\bullet.png");
textureBrick[2] = loadTex("C:\\Users\\Owner\\Desktop\\enemy.png");
}
void timer(int)
{
down--;
if (down <= -160.0f)
{
down = 0.0f;
}
glutPostRedisplay();
glutTimerFunc(1000.0 / 60.0, timer, 0);
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureBrick[2]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-10.0f, 100.0f + down);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(10.0f, 100.0f + down);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(10.0f, 80.0f + down);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-10.0f, 80.0f + down);
glEnd();
glBindTexture(GL_TEXTURE_2D, textureBrick[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-10.0f+move_plane, -80.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(10.0f+move_plane, -80.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(10.0f+move_plane, -100.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-10.0f+move_plane, -100.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, textureBrick[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-2.5f+move_plane, -75.0f+up);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(2.5f+move_plane, -75.0f+up);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(2.5f+move_plane, -80.0f+up);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-2.5f+move_plane, -80.0f+up);
glEnd();
glPopMatrix();
glutSwapBuffers();
glDisable(GL_TEXTURE_2D);
}
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat aspectRatio;
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
else
glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void shoot()
{
up++;
if (up >= 175.0f)
{
up = 0.0f;
glutIdleFunc(NULL);
}
glutPostRedisplay();
}
void handleKeypress(unsigned char key, int x, int y)
{
int animate = 0;
switch(key)
{
case 27:
exit(0);
break;
case 32:
animate = !animate;
if (animate)
glutIdleFunc(shoot);
else
glutIdleFunc(NULL);
break;
}
glutPostRedisplay();
}
void handleSpecialKeypress(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
move_plane--;
if (move_plane <= -125.0f)
{
move_plane = -125.0f;
}
break;
case GLUT_KEY_RIGHT:
move_plane++;
if (move_plane >= 125.0f)
{
move_plane = 125.0f;
}
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(400, 300);
glutInitWindowSize(800, 600);
glutCreateWindow("Combat");
glutDisplayFunc(renderScene);
glutTimerFunc(1000.0 / 60.0, timer, 0);
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(handleSpecialKeypress);
init();
glutMainLoop();
return 0;
}