RedReaper132

Episode 7

May 6th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.22 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. };
  29.  
  30. //start of the program
  31. int main( int argc, char* args[] )
  32. {
  33.   //initialize SDL
  34.   SDL_Init(SDL_INIT_EVERYTHING);
  35.  
  36.   //Set OpenGL memory usage
  37.   SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  38.   SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
  39.   SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  40.   SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
  41.   SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
  42.   SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  43.   SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  44.  
  45.   //Caption of the window
  46.   SDL_WM_SetCaption( "Our first game", NULL );
  47.  
  48.   //Size of the window
  49.   SDL_SetVideoMode(600,400,32, SDL_OPENGL );
  50.  
  51.   //Specific the clear color
  52.   glClearColor(1,1,1,1); //RED,GREEN,BLUE,ALPHA
  53.  
  54.   //What portion of the screen we will display
  55.   glViewport(0,0,600,400);
  56.  
  57.   //Shader model - Use this
  58.   glShadeModel(GL_SMOOTH);
  59.  
  60.   //2D rendering
  61.   glMatrixMode(GL_PROJECTION);
  62.  
  63.   //"Save" it
  64.   glLoadIdentity();
  65.  
  66.   //Disable depth checking
  67.   glDisable(GL_DEPTH_TEST);
  68.  
  69.   std::cout << "OpenGL is running\n";
  70.   std::cout << "Main loop has started\n";
  71.  
  72.   //Handles the main loop
  73.   bool isRunning = true;
  74.  
  75.   //For handling with event
  76.   SDL_Event event;
  77.  
  78.   float myX = 300; //starting x position of rectangle
  79.   float myY = 370; //starting y position of rectangle
  80.   float width = 80; //width of the rectangle
  81.   float height = 20; //height of the rectangle
  82.  
  83.   bool left = false,right = false; //we save in which state the button is  
  84.  
  85.   //The ball variables
  86.   float ballX = 50; //x position
  87.   float ballY = 50; //y position
  88.   float ballWH = 30; //width and height of the ball
  89.  
  90.   float vellX = 0.2; //x speed
  91.   float vellY = 0.2; //y speed
  92.  
  93.   //Brick elements
  94.   const static int BRICKS = 45; //Constant number where we define how many bricks we want
  95.  
  96.   Brick bricks[BRICKS]; //We create an array of bricks
  97.  
  98.   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
  99.     {
  100.       if ( x > 560 ) //If x is near the right edge of the screen
  101.     {
  102.       x = 4; //We start going from the left again
  103.       y += 25; //And move a down a little
  104.     }
  105.       bricks[n].x = x; //Set currents bricks x position
  106.       bricks[n].y = y; //Y position
  107.       bricks[n].width = 60; //Width
  108.       bricks[n].height = 20; //Height
  109.     }
  110.  
  111.  
  112.   //Main game loop
  113.   while ( isRunning )
  114.     {
  115.       //EVENTS
  116.       while ( SDL_PollEvent(&event) )
  117.     {
  118.       //if the window was closed
  119.       if ( event.type == SDL_QUIT )
  120.         {
  121.           isRunning = false;
  122.         }
  123.  
  124.       //If a button was released and the button is escape
  125.       if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
  126.         {
  127.           isRunning = false;
  128.         }
  129.  
  130.       if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r )
  131.         {
  132.           glClearColor(1,0,0,1);
  133.         }
  134.  
  135.       if ( event.type == SDL_KEYDOWN ) //Check for presed down buttons
  136.         {
  137.           if ( event.key.keysym.sym == SDLK_LEFT ) //Check left key
  138.         {
  139.           left = true; //Set the boolean value for left key to true (it is pressed)
  140.         }
  141.  
  142.           else if ( event.key.keysym.sym == SDLK_RIGHT ) //Check Right key
  143.         {
  144.           right = true; //Set the boolean value for right key to true (it is pressed)
  145.         }
  146.            
  147.         }
  148.  
  149.       else if ( event.type == SDL_KEYUP ) //Checking for released buttons
  150.         {
  151.           if ( event.key.keysym.sym == SDLK_LEFT ) //Left key
  152.         {
  153.           left = false; //Set the value to false (key is released)
  154.         }
  155.          
  156.           else if ( event.key.keysym.sym == SDLK_RIGHT ) //Right key
  157.         {
  158.           right = false; //Key is released
  159.         }
  160.         }
  161.  
  162.       //logic that should happen for a certain event
  163.     }
  164.  
  165.       //LOGIC
  166.       if ( left == true ) //If left key is pressed
  167.     {
  168.       myX -= 0.5; //Move left
  169.     }
  170.  
  171.       if ( right == true ) //If right key is pressed
  172.     {
  173.       myX += 0.5; //Move right
  174.     }
  175.  
  176.       if ( myX < 0 ) //If the left border of the pad is over the left part of the screen
  177.     {
  178.       myX = 0; //Put the pad back so it isn't over the border
  179.     }
  180.  
  181.       if ( myX+width > 600 ) //If the pad is over the right border of the screen
  182.     {
  183.       myX = 600-width; //Move it back so it only touches the right border
  184.     }
  185.  
  186.       //The ball logic
  187.       ballX += vellX; //Move the ball on x axis
  188.       ballY += vellY; //move the ball on y axis
  189.  
  190.       if ( ballX < 0 ) //Check if the ball hit the left edge of screen
  191.     {
  192.       vellX = -vellX; //negate the x velocity
  193.     }
  194.  
  195.       else if ( ballX+ballWH>600 )
  196.     {
  197.       vellX = -vellX;
  198.     }
  199.  
  200.       if ( ballY < 0 )
  201.     {
  202.       vellY = -vellY;
  203.     }
  204.  
  205.       else if ( ballY+ballWH > 400 ) //if the ball hit the bottom edge of screen
  206.     {
  207.       isRunning = false; //close game
  208.     }
  209.  
  210.       if ( checkCollision(ballX,ballY,ballWH,ballWH,myX,myY,width,height) == true ) //if there is a collision between the ball and pad
  211.     {
  212.       vellY = -vellY; //change the ball's y velocity/speed
  213.     }
  214.  
  215.  
  216.  
  217.       //RENDERING to the screen
  218.       glClear(GL_COLOR_BUFFER_BIT);
  219.  
  220.       glPushMatrix(); //Start rendering phase
  221.  
  222.       glOrtho(0,600,400,0,-1,1); //Set the matrix
  223.  
  224.       glColor4ub(0,0,0,255); //Black color
  225.  
  226.       glBegin(GL_QUADS); //Start drawing the pad
  227.  
  228.       glVertex2f(myX,myY); //Upper-left corner
  229.       glVertex2f(myX+width,myY); //Upper-right corner
  230.       glVertex2f(myX+width,myY+height); //Down-right corner
  231.       glVertex2f(myX,myY+height); //Down-left corner
  232.  
  233.       glEnd(); //End drawing
  234.  
  235.       glColor4ub(255,0,0,255); //Red color
  236.  
  237.       glBegin(GL_QUADS); //Render of the ball, same method as for the pad
  238.       glVertex2f(ballX,ballY);
  239.       glVertex2f(ballX+ballWH,ballY);
  240.       glVertex2f(ballX+ballWH,ballY+ballWH);
  241.       glVertex2f(ballX,ballY+ballWH);
  242.       glEnd();
  243.  
  244.       glColor4ub(0,0,255,255); //Blue color
  245.  
  246.       glBegin(GL_QUADS); //Render the bricks
  247.  
  248.       for ( int n = 0; n < BRICKS; n++ ) //We go throught all the elements (bricks)
  249.     {
  250.       if ( n%2 == 0 ) glColor4ub(0,255,255,255); //Every 2nd bricks has a different color
  251.       else glColor4ub(0,0,255,255); //Else it has normal blue color
  252.  
  253.       //The 4 edges of the current brick
  254.       glVertex2f(bricks[n].x,bricks[n].y);
  255.       glVertex2f(bricks[n].x+bricks[n].width,bricks[n].y);
  256.       glVertex2f(bricks[n].x+bricks[n].width,bricks[n].y+bricks[n].height);
  257.       glVertex2f(bricks[n].x,bricks[n].y+bricks[n].height);
  258.     }
  259.  
  260.       glEnd(); //End drawing
  261.  
  262.       glPopMatrix(); //End rendering phase
  263.  
  264.       SDL_GL_SwapBuffers();
  265.  
  266.       SDL_Delay(1); //Delay / pause
  267.     }
  268.  
  269.   SDL_Quit();
  270.  
  271.   return 0;
  272. }
Add Comment
Please, Sign In to add comment