I am making a game where two planes shoot at each other to start with. here is my code so far.
#include <stdlib.h>
#include <freeglut.h>
#include <iostream>
#include "SOIL.h"
#include <math.h>
GLuint texture[8];
const float PI = 3.14159;
float rotate = 0.125f, vertical = 0.0001f, horizontal = 0.0001f, horizontal_one = 0.0001f, vertical_one = 0.0001f, rotate_one = 0.125f, up = 0.0001f, angle = 0.0f, screen = 0.001f, up_two=0.0001f;
int flag[4] = { 0 };
static int animate;
static int animate_two;
bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
{
// AABB 1
float x1Min = x;
float x1Max = x + oWidth;
float y1Max = y + oHeight;
float y1Min = y;
// AABB 2
float x2Min = xTwo;
float x2Max = xTwo + oTwoWidth;
float y2Max = yTwo + oTwoHeight;
float y2Min = yTwo;
// Collision tests
if (x1Max < x2Min || x1Min > x2Max) return false;
if (y1Max < y2Min || y1Min > y2Max) return false;
return true;
}
void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, 1.0f, -1.0f);
}
int LoadGLTextures()
{
texture[0] = SOIL_load_OGL_texture
(
"enemyplane.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
);
if (texture[0] == 0)
return false;
texture[1] = SOIL_load_OGL_texture
(
"enemyplane.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
);
if (texture[1] == 0)
return false;
texture[2] = SOIL_load_OGL_texture
(
"bullet.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
);
if (texture[2] == 0)
return false;
texture[3] = SOIL_load_OGL_texture
(
"coll.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
);
if (texture[3] == 0)
return false;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
return true;
}
void handleSpecialKeypress(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_UP:
if (rotate_one == 0.875f)
{
vertical_one -= 0.25f;
horizontal_one -= 0.25f;
}
if (rotate_one == 0.625f)
{
vertical_one -= 0.25f;
horizontal_one += 0.25f;
}
if (rotate_one == 0.125f)
{
vertical_one += 0.25f;
horizontal_one -= 0.25f;
}
if (rotate_one == 0.375f)
{
vertical_one += 0.25f;
horizontal_one += 0.25f;
}
if (rotate_one == 0.75f)
{
vertical_one -= 0.25f;
}
if (rotate_one == 0.25f)
{
vertical_one += 0.25f;
}
if (rotate_one == 0.5f)
{
horizontal_one += 0.25f;
}
if (rotate_one == 0.0f)
{
horizontal_one -= 0.25f;
}
if (vertical_one >= 9.5f)
{
vertical_one = 9.5f;
}
if (vertical_one <= -9.5f)
{
vertical_one = -9.5f;
}
if (horizontal_one <= -14.5f)
{
horizontal_one = -14.5f;
}
if (horizontal_one >= 4.5f)
{
horizontal_one = 4.5f;
}
break;
case GLUT_KEY_DOWN:
if (rotate_one == 0.875f)
{
vertical_one += 0.25f;
horizontal_one += 0.25f;
}
if (rotate_one == 0.625f)
{
vertical_one += 0.25f;
horizontal_one -= 0.25f;
}
if (rotate_one == 0.375f)
{
vertical_one -= 0.25f;
horizontal_one -= 0.25f;
}
if (rotate_one == 0.125f)
{
vertical_one -= 0.25f;
horizontal_one += 0.25f;
}
if (rotate_one == 0.25f)
{
vertical_one -= 0.25f;
}
if (rotate_one == 0.75f)
{
vertical_one += 0.25f;
}
if (rotate_one == 0.5f)
{
horizontal_one -= 0.25f;
}
if (rotate_one == 0.0f)
{
horizontal_one += 0.25f;
}
if (vertical_one >= 9.5f)
{
vertical_one = 9.5f;
}
if (vertical_one <= -9.5f)
{
vertical_one = -9.5f;
}
if (horizontal_one >= 4.5f)
{
horizontal_one = 4.5f;
}
if (horizontal_one <= -14.5f)
{
horizontal_one = -14.5f;
}
break;
case GLUT_KEY_RIGHT:
rotate_one += 0.125f;
if (rotate_one >= 1.0f)
{
rotate_one = 0.0f;
}
break;
case GLUT_KEY_LEFT:
rotate_one -= 0.125f;
if (rotate_one <= -0.125f)
{
rotate_one = 0.875f;
}
break;
}
glutPostRedisplay();
}
void shoot()
{
up += 0.5f;
if (up >= 15.0f)
{
animate = 0;
up = 0.0f;
glutIdleFunc(NULL);
}
glutPostRedisplay();
}
void shoot_two()
{
up_two += 0.5f;
if (up_two >= 15.0f)
{
animate_two = 0;
up_two = 0.0f;
glutIdleFunc(NULL);
}
glutPostRedisplay();
}
void handleKeypress(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
case 's':
if (animate)
{
glutIdleFunc(shoot);
}
else
{
glutIdleFunc(NULL);
}
break;
case ' ':
if (animate_two)
{
glutIdleFunc(shoot_two);
}
else
{
glutIdleFunc(NULL);
}
break;
case 97:
rotate += 0.125f;
if (rotate >= 1.0f)
{
rotate = 0.0f;
}
break;
case 100:
rotate -= 0.125f;
if (rotate <= -0.125f)
{
rotate = 0.875f;
}
break;
case 119:
if (rotate == 0.875f)
{
vertical -= 0.25f;
horizontal += 0.25f;
}
if (rotate == 0.625f)
{
vertical -= 0.25f;
horizontal -= 0.25f;
}
if (rotate == 0.125f)
{
vertical += 0.25f;
horizontal += 0.25f;
}
if (rotate == 0.375f)
{
vertical += 0.25f;
horizontal -= 0.25f;
}
if (rotate == 0.75f)
{
vertical -= 0.25f;
}
if (rotate == 0.25f)
{
vertical += 0.25f;
}
if (rotate == 0.5f)
{
horizontal -= 0.25f;
}
if (rotate == 0.0f)
{
horizontal += 0.25f;
}
if (vertical >= 9.5f)
{
vertical = 9.5f;
}
if (vertical <= -9.5f)
{
vertical = -9.5f;
}
if (horizontal <= -4.5f)
{
horizontal = -4.5f;
}
if (horizontal >= 14.5f)
{
horizontal = 14.5f;
}
break;
case 120:
if (rotate == 0.875f)
{
vertical += 0.25f;
horizontal -= 0.25f;
}
if (rotate == 0.625f)
{
vertical += 0.25f;
horizontal += 0.25f;
}
if (rotate == 0.375f)
{
vertical -= 0.25f;
horizontal += 0.25f;
}
if (rotate == 0.125f)
{
vertical -= 0.25f;
horizontal -= 0.25f;
}
if (rotate == 0.25f)
{
vertical -= 0.25f;
}
if (rotate == 0.75f)
{
vertical += 0.25f;
}
if (rotate == 0.5f)
{
horizontal += 0.25f;
}
if (rotate == 0.0f)
{
horizontal -= 0.25f;
}
if (vertical >= 9.5f)
{
vertical = 9.5f;
}
if (vertical <= -9.5f)
{
vertical = -9.5f;
}
if (horizontal <= -4.5f)
{
horizontal = -4.5f;
}
if (horizontal >= 14.5f)
{
horizontal = 14.5f;
}
break;
}
glutPostRedisplay();
}
void drawcollision_one()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[3]);
glBegin(GL_POLYGON);
glTexCoord3f(0.0f + screen, 0.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glTexCoord3f(0.167f + screen, 0.0f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glTexCoord3f(0.167f + screen, 1.0f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glTexCoord3f(0.0f + screen, 1.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void timer(int val)
{
screen += 0.1667f;
if (screen >= 1.0f)
{
screen = 1.0f;
}
glutPostRedisplay();
glutTimerFunc(500, timer, 0);
}
void coll_plane_one()
{
//draw bullet
float x = 5.0f+horizontal_one;
float y = 0.0f+vertical_one;
float oWidth = 0.125f;
float oHeight = 0.125f;
//draw plane
float xTwo = -5.0f+horizontal+up_two;
float yTwo = 0.0f+vertical;
float oTwoWidth = 1.0f;
float oTwoHeight = 1.0f;
if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
{
drawcollision_one();
}
}
void drawplane_one()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_POLYGON);
glTexCoord3f(0.0f + rotate, 0.0f, 0.0f);
glVertex3f(-5.5f + horizontal, -0.5f + vertical, 0.0f);
glTexCoord3f(0.125f + rotate, 0.0f, 0.0f);
glVertex3f(-5.5f + horizontal, 0.5f + vertical, 0.0f);
glTexCoord3f(0.125f + rotate, 1.0f, 0.0f);
glVertex3f(-4.5f + horizontal, 0.5f + vertical, 0.0f);
glTexCoord3f(0.0f + rotate, 1.0f, 0.0f);
glVertex3f(-4.5f + horizontal, -0.5f + vertical, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void drawplane_two()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_POLYGON);
glTexCoord3f(0.0f + rotate_one, 0.0f, 0.0f);
glVertex3f(5.5f + horizontal_one, -0.5f + vertical_one, 0.0f);
glTexCoord3f(0.125f + rotate_one, 0.0f, 0.0f);
glVertex3f(5.5f + horizontal_one, 0.5f + vertical_one, 0.0f);
glTexCoord3f(0.125f + rotate_one, 1.0f, 0.0f);
glVertex3f(4.5f + horizontal_one, 0.5f + vertical_one, 0.0f);
glTexCoord3f(0.0f + rotate_one, 1.0f, 0.0f);
glVertex3f(4.5f + horizontal_one, -0.5f + vertical_one, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void drawbullet_one()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glPushMatrix();
if (rotate == 0.25f)
{
angle = PI / 2.0f;
}
if (rotate == 0.75f)
{
angle = (3.0f*PI) / 2.0f;
glTranslatef(0.0f, -1.0f, 0.0f);
}
if (rotate == 0.5f)
{
angle = PI;
glTranslatef(-0.55f, -0.5f, 0.0f);
}
if (rotate == 0.0f)
{
angle = 0.0f;
glTranslatef(0.55f, -0.5f, 0.0f);
}
if (rotate == 0.125f)
{
angle = PI / 4.0f;
glTranslatef(0.35f, -0.15f, 0.0f);
}
if (rotate == 0.375f)
{
angle = (3.0f*PI) / 4.0f;
glTranslatef(-0.35f, -0.15f, 0.0f);
}
if (rotate == 0.625f)
{
angle = (5.0f*PI) / 4.0f;
glTranslatef(-0.35f, -0.75f, 0.0f);
}
if (rotate == 0.875f)
{
angle = (7.0f*PI) / 4.0f;
glTranslatef(0.35f, -0.75f, 0.0f);
}
glTranslatef(0.0f, 0.5f, 0.0f);
float r = sqrt(pow(1.0f, 2) + pow(1.0f, 2));
glBegin(GL_POLYGON);
glTexCoord3f(0.0f, 0.0f, 0.0f);
glVertex3f(-4.9375f+cos(angle)*r*up+horizontal, 0.0625f+sin(angle)*r*up+vertical, 0.0f);
glTexCoord3f(1.0f, 0.0f, 0.0f);
glVertex3f(-5.0625f+cos(angle)*r*up+horizontal, 0.0625f+sin(angle)*r*up+vertical, 0.0f);
glTexCoord3f(1.0f, 1.0f, 0.0f);
glVertex3f(-5.0625f+cos(angle)*r*up+horizontal, -0.0625f+sin(angle)*r*up+vertical, 0.0f);
glTexCoord3f(0.0f, 1.0f, 0.0f);
glVertex3f(-4.9375f+cos(angle)*r*up+horizontal, -0.0625f+sin(angle)*r*up+vertical, 0.0f);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
void drawbullet_two()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glPushMatrix();
if (rotate_one == 0.25f)
{
angle = PI / 2.0f;
}
if (rotate_one == 0.75f)
{
angle = (3.0f*PI) / 2.0f;
glTranslatef(0.0f, -1.0f, 0.0f);
}
if (rotate_one == 0.5f)
{
angle = 0.0f;
glTranslatef(0.55f, -0.5f, 0.0f);
}
if (rotate_one == 0.0f)
{
angle = PI;
glTranslatef(-0.55f, -0.5f, 0.0f);
}
if (rotate_one == 0.125f)
{
angle = (3.0f*PI) / 4.0f;
glTranslatef(-0.35f, -0.15f, 0.0f);
}
if (rotate_one == 0.375f)
{
angle = PI / 4.0f;
glTranslatef(0.35f, -0.15f, 0.0f);
}
if (rotate_one == 0.625f)
{
angle = (7.0f*PI) / 4.0f;
glTranslatef(0.35f, -0.75f, 0.0f);
}
if (rotate_one == 0.875f)
{
angle = (5.0f*PI) / 4.0f;
glTranslatef(-0.35f, -0.75f, 0.0f);
}
glTranslatef(0.0f, 0.5f, 0.0f);
float r = sqrt(pow(1.0f, 2) + pow(1.0f, 2));
glBegin(GL_POLYGON);
glTexCoord3f(0.0f, 0.0f, 0.0f);
glVertex3f(5.0625f + cos(angle)*r*up_two + horizontal_one, 0.0625f + sin(angle)*r*up_two + vertical_one, 0.0f);
glTexCoord3f(1.0f, 0.0f, 0.0f);
glVertex3f(4.9375f + cos(angle)*r*up_two + horizontal_one, 0.0625f + sin(angle)*r*up_two + vertical_one, 0.0f);
glTexCoord3f(1.0f, 1.0f, 0.0f);
glVertex3f(4.9375f + cos(angle)*r*up_two + horizontal_one, -0.0625f + sin(angle)*r*up_two + vertical_one, 0.0f);
glTexCoord3f(0.0f, 1.0f, 0.0f);
glVertex3f(5.0625f + cos(angle)*r*up_two + horizontal_one, -0.0625f + sin(angle)*r*up_two + vertical_one, 0.0f);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glGenTextures(8, &texture[8]);
drawplane_one();
drawplane_two();
drawbullet_one();
drawbullet_two();
coll_plane_one();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(500, 300);
glutInitWindowSize(800, 600);
glutCreateWindow("1945");
glutTimerFunc(500, timer, 0);
glutSpecialFunc(handleSpecialKeypress);
glutKeyboardFunc(handleKeypress);
glutDisplayFunc(display);
SetupRC();
LoadGLTextures();
glutMainLoop();
return 0;
}
My irony sense is tingling.