Advertisement
RedReaper132

Episode 8

May 6th, 2011
3,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.17 KB | None | 0 0
  1. /*
  2. Compile command:
  3. g++ -o test main.cpp -lSDLmain -lSDL -lGL
  4.  */
  5.  
  6. // specific headers
  7. #include "SDL/SDL.h"
  8. #include "SDL/SDL_opengl.h"
  9. #include <iostream>
  10.  
  11. bool checkCollision(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh) //Funcfion for checking collision
  12. {
  13.   if ( Ay+Ah < By ) return false; //if A is more to the left than B
  14.   else if ( Ay > By+Bh ) return false; //if A is more to the right than B
  15.   else if ( Ax+Aw < Bx ) return false; //if A is higher than B
  16.   else if ( Ax > Bx+Bw ) return false; //if A is lower than B
  17.  
  18.   return true; //There is a collision because none of above returned false
  19. }
  20.  
  21. struct Brick //Brick structures which holds 4 elements
  22. {
  23.   //Elements are used for x and y position of the brick and its width and height
  24.   float x;
  25.   float y;
  26.   float width;
  27.   float height;
  28.   bool alive; //Alive variable. We use to to store in which state the brick is (broken, not broken)
  29. };
  30.  
  31. //start of the program
  32. int main( int argc, char* args[] )
  33. {
  34.   //initialize SDL
  35.   SDL_Init(SDL_INIT_EVERYTHING);
  36.  
  37.   //Set OpenGL memory usage
  38.   SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  39.   SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
  40.   SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  41.   SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
  42.   SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
  43.   SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  44.   SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  45.  
  46.   //Caption of the window
  47.   SDL_WM_SetCaption( "Our first game", NULL );
  48.  
  49.   //Size of the window
  50.   SDL_SetVideoMode(600,400,32, SDL_OPENGL );
  51.  
  52.   //Specific the clear color
  53.   glClearColor(1,1,1,1); //RED,GREEN,BLUE,ALPHA
  54.  
  55.   //What portion of the screen we will display
  56.   glViewport(0,0,600,400);
  57.  
  58.   //Shader model - Use this
  59.   glShadeModel(GL_SMOOTH);
  60.  
  61.   //2D rendering
  62.   glMatrixMode(GL_PROJECTION);
  63.  
  64.   //"Save" it
  65.   glLoadIdentity();
  66.  
  67.   //Disable depth checking
  68.   glDisable(GL_DEPTH_TEST);
  69.  
  70.   std::cout << "OpenGL is running\n";
  71.   std::cout << "Main loop has started\n";
  72.  
  73.   //Handles the main loop
  74.   bool isRunning = true;
  75.  
  76.   //For handling with event
  77.   SDL_Event event;
  78.  
  79.   float myX = 300; //starting x position of rectangle
  80.   float myY = 370; //starting y position of rectangle
  81.   float width = 80; //width of the rectangle
  82.   float height = 20; //height of the rectangle
  83.  
  84.   bool left = false,right = false; //we save in which state the button is  
  85.  
  86.   //The ball variables
  87.   float ballX = 50; //x position
  88.   float ballY = 350; //y position
  89.   float ballWH = 30; //width and height of the ball
  90.  
  91.   float vellX = 0.2; //x speed
  92.   float vellY = -0.2; //y speed
  93.  
  94.   //Brick elements
  95.   const static int BRICKS = 45; //Constant number where we define how many bricks we want
  96.  
  97.   Brick bricks[BRICKS]; //We create an array of bricks
  98.  
  99.   for ( int n = 0, x = 4, y = 10; n < BRICKS; n++, x+=66 ) //A for loop that goes throught the array so we can set the positions
  100.     {
  101.       if ( x > 560 ) //If x is near the right edge of the screen
  102.     {
  103.       x = 4; //We start going from the left again
  104.       y += 25; //And move a down a little
  105.     }
  106.       bricks[n].x = x; //Set currents bricks x position
  107.       bricks[n].y = y; //Y position
  108.       bricks[n].width = 60; //Width
  109.       bricks[n].height = 20; //Height
  110.       bricks[n].alive = true; //Set alive to true (not broken)
  111.     }
  112.  
  113.  
  114.   //Main game loop
  115.   while ( isRunning )
  116.     {
  117.       //EVENTS
  118.       while ( SDL_PollEvent(&event) )
  119.     {
  120.       //if the window was closed
  121.       if ( event.type == SDL_QUIT )
  122.         {
  123.           isRunning = false;
  124.         }
  125.  
  126.       //If a button was released and the button is escape
  127.       if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
  128.         {
  129.           isRunning = false;
  130.         }
  131.  
  132.       if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r )
  133.         {
  134.           glClearColor(1,0,0,1);
  135.         }
  136.  
  137.       if ( event.type == SDL_KEYDOWN ) //Check for presed down buttons
  138.         {
  139.           if ( event.key.keysym.sym == SDLK_LEFT ) //Check left key
  140.         {
  141.           left = true; //Set the boolean value for left key to true (it is pressed)
  142.         }
  143.  
  144.           else if ( event.key.keysym.sym == SDLK_RIGHT ) //Check Right key
  145.         {
  146.           right = true; //Set the boolean value for right key to true (it is pressed)
  147.         }
  148.            
  149.         }
  150.  
  151.       else if ( event.type == SDL_KEYUP ) //Checking for released buttons
  152.         {
  153.           if ( event.key.keysym.sym == SDLK_LEFT ) //Left key
  154.         {
  155.           left = false; //Set the value to false (key is released)
  156.         }
  157.          
  158.           else if ( event.key.keysym.sym == SDLK_RIGHT ) //Right key
  159.         {
  160.           right = false; //Key is released
  161.         }
  162.         }
  163.  
  164.       //logic that should happen for a certain event
  165.     }
  166.  
  167.       //LOGIC
  168.       if ( left == true ) //If left key is pressed
  169.     {
  170.       myX -= 0.5; //Move left
  171.     }
  172.  
  173.       if ( right == true ) //If right key is pressed
  174.     {
  175.       myX += 0.5; //Move right
  176.     }
  177.  
  178.       if ( myX < 0 ) //If the left border of the pad is over the left part of the screen
  179.     {
  180.       myX = 0; //Put the pad back so it isn't over the border
  181.     }
  182.  
  183.       if ( myX+width > 600 ) //If the pad is over the right border of the screen
  184.     {
  185.       myX = 600-width; //Move it back so it only touches the right border
  186.     }
  187.  
  188.       //The ball logic
  189.       ballX += vellX; //Move the ball on x axis first
  190.  
  191.       for ( int n = 0; n < BRICKS; n++ ) //Go throught the array of bricks
  192.     {
  193.       if ( bricks[n].alive == true ) //If the bricks is alive
  194.         {
  195.           if ( checkCollision(ballX,ballY,ballWH,ballWH,bricks[n].x,bricks[n].y,bricks[n].width, bricks[n].height) == true ) //Check for collision with the ball
  196.         {
  197.           vellX = -vellX; //Change x velocity of the ball
  198.           bricks[n].alive = false; //Set the alive variable to false (brick is broken)
  199.           break; //Stop checking for collision on x axis
  200.         }
  201.         }
  202.     }
  203.  
  204.       ballY += vellY; //move the ball on y axis second
  205.  
  206.       for ( int n = 0; n < BRICKS; n++ ) //Go throught the array of bricks
  207.     {
  208.       if ( bricks[n].alive == true ) //If the brick is alive
  209.         {
  210.           if ( checkCollision(ballX,ballY,ballWH,ballWH,bricks[n].x,bricks[n].y,bricks[n].width, bricks[n].height) == true ) //Check for collision
  211.         {
  212.           vellY = -vellY; //Change y velocity of the ball
  213.           bricks[n].alive = false; //Set alive varible to false
  214.           break; //Stop checking for collision on y axis
  215.         }
  216.         }
  217.     }
  218.  
  219.       if ( ballX < 0 ) //Check if the ball hit the left edge of screen
  220.     {
  221.       vellX = -vellX; //negate the x velocity
  222.     }
  223.  
  224.       else if ( ballX+ballWH>600 )
  225.     {
  226.       vellX = -vellX;
  227.     }
  228.  
  229.       if ( ballY < 0 )
  230.     {
  231.       vellY = -vellY;
  232.     }
  233.  
  234.       else if ( ballY+ballWH > 400 ) //if the ball hit the bottom edge of screen
  235.     {
  236.       //We set everything back as it was on the start, so we get a restart game impresion
  237.  
  238.       myX = 300; //starting x position of rectangle
  239.       myY = 370; //starting y position of rectangle
  240.       width = 80; //width of the rectangle
  241.       height = 20; //height of the rectangle
  242.      
  243.       left = false,right = false; //Set the buttons back to false  
  244.      
  245.       //The ball variables
  246.       ballX = 50; //x position
  247.       ballY = 350; //y position
  248.       ballWH = 30; //width and height of the ball
  249.      
  250.       vellX = 0.2; //x speed
  251.       vellY = -0.2; //y speed
  252.  
  253.       for ( int n = 0; n < BRICKS; n++ ) //A for loop that goes throught the array so we can set every bricks alive to true (not broken)
  254.         {
  255.           bricks[n].alive = true;
  256.         }
  257.     }
  258.  
  259.       if ( checkCollision(ballX,ballY,ballWH,ballWH,myX,myY,width,height) == true ) //if there is a collision between the ball and pad
  260.     {
  261.       vellY = -vellY; //change the ball's y velocity/speed
  262.     }
  263.  
  264.  
  265.  
  266.       //RENDERING to the screen
  267.       glClear(GL_COLOR_BUFFER_BIT);
  268.  
  269.       glPushMatrix(); //Start rendering phase
  270.  
  271.       glOrtho(0,600,400,0,-1,1); //Set the matrix
  272.  
  273.       glColor4ub(0,0,0,255); //Black color
  274.  
  275.       glBegin(GL_QUADS); //Start drawing the pad
  276.  
  277.       glVertex2f(myX,myY); //Upper-left corner
  278.       glVertex2f(myX+width,myY); //Upper-right corner
  279.       glVertex2f(myX+width,myY+height); //Down-right corner
  280.       glVertex2f(myX,myY+height); //Down-left corner
  281.  
  282.       glEnd(); //End drawing
  283.  
  284.       glColor4ub(255,0,0,255); //Red color
  285.  
  286.       glBegin(GL_QUADS); //Render of the ball, same method as for the pad
  287.       glVertex2f(ballX,ballY);
  288.       glVertex2f(ballX+ballWH,ballY);
  289.       glVertex2f(ballX+ballWH,ballY+ballWH);
  290.       glVertex2f(ballX,ballY+ballWH);
  291.       glEnd();
  292.  
  293.       glColor4ub(0,0,255,255); //Blue color
  294.  
  295.       glBegin(GL_QUADS); //Render the bricks
  296.  
  297.       for ( int n = 0; n < BRICKS; n++ ) //We go throught all the elements (bricks)
  298.     {
  299.       if ( bricks[n].alive == true ) //We only draw the brick if it's alive
  300.         {
  301.           if ( n%2 == 0 ) glColor4ub(122,0,255,255); //Every 2nd bricks has a different color
  302.           else glColor4ub(0,0,255,255); //Else it has normal blue color
  303.          
  304.           //The 4 edges of the current brick
  305.           glVertex2f(bricks[n].x,bricks[n].y);
  306.           glVertex2f(bricks[n].x+bricks[n].width,bricks[n].y);
  307.           glVertex2f(bricks[n].x+bricks[n].width,bricks[n].y+bricks[n].height);
  308.           glVertex2f(bricks[n].x,bricks[n].y+bricks[n].height);
  309.         }
  310.     }
  311.  
  312.       glEnd(); //End drawing
  313.  
  314.       glPopMatrix(); //End rendering phase
  315.  
  316.       SDL_GL_SwapBuffers();
  317.  
  318.       SDL_Delay(1); //Delay / pause
  319.     }
  320.  
  321.   SDL_Quit();
  322.  
  323.   return 0;
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement