Advertisement
RedReaper132

Episode 10

Jun 6th, 2011
2,427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.88 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 the textures
  148.   unsigned int pad_texture = 0;
  149.   unsigned int ball_texture = 0;
  150.   unsigned int brick_texture = 0;
  151.   unsigned int back_texture = 0;
  152.   //Load the image into the textures using the function
  153.   pad_texture = loadTexture("pad.png");
  154.   ball_texture = loadTexture("ball.png");
  155.   brick_texture = loadTexture("brick.png");
  156.   back_texture = loadTexture("back.png");
  157.  
  158.   int DESTROYED = 0;
  159.  
  160.   //unsigned int pad_texture = 0;
  161.  
  162.   //Main game loop
  163.   while ( isRunning )
  164.     {
  165.       //EVENTS
  166.       while ( SDL_PollEvent(&event) )
  167.     {
  168.       //if the window was closed
  169.       if ( event.type == SDL_QUIT )
  170.         {
  171.           isRunning = false;
  172.         }
  173.  
  174.       //If a button was released and the button is escape
  175.       if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
  176.         {
  177.           isRunning = false;
  178.         }
  179.  
  180.       if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r )
  181.         {
  182.           glClearColor(1,0,0,1);
  183.         }
  184.  
  185.       if ( event.type == SDL_KEYDOWN ) //Check for presed down buttons
  186.         {
  187.           if ( event.key.keysym.sym == SDLK_LEFT ) //Check left key
  188.         {
  189.           left = true; //Set the boolean value for left key to true (it is pressed)
  190.         }
  191.  
  192.           else if ( event.key.keysym.sym == SDLK_RIGHT ) //Check Right key
  193.         {
  194.           right = true; //Set the boolean value for right key to true (it is pressed)
  195.         }
  196.            
  197.         }
  198.  
  199.       else if ( event.type == SDL_KEYUP ) //Checking for released buttons
  200.         {
  201.           if ( event.key.keysym.sym == SDLK_LEFT ) //Left key
  202.         {
  203.           left = false; //Set the value to false (key is released)
  204.         }
  205.          
  206.           else if ( event.key.keysym.sym == SDLK_RIGHT ) //Right key
  207.         {
  208.           right = false; //Key is released
  209.         }
  210.         }
  211.  
  212.       //logic that should happen for a certain event
  213.     }
  214.  
  215.       //LOGIC
  216.       if ( left == true ) //If left key is pressed
  217.     {
  218.       myX -= 0.5; //Move left
  219.     }
  220.  
  221.       if ( right == true ) //If right key is pressed
  222.     {
  223.       myX += 0.5; //Move right
  224.     }
  225.  
  226.       if ( myX < 0 ) //If the left border of the pad is over the left part of the screen
  227.     {
  228.       myX = 0; //Put the pad back so it isn't over the border
  229.     }
  230.  
  231.       if ( myX+width > 600 ) //If the pad is over the right border of the screen
  232.     {
  233.       myX = 600-width; //Move it back so it only touches the right border
  234.     }
  235.  
  236.       //The ball logic
  237.       ballX += vellX; //Move the ball on x axis first
  238.  
  239.       for ( int n = 0; n < BRICKS; n++ ) //Go throught the array of bricks
  240.     {
  241.       if ( bricks[n].alive == true ) //If the bricks is alive
  242.         {
  243.           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
  244.         {
  245.           vellX = -vellX; //Change x velocity of the ball
  246.           bricks[n].alive = false; //Set the alive variable to false (brick is broken)
  247.           DESTROYED += 1;
  248.           break; //Stop checking for collision on x axis
  249.         }
  250.         }
  251.     }
  252.  
  253.       ballY += vellY; //move the ball on y axis second
  254.  
  255.       for ( int n = 0; n < BRICKS; n++ ) //Go throught the array of bricks
  256.     {
  257.       if ( bricks[n].alive == true ) //If the brick is alive
  258.         {
  259.           if ( checkCollision(ballX,ballY,ballWH,ballWH,bricks[n].x,bricks[n].y,bricks[n].width, bricks[n].height) == true ) //Check for collision
  260.         {
  261.           vellY = -vellY; //Change y velocity of the ball
  262.           bricks[n].alive = false; //Set alive varible to false
  263.           DESTROYED += 1;
  264.           break; //Stop checking for collision on y axis
  265.         }
  266.         }
  267.     }
  268.  
  269.       if ( ballX < 0 ) //Check if the ball hit the left edge of screen
  270.     {
  271.       vellX = -vellX; //negate the x velocity
  272.     }
  273.  
  274.       else if ( ballX+ballWH>600 )
  275.     {
  276.       vellX = -vellX;
  277.     }
  278.  
  279.       if ( ballY < 0 )
  280.     {
  281.       vellY = -vellY;
  282.     }
  283.  
  284.       else if ( (ballY+ballWH > 400) || (DESTROYED == BRICKS)  ) //if the ball hit the bottom edge of screen or we have destroey all the bricks
  285.     {
  286.       //We set everything back as it was on the start, so we get a restart game impresion
  287.       DESTROYED = 0;
  288.  
  289.       myX = 300; //starting x position of rectangle
  290.       myY = 370; //starting y position of rectangle
  291.       width = 80; //width of the rectangle
  292.       height = 20; //height of the rectangle
  293.      
  294.       left = false,right = false; //Set the buttons back to false  
  295.      
  296.       //The ball variables
  297.       ballX = 50; //x position
  298.       ballY = 350; //y position
  299.       ballWH = 30; //width and height of the ball
  300.      
  301.       vellX = 0.2; //x speed
  302.       vellY = -0.2; //y speed
  303.  
  304.       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)
  305.         {
  306.           bricks[n].alive = true;
  307.         }
  308.     }
  309.  
  310.       if ( checkCollision(ballX,ballY,ballWH,ballWH,myX,myY,width,height) == true ) //if there is a collision between the ball and pad
  311.     {
  312.       vellY = -vellY; //change the ball's y velocity/speed
  313.      
  314.       //Calculate dinamic bounce of the ball
  315.       float temp = ballX+ballWH/2;
  316.       temp = temp - myX; // 0 - 80
  317.       temp = temp - 40; // -40 -> 40
  318.       temp = temp / 100; // -0.25 -> 0.25
  319.       vellX = temp;
  320.  
  321.     }
  322.  
  323.  
  324.  
  325.       //RENDERING to the screen
  326.       glClear(GL_COLOR_BUFFER_BIT);
  327.  
  328.       glPushMatrix(); //Start rendering phase
  329.  
  330.       glOrtho(0,600,400,0,-1,1); //Set the matrix
  331.  
  332.       glColor4ub(255,255,255,255); //White color
  333.  
  334.       //Enable textures when we are going to blend an texture
  335.       glEnable(GL_TEXTURE_2D);
  336.  
  337.       glBindTexture(GL_TEXTURE_2D,back_texture);
  338.  
  339.       //Render screen size rect and bind a texture to it
  340.       glBegin(GL_QUADS);
  341.       glTexCoord2d(0,0); glVertex2f(0,0);
  342.       glTexCoord2d(1,0); glVertex2f(600,0);
  343.       glTexCoord2d(1,1); glVertex2f(600,400);
  344.       glTexCoord2d(0,1); glVertex2f(0,400);
  345.       glEnd();
  346.  
  347.  
  348.       //What texture we are going to use
  349.       glBindTexture(GL_TEXTURE_2D,pad_texture);
  350.  
  351.       glBegin(GL_QUADS); //Start drawing the pad
  352.  
  353.       //We set the corners of the texture using glTexCoord2d
  354.       glTexCoord2d(0,0); glVertex2f(myX,myY); //Upper-left corner
  355.       glTexCoord2d(1,0); glVertex2f(myX+width,myY); //Upper-right corner
  356.       glTexCoord2d(1,1); glVertex2f(myX+width,myY+height); //Down-right corner
  357.       glTexCoord2d(0,1); glVertex2f(myX,myY+height); //Down-left corner
  358.  
  359.       glEnd(); //End drawing
  360.  
  361.       //Bind ball texture
  362.       glBindTexture(GL_TEXTURE_2D,ball_texture);
  363.      
  364.       glColor4ub(255,255,255,255); //White color
  365.  
  366.       glBegin(GL_QUADS); //Render of the ball, same method as for the pad
  367.       glTexCoord2d(0,0); glVertex2f(ballX,ballY);
  368.       glTexCoord2d(1,0); glVertex2f(ballX+ballWH,ballY);
  369.       glTexCoord2d(1,1); glVertex2f(ballX+ballWH,ballY+ballWH);
  370.       glTexCoord2d(0,1); glVertex2f(ballX,ballY+ballWH);
  371.       glEnd();
  372.  
  373.       glColor4ub(0,0,255,255); //Blue color
  374.  
  375.       glBindTexture(GL_TEXTURE_2D,brick_texture);
  376.  
  377.       glBegin(GL_QUADS);  //Render the bricks
  378.       for ( int n = 0; n < BRICKS; n++ ) //We go throught all the elements (bricks)
  379.     {
  380.       if ( bricks[n].alive == true ) //We only draw the brick if it's alive
  381.         {
  382.           if ( n%2 == 0 ) glColor4ub(122,0,255,255); //Every 2nd bricks has a different color
  383.           else glColor4ub(0,0,255,255); //Else it has normal blue color
  384.          
  385.           //The 4 edges of the current brick
  386.           glTexCoord2d(0,0); glVertex2f(bricks[n].x,bricks[n].y);
  387.           glTexCoord2d(1,0); glVertex2f(bricks[n].x+bricks[n].width,bricks[n].y);
  388.           glTexCoord2d(1,1); glVertex2f(bricks[n].x+bricks[n].width,bricks[n].y+bricks[n].height);
  389.           glTexCoord2d(0,1); glVertex2f(bricks[n].x,bricks[n].y+bricks[n].height);
  390.         }
  391.     }
  392.  
  393.       glDisable(GL_TEXTURE_2D);
  394.  
  395.       glEnd(); //End drawing
  396.  
  397.       glPopMatrix(); //End rendering phase
  398.  
  399.       SDL_GL_SwapBuffers();
  400.  
  401.       SDL_Delay(1); //Delay / pause
  402.     }
  403.  
  404.   SDL_Quit();
  405.  
  406.   return 0;
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement