Advertisement
RedReaper132

Episode 9

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