1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include "bullet.h"
#include <iostream>
float planetObjX[3] = {-1, 0, 1};
float planetObjY[3] = {2};
float num = 0.0;
float battleShipX = 0;
float battleShipY = 0;
float shipBulletX = 0;
float shipBulletY = 0;
int bulletCount = -1;
bool fire = false;
std::vector<Bullet> shipBullet(100, Bullet(shipBulletX,shipBulletY));
void randNumGenerate()
{
srand(time(NULL));
num = rand()%2/3 ;
}
void initRendering()
{
glEnable(GL_DEPTH_TEST);
}
//this function will keep the ratio of the drawingeven after resizing the window
void handleResize(int w, int h)
{
if (h == 0)
{
h = 1;
}
float aspectRatio = w*1.0/h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, aspectRatio, 1.0, 200.0);
}
void update(int value)
{
/*
necessary updation comes here
*/
planetObjY[0] -= 0.07+num;
planetObjY[1] -= 0.05+num;
planetObjY[2] -= 0.03+num;
if (planetObjY[0] <= -2.0f)
planetObjY[0] = 2.0f;
if (planetObjY[1] <= -2.0f)
planetObjY[1] = 2.0f;
if (planetObjY[2] <= -2.0f)
planetObjY[2] = 2.0f;
if (fire == true)
shipBullet[bulletCount].moveBullet(0.1);
glutPostRedisplay(); //from this glut will be informed that the display has changed
glutTimerFunc(25, update, 0); //value = 0, update will be called for every 25 milliseconds
}
void drawSpaceObject()
{
glPointSize(3.0f);
glBegin(GL_POINTS);
glColor3f(0.9f, 0.7f, 0.8f );
glVertex3f(planetObjX[0],planetObjY[0],-5.0f);
glVertex3f(planetObjX[1],planetObjY[1],-5.0f);
glVertex3f(planetObjX[2],planetObjY[2],-5.0f);
glEnd();
}
void drawBattleShip()
{
glBegin(GL_TRIANGLES);
glVertex3f(battleShipX,battleShipY,-5.0f);
glVertex3f(battleShipX-0.3,battleShipY-0.5,-5.0f);
glVertex3f(battleShipX+0.3,battleShipY-0.5,-5.0f);
glVertex3f(battleShipX+0.2,battleShipY-0.5,-5.0f);
glVertex3f(battleShipX,battleShipY-0.7,-5.0f);
glVertex3f(battleShipX-0.2,battleShipY-0.5,-5.0f);
glEnd();
}
void drawScene()
{
float bullY=0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*
necessary drawings come here
*/
drawSpaceObject();
drawBattleShip();
if (fire == true)
{
shipBullet[bulletCount].drawBullet();
}
glutSwapBuffers();
}
void handleKeyPress(unsigned char key, int x, int y)
{
switch (key)
{
case 'q':
exit(0);
break;
case 'a':
battleShipX -= 0.05;
shipBulletX = battleShipX;
break;
case 'd':
battleShipX += 0.05;
shipBulletX = battleShipX;
break;
case 'f':
fire = true;
bulletCount += 1;
shipBullet.push_back(Bullet());
shipBullet[bulletCount].setBulletPos(battleShipX,battleShipY);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("Star Battle");
initRendering();
randNumGenerate();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeyPress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}
|