Guest User

Untitled

a guest
Jan 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.65 KB | None | 0 0
  1. /*
  2.  * Bubblasteroids.c
  3.  * Author: Sam Churney
  4.  * Course: CSCI 3161
  5.  * Date: September 24, 2012
  6.  * Purpose: Simple Asteroids Game with OpenGL
  7.  *
  8.  *
  9.  */
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <GLUT/glut.h>  // OpenGL Graphics Utility Library
  14. #include <math.h>
  15.  
  16. #define RAD2DEG 180.0/M_PI
  17. #define DEG2RAD M_PI/180.0
  18. #define MAXSHIPSPEED 1.5
  19. #define MAX_PHOTONS     8
  20. #define drawCircle() glCallList(circle)
  21.  
  22.  
  23. /* -- display list for drawing a circle ------------------------------------- */
  24.  
  25. static GLuint   circle;
  26.  
  27. void buildCircle() {
  28.     GLint   i;
  29.  
  30.     circle = glGenLists(1);
  31.     glNewList(circle, GL_COMPILE);
  32.     glBegin(GL_POLYGON);
  33.     for(i=0; i<40; i++)
  34.         glVertex2d(cos(i*M_PI/1.0), sin(i*M_PI/1.0));
  35.     glEnd();
  36.     glEndList();
  37. }
  38.  
  39.  
  40. typedef struct {
  41.     int time_to_live;
  42.     double  x, y, dx, dy;
  43. } Photon;
  44.  
  45.  
  46. /*---------- Global Variables --------------*/
  47.  
  48. static double angle = 0.0;
  49. static double ShipRotateIncrement = 10.0;
  50. static double ShipMoveIncrement = 0.020;
  51. static double ShipLocX = 0.0, ShipLocY = 0.0;
  52. static double ShipVelX = 0.0, ShipVelY = 0.0;
  53. //static float PhotonLocX, PhotonLocY;
  54. //static float PhotonVelX = 0.5, PhotonVelY = 0.5;
  55. static double PhotonMoveIncrement = 0.0001;
  56. static Photon   photons[MAX_PHOTONS];
  57.  
  58. static int      up=0, down=0, left=0, right=0, spacebar=0;      /* state of cursor keys */
  59. int KeyPress;
  60.  
  61. void print_photon (Photon* photon) {
  62.     printf("(%d): %f, %f - %f, %f\n", photon->time_to_live, photon->x, photon->y, photon->dx, photon->dy);
  63. }
  64.  
  65. // glutKeyboardFunc is called to handle keyboard input
  66. void my_key( unsigned char key, int x, int y ) {
  67.     switch(key) {
  68.         case 32:
  69.             spacebar = 1;
  70.             break;
  71.         case 27:
  72.             exit(0); break;
  73.     }
  74. }
  75.  
  76.  
  77. static void SpecialKeyPress( int key, int x, int y ) {
  78.     switch ( key ) {
  79.         case GLUT_KEY_UP:
  80.             up = 1;
  81.             break;
  82.         case GLUT_KEY_DOWN:
  83.             down = 1;
  84.             break;
  85.         case GLUT_KEY_LEFT:
  86.             left = 1;
  87.             break;
  88.         case GLUT_KEY_RIGHT:
  89.             right = 1;
  90.             break;
  91.     }
  92. }
  93.  
  94. static void SpecialKeyRelease( int key, int x, int y ) {
  95.     switch ( key ) {
  96.         case GLUT_KEY_UP:
  97.             up = 0;
  98.             break;
  99.         case GLUT_KEY_DOWN:
  100.             down = 0;
  101.             break;
  102.         case GLUT_KEY_LEFT:
  103.             left = 0;
  104.             break;
  105.         case GLUT_KEY_RIGHT:
  106.             right = 0;
  107.             break;
  108.     }
  109. }
  110.  
  111. static void KeyRelease( unsigned char key, int x, int y ) {
  112.     switch ( key ) {
  113.         case 32:
  114.             spacebar = 0;
  115.             break;
  116.         default:
  117.             break;
  118.     }
  119. }
  120.  
  121. void init() {
  122.     glMatrixMode(GL_PROJECTION);
  123.     glLoadIdentity();
  124.     gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
  125.     glMatrixMode(GL_MODELVIEW);
  126. }
  127.  
  128.  
  129. void drawShip() {
  130.     glPushMatrix();
  131.     glTranslatef(ShipLocX, ShipLocY, 0.0);
  132.     glRotatef(angle, 0.0, 0.0, 1.0);
  133.     glBegin(GL_LINE_LOOP);
  134.     glColor3f(1.0, 1.0, 1.0);
  135.     glVertex3f( 0.0, 0.050, 0.0 );
  136.     glVertex3f( -0.025, -0.050, 0.0 );
  137.     glVertex3f( 0.025, -0.050, 0.0 );
  138.     glEnd();
  139.     glPopMatrix();
  140. }
  141.  
  142. void draw_photon(Photon* photon) {
  143.     glPushMatrix();
  144.     glTranslatef(photon->x, photon->y, 0.0);
  145.     //print_photon(photon);
  146.     glPointSize(3.0);
  147.     glBegin(GL_POINTS);
  148.     glColor3f(1.0, 0.0, 0.0);
  149.     //glVertex3f(photon->x, photon->y, 0.0);
  150.     glVertex3f(0, 0, 0.0);
  151.     glEnd();
  152.     glPopMatrix();
  153. }
  154.  
  155.  
  156. //myDisplay() handles the animation and redrawing of the scene
  157. void myDisplay(void) {
  158.     glClear(GL_COLOR_BUFFER_BIT);
  159.     glLoadIdentity();
  160.     drawShip();
  161.     glLoadIdentity();
  162.  
  163.     int i=0;
  164.     for (i=0; i<MAX_PHOTONS; i++) {
  165.         if (photons[i].time_to_live > 0) {
  166.             photons[i].time_to_live--;
  167.             photons[i].x += photons[i].dx;
  168.             photons[i].y += photons[i].dy;
  169.             draw_photon(&photons[i]);
  170.         }
  171.     }
  172.  
  173.     drawCircle();
  174.  
  175.     glutSwapBuffers();
  176.     glutPostRedisplay();
  177. }
  178.  
  179.  
  180. void myTimer(int value) {
  181.     /*
  182.      *  timer callback function
  183.      */
  184.  
  185.     /* advance photon laser shots, eliminating those that have gone past
  186.        the window boundaries */
  187.  
  188.     /* advance asteroids */
  189.  
  190.     /* test for and handle collisions */
  191.  
  192.     if(spacebar==1) {
  193.         int i=0;
  194.         for (i; i<MAX_PHOTONS; i++){
  195.             if(photons[i].time_to_live == 0){
  196.                 print_photon(&photons[i]);
  197.                 photons[i].time_to_live = 5000;
  198.                 photons[i].x = ShipLocX;
  199.                 photons[i].y = ShipLocY;
  200.                 photons[i].dx = -sin(DEG2RAD * angle) * PhotonMoveIncrement;
  201.                 photons[i].dy = cos(DEG2RAD * angle) * PhotonMoveIncrement;
  202.                 break;
  203.             }
  204.         }
  205.  
  206.     }
  207.  
  208.  
  209.     //Advance the Ship
  210.     if(up==1) {
  211.         ShipVelX += 0.05;
  212.         ShipVelY += 0.05;
  213.         ShipLocX = ShipLocX + (-sin(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelX);
  214.         ShipLocY = ShipLocY + (cos(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelY);
  215.     }
  216.  
  217.     //Ship continues to slide when nothing pressed
  218.     if(up == 0 && down ==0) {
  219.         if(ShipVelX >0)
  220.             ShipVelX -=0.05;
  221.         if(ShipVelY > 0)
  222.             ShipVelY -=0.05;
  223.  
  224.         ShipLocX = ShipLocX + (-sin(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelX);
  225.         ShipLocY = ShipLocY + (cos(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelY);
  226.     }
  227.  
  228.     //Speed Caps
  229.     if(ShipVelX >= MAXSHIPSPEED)
  230.         ShipVelX = MAXSHIPSPEED;
  231.     if(ShipVelY >= MAXSHIPSPEED)
  232.         ShipVelY = MAXSHIPSPEED;
  233.  
  234.     if(ShipVelX <= 0)
  235.         ShipVelX = 0;
  236.     if(ShipVelY <= 0)
  237.         ShipVelY = 0;
  238.  
  239.     //Slows down the ship
  240.     if(down==1) {
  241.         if(ShipVelX >0)
  242.             ShipVelX -= 0.08;
  243.         if(ShipVelY > 0)
  244.             ShipVelY -= 0.08;
  245.         ShipLocX = ShipLocX + (-sin(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelX);
  246.         ShipLocY = ShipLocY + (cos(DEG2RAD*angle))*(ShipMoveIncrement*ShipVelY);
  247.     }
  248.  
  249.     //Rotate the Ship
  250.     if(right==1) {
  251.         angle -= ShipRotateIncrement;
  252.     }
  253.  
  254.     if(left==1) {
  255.         angle += ShipRotateIncrement;
  256.     }
  257.  
  258.  
  259.     //Ship Wrap Around
  260.     if(ShipLocX > 1.03)
  261.         ShipLocX = -1.03;
  262.     else if(ShipLocX < -1.03)
  263.         ShipLocX = 1.03;
  264.  
  265.     if(ShipLocY > 1.03)
  266.         ShipLocY = -1.03;
  267.     else if(ShipLocY < -1.03)
  268.         ShipLocY = 1.03;
  269.  
  270.  
  271.  
  272.  
  273.     glutPostRedisplay();
  274.  
  275.     glutTimerFunc(33, myTimer, value);          /* 30 frames per second */
  276.  
  277. }
  278.  
  279. void myReshape(int w, int h) {
  280.     GLfloat aspect;
  281.  
  282.     glViewport(0, 0, w, h);
  283.     aspect = (GLfloat) w/h;
  284.     glMatrixMode(GL_PROJECTION);
  285.     glLoadIdentity();
  286.     if (w<=h)
  287.         gluOrtho2D(-1.0, 1.0, -1.0/aspect, 1.0/aspect);
  288.     else
  289.         gluOrtho2D(-aspect, aspect, -1.0, 1.0);
  290.     glMatrixMode(GL_MODELVIEW);
  291. }
  292.  
  293.  
  294. /* -- main ------------------------------------------------------------------ */
  295.  
  296. int main( int argc, char *argv[] )
  297. {
  298.     glutInit(&argc,argv);
  299.  
  300.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  301.  
  302.     glutInitWindowPosition( 400, 400 );
  303.     glutInitWindowSize( 600, 600 );
  304.     glutCreateWindow( "Bubblasteroids" );
  305.  
  306.     glutDisplayFunc(myDisplay);
  307.  
  308.     glutIgnoreKeyRepeat(1);
  309.     glutKeyboardFunc( my_key );
  310.     glutSpecialFunc(SpecialKeyPress);
  311.     glutSpecialUpFunc(SpecialKeyRelease);
  312.     glutKeyboardUpFunc(KeyRelease);
  313.  
  314.     glutReshapeFunc( myReshape );
  315.     glutTimerFunc(33, myTimer ,0);
  316.     glClearColor(0.0, 0.0, 0.0, 1.0);
  317.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  318.  
  319.     init();
  320.  
  321.     glutMainLoop();
  322.     return(0);
  323. }
Add Comment
Please, Sign In to add comment