Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Bubblasteroids.c
- * Author: Sam Churney
- * Course: CSCI 3161
- * Date: September 24, 2012
- * Purpose: Simple Asteroids Game with OpenGL
- *
- *
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <GLUT/glut.h> // OpenGL Graphics Utility Library
- #include <math.h>
- #define RAD2DEG 180.0/M_PI
- #define DEG2RAD M_PI/180.0
- #define MAXSHIPSPEED 1.5
- #define MAX_PHOTONS 8
- #define drawCircle() glCallList(circle)
- /* -- display list for drawing a circle ------------------------------------- */
- static GLuint circle;
- void buildCircle() {
- GLint i;
- circle = glGenLists(1);
- glNewList(circle, GL_COMPILE);
- glBegin(GL_POLYGON);
- for(i=0; i<40; i++)
- glVertex2d(cos(i*M_PI/1.0), sin(i*M_PI/1.0));
- glEnd();
- glEndList();
- }
- typedef struct {
- int time_to_live;
- double x, y, dx, dy;
- } Photon;
- /*---------- Global Variables --------------*/
- static double angle = 0.0;
- static double ShipRotateIncrement = 10.0;
- static double ShipMoveIncrement = 0.020;
- static double ShipLocX = 0.0, ShipLocY = 0.0;
- static double ShipVelX = 0.0, ShipVelY = 0.0;
- //static float PhotonLocX, PhotonLocY;
- //static float PhotonVelX = 0.5, PhotonVelY = 0.5;
- static double PhotonMoveIncrement = 0.0001;
- static Photon photons[MAX_PHOTONS];
- static int up=0, down=0, left=0, right=0, spacebar=0; /* state of cursor keys */
- int KeyPress;
- void print_photon (Photon* photon) {
- printf("(%d): %f, %f - %f, %f\n", photon->time_to_live, photon->x, photon->y, photon->dx, photon->dy);
- }
- // glutKeyboardFunc is called to handle keyboard input
- void my_key( unsigned char key, int x, int y ) {
- switch(key) {
- case 32:
- spacebar = 1;
- break;
- case 27:
- exit(0); break;
- }
- }
- static void SpecialKeyPress( int key, int x, int y ) {
- switch ( key ) {
- case GLUT_KEY_UP:
- up = 1;
- break;
- case GLUT_KEY_DOWN:
- down = 1;
- break;
- case GLUT_KEY_LEFT:
- left = 1;
- break;
- case GLUT_KEY_RIGHT:
- right = 1;
- break;
- }
- }
- static void SpecialKeyRelease( int key, int x, int y ) {
- switch ( key ) {
- case GLUT_KEY_UP:
- up = 0;
- break;
- case GLUT_KEY_DOWN:
- down = 0;
- break;
- case GLUT_KEY_LEFT:
- left = 0;
- break;
- case GLUT_KEY_RIGHT:
- right = 0;
- break;
- }
- }
- static void KeyRelease( unsigned char key, int x, int y ) {
- switch ( key ) {
- case 32:
- spacebar = 0;
- break;
- default:
- break;
- }
- }
- void init() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
- glMatrixMode(GL_MODELVIEW);
- }
- void drawShip() {
- glPushMatrix();
- glTranslatef(ShipLocX, ShipLocY, 0.0);
- glRotatef(angle, 0.0, 0.0, 1.0);
- glBegin(GL_LINE_LOOP);
- glColor3f(1.0, 1.0, 1.0);
- glVertex3f( 0.0, 0.050, 0.0 );
- glVertex3f( -0.025, -0.050, 0.0 );
- glVertex3f( 0.025, -0.050, 0.0 );
- glEnd();
- glPopMatrix();
- }
- void draw_photon(Photon* photon) {
- glPushMatrix();
- glTranslatef(photon->x, photon->y, 0.0);
- //print_photon(photon);
- glPointSize(3.0);
- glBegin(GL_POINTS);
- glColor3f(1.0, 0.0, 0.0);
- //glVertex3f(photon->x, photon->y, 0.0);
- glVertex3f(0, 0, 0.0);
- glEnd();
- glPopMatrix();
- }
- //myDisplay() handles the animation and redrawing of the scene
- void myDisplay(void) {
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- drawShip();
- glLoadIdentity();
- int i=0;
- for (i=0; i<MAX_PHOTONS; i++) {
- if (photons[i].time_to_live > 0) {
- photons[i].time_to_live--;
- photons[i].x += photons[i].dx;
- photons[i].y += photons[i].dy;
- draw_photon(&photons[i]);
- }
- }
- drawCircle();
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void myTimer(int value) {
- /*
- * timer callback function
- */
- /* advance photon laser shots, eliminating those that have gone past
- the window boundaries */
- /* advance asteroids */
- /* test for and handle collisions */
- if(spacebar==1) {
- int i=0;
- for (i; i<MAX_PHOTONS; i++){
- if(photons[i].time_to_live == 0){
- print_photon(&photons[i]);
- photons[i].time_to_live = 5000;
- photons[i].x = ShipLocX;
- photons[i].y = ShipLocY;
- photons[i].dx = -sin(DEG2RAD * angle) * PhotonMoveIncrement;
- photons[i].dy = cos(DEG2RAD * angle) * PhotonMoveIncrement;
- break;
- }
- }
- }
- //Advance the Ship
- if(up==1) {
- ShipVelX += 0.05;
- ShipVelY += 0.05;
- ShipLocX = ShipLocX + (-sin(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelX);
- ShipLocY = ShipLocY + (cos(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelY);
- }
- //Ship continues to slide when nothing pressed
- if(up == 0 && down ==0) {
- if(ShipVelX >0)
- ShipVelX -=0.05;
- if(ShipVelY > 0)
- ShipVelY -=0.05;
- ShipLocX = ShipLocX + (-sin(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelX);
- ShipLocY = ShipLocY + (cos(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelY);
- }
- //Speed Caps
- if(ShipVelX >= MAXSHIPSPEED)
- ShipVelX = MAXSHIPSPEED;
- if(ShipVelY >= MAXSHIPSPEED)
- ShipVelY = MAXSHIPSPEED;
- if(ShipVelX <= 0)
- ShipVelX = 0;
- if(ShipVelY <= 0)
- ShipVelY = 0;
- //Slows down the ship
- if(down==1) {
- if(ShipVelX >0)
- ShipVelX -= 0.08;
- if(ShipVelY > 0)
- ShipVelY -= 0.08;
- ShipLocX = ShipLocX + (-sin(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelX);
- ShipLocY = ShipLocY + (cos(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelY);
- }
- //Rotate the Ship
- if(right==1) {
- angle -= ShipRotateIncrement;
- }
- if(left==1) {
- angle += ShipRotateIncrement;
- }
- //Ship Wrap Around
- if(ShipLocX > 1.03)
- ShipLocX = -1.03;
- else if(ShipLocX < -1.03)
- ShipLocX = 1.03;
- if(ShipLocY > 1.03)
- ShipLocY = -1.03;
- else if(ShipLocY < -1.03)
- ShipLocY = 1.03;
- glutPostRedisplay();
- glutTimerFunc(33, myTimer, value); /* 30 frames per second */
- }
- void myReshape(int w, int h) {
- GLfloat aspect;
- glViewport(0, 0, w, h);
- aspect = (GLfloat) w/h;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- if (w<=h)
- gluOrtho2D(-1.0, 1.0, -1.0/aspect, 1.0/aspect);
- else
- gluOrtho2D(-aspect, aspect, -1.0, 1.0);
- glMatrixMode(GL_MODELVIEW);
- }
- /* -- main ------------------------------------------------------------------ */
- int main( int argc, char *argv[] )
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowPosition( 400, 400 );
- glutInitWindowSize( 600, 600 );
- glutCreateWindow( "Bubblasteroids" );
- glutDisplayFunc(myDisplay);
- glutIgnoreKeyRepeat(1);
- glutKeyboardFunc( my_key );
- glutSpecialFunc(SpecialKeyPress);
- glutSpecialUpFunc(SpecialKeyRelease);
- glutKeyboardUpFunc(KeyRelease);
- glutReshapeFunc( myReshape );
- glutTimerFunc(33, myTimer ,0);
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- init();
- glutMainLoop();
- return(0);
- }
Add Comment
Please, Sign In to add comment