Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Compile command:
- g++ -o test main.cpp -lSDLmain -lSDL -lGL -lSDL_image
- */
- // specific headers
- #include "SDL.h"
- #include "SDL_opengl.h"
- #include "SDL_image.h"
- #include <iostream>
- #include <string>
- //Function for loading an image into an texture
- GLuint loadTexture( const std::string &fileName )
- {
- SDL_Surface *image = IMG_Load( fileName.c_str() );
- SDL_DisplayFormatAlpha(image);
- unsigned object(0);
- glGenTextures(1, &object);
- glBindTexture(GL_TEXTURE_2D, object);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
- //Free surface
- SDL_FreeSurface(image);
- return object;
- }
- bool checkCollision(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh) //Funcfion for checking collision
- {
- if ( Ay+Ah < By ) return false; //if A is more to the left than B
- else if ( Ay > By+Bh ) return false; //if A is more to the right than B
- else if ( Ax+Aw < Bx ) return false; //if A is higher than B
- else if ( Ax > Bx+Bw ) return false; //if A is lower than B
- return true; //There is a collision because none of above returned false
- }
- struct Brick //Brick structures which holds 4 elements
- {
- //Elements are used for x and y position of the brick and its width and height
- float x;
- float y;
- float width;
- float height;
- bool alive; //Alive variable. We use to to store in which state the brick is (broken, not broken)
- };
- //start of the program
- int main( int argc, char* args[] )
- {
- //initialize SDL
- SDL_Init(SDL_INIT_EVERYTHING);
- //Set OpenGL memory usage
- SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
- SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
- SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
- SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
- SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
- SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
- SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
- //Caption of the window
- SDL_WM_SetCaption( "Our first game", NULL );
- //Size of the window
- SDL_SetVideoMode(600,400,32, SDL_OPENGL );
- //Specific the clear color
- glClearColor(1,1,1,1); //RED,GREEN,BLUE,ALPHA
- //What portion of the screen we will display
- glViewport(0,0,600,400);
- //Shader model - Use this
- glShadeModel(GL_SMOOTH);
- //2D rendering
- glMatrixMode(GL_PROJECTION);
- //"Save" it
- glLoadIdentity();
- //Disable depth checking
- glDisable(GL_DEPTH_TEST);
- //We enable blending of textures and set how we are going to blend it
- glEnable( GL_BLEND );
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- std::cout << "OpenGL is running\n";
- std::cout << "Main loop has started\n";
- //Handles the main loop
- bool isRunning = true;
- //For handling with event
- SDL_Event event;
- float myX = 300; //starting x position of rectangle
- float myY = 370; //starting y position of rectangle
- float width = 80; //width of the rectangle
- float height = 20; //height of the rectangle
- bool left = false,right = false; //we save in which state the button is
- //The ball variables
- float ballX = 50; //x position
- float ballY = 350; //y position
- float ballWH = 30; //width and height of the ball
- float vellX = 0.2; //x speed
- float vellY = -0.2; //y speed
- //Brick elements
- const static int BRICKS = 45; //Constant number where we define how many bricks we want
- Brick bricks[BRICKS]; //We create an array of bricks
- 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
- {
- if ( x > 560 ) //If x is near the right edge of the screen
- {
- x = 4; //We start going from the left again
- y += 25; //And move a down a little
- }
- bricks[n].x = x; //Set currents bricks x position
- bricks[n].y = y; //Y position
- bricks[n].width = 60; //Width
- bricks[n].height = 20; //Height
- bricks[n].alive = true; //Set alive to true (not broken)
- }
- //Create the textures
- unsigned int pad_texture = 0;
- unsigned int ball_texture = 0;
- unsigned int brick_texture = 0;
- unsigned int back_texture = 0;
- //Load the image into the textures using the function
- pad_texture = loadTexture("pad.png");
- ball_texture = loadTexture("ball.png");
- brick_texture = loadTexture("brick.png");
- back_texture = loadTexture("back.png");
- int DESTROYED = 0;
- //unsigned int pad_texture = 0;
- //Main game loop
- while ( isRunning )
- {
- //EVENTS
- while ( SDL_PollEvent(&event) )
- {
- //if the window was closed
- if ( event.type == SDL_QUIT )
- {
- isRunning = false;
- }
- //If a button was released and the button is escape
- if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
- {
- isRunning = false;
- }
- if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r )
- {
- glClearColor(1,0,0,1);
- }
- if ( event.type == SDL_KEYDOWN ) //Check for presed down buttons
- {
- if ( event.key.keysym.sym == SDLK_LEFT ) //Check left key
- {
- left = true; //Set the boolean value for left key to true (it is pressed)
- }
- else if ( event.key.keysym.sym == SDLK_RIGHT ) //Check Right key
- {
- right = true; //Set the boolean value for right key to true (it is pressed)
- }
- }
- else if ( event.type == SDL_KEYUP ) //Checking for released buttons
- {
- if ( event.key.keysym.sym == SDLK_LEFT ) //Left key
- {
- left = false; //Set the value to false (key is released)
- }
- else if ( event.key.keysym.sym == SDLK_RIGHT ) //Right key
- {
- right = false; //Key is released
- }
- }
- //logic that should happen for a certain event
- }
- //LOGIC
- if ( left == true ) //If left key is pressed
- {
- myX -= 0.5; //Move left
- }
- if ( right == true ) //If right key is pressed
- {
- myX += 0.5; //Move right
- }
- if ( myX < 0 ) //If the left border of the pad is over the left part of the screen
- {
- myX = 0; //Put the pad back so it isn't over the border
- }
- if ( myX+width > 600 ) //If the pad is over the right border of the screen
- {
- myX = 600-width; //Move it back so it only touches the right border
- }
- //The ball logic
- ballX += vellX; //Move the ball on x axis first
- for ( int n = 0; n < BRICKS; n++ ) //Go throught the array of bricks
- {
- if ( bricks[n].alive == true ) //If the bricks is alive
- {
- 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
- {
- vellX = -vellX; //Change x velocity of the ball
- bricks[n].alive = false; //Set the alive variable to false (brick is broken)
- DESTROYED += 1;
- break; //Stop checking for collision on x axis
- }
- }
- }
- ballY += vellY; //move the ball on y axis second
- for ( int n = 0; n < BRICKS; n++ ) //Go throught the array of bricks
- {
- if ( bricks[n].alive == true ) //If the brick is alive
- {
- if ( checkCollision(ballX,ballY,ballWH,ballWH,bricks[n].x,bricks[n].y,bricks[n].width, bricks[n].height) == true ) //Check for collision
- {
- vellY = -vellY; //Change y velocity of the ball
- bricks[n].alive = false; //Set alive varible to false
- DESTROYED += 1;
- break; //Stop checking for collision on y axis
- }
- }
- }
- if ( ballX < 0 ) //Check if the ball hit the left edge of screen
- {
- vellX = -vellX; //negate the x velocity
- }
- else if ( ballX+ballWH>600 )
- {
- vellX = -vellX;
- }
- if ( ballY < 0 )
- {
- vellY = -vellY;
- }
- else if ( (ballY+ballWH > 400) || (DESTROYED == BRICKS) ) //if the ball hit the bottom edge of screen or we have destroey all the bricks
- {
- //We set everything back as it was on the start, so we get a restart game impresion
- DESTROYED = 0;
- myX = 300; //starting x position of rectangle
- myY = 370; //starting y position of rectangle
- width = 80; //width of the rectangle
- height = 20; //height of the rectangle
- left = false,right = false; //Set the buttons back to false
- //The ball variables
- ballX = 50; //x position
- ballY = 350; //y position
- ballWH = 30; //width and height of the ball
- vellX = 0.2; //x speed
- vellY = -0.2; //y speed
- 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)
- {
- bricks[n].alive = true;
- }
- }
- if ( checkCollision(ballX,ballY,ballWH,ballWH,myX,myY,width,height) == true ) //if there is a collision between the ball and pad
- {
- vellY = -vellY; //change the ball's y velocity/speed
- //Calculate dinamic bounce of the ball
- float temp = ballX+ballWH/2;
- temp = temp - myX; // 0 - 80
- temp = temp - 40; // -40 -> 40
- temp = temp / 100; // -0.25 -> 0.25
- vellX = temp;
- }
- //RENDERING to the screen
- glClear(GL_COLOR_BUFFER_BIT);
- glPushMatrix(); //Start rendering phase
- glOrtho(0,600,400,0,-1,1); //Set the matrix
- glColor4ub(255,255,255,255); //White color
- //Enable textures when we are going to blend an texture
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D,back_texture);
- //Render screen size rect and bind a texture to it
- glBegin(GL_QUADS);
- glTexCoord2d(0,0); glVertex2f(0,0);
- glTexCoord2d(1,0); glVertex2f(600,0);
- glTexCoord2d(1,1); glVertex2f(600,400);
- glTexCoord2d(0,1); glVertex2f(0,400);
- glEnd();
- //What texture we are going to use
- glBindTexture(GL_TEXTURE_2D,pad_texture);
- glBegin(GL_QUADS); //Start drawing the pad
- //We set the corners of the texture using glTexCoord2d
- glTexCoord2d(0,0); glVertex2f(myX,myY); //Upper-left corner
- glTexCoord2d(1,0); glVertex2f(myX+width,myY); //Upper-right corner
- glTexCoord2d(1,1); glVertex2f(myX+width,myY+height); //Down-right corner
- glTexCoord2d(0,1); glVertex2f(myX,myY+height); //Down-left corner
- glEnd(); //End drawing
- //Bind ball texture
- glBindTexture(GL_TEXTURE_2D,ball_texture);
- glColor4ub(255,255,255,255); //White color
- glBegin(GL_QUADS); //Render of the ball, same method as for the pad
- glTexCoord2d(0,0); glVertex2f(ballX,ballY);
- glTexCoord2d(1,0); glVertex2f(ballX+ballWH,ballY);
- glTexCoord2d(1,1); glVertex2f(ballX+ballWH,ballY+ballWH);
- glTexCoord2d(0,1); glVertex2f(ballX,ballY+ballWH);
- glEnd();
- glColor4ub(0,0,255,255); //Blue color
- glBindTexture(GL_TEXTURE_2D,brick_texture);
- glBegin(GL_QUADS); //Render the bricks
- for ( int n = 0; n < BRICKS; n++ ) //We go throught all the elements (bricks)
- {
- if ( bricks[n].alive == true ) //We only draw the brick if it's alive
- {
- if ( n%2 == 0 ) glColor4ub(122,0,255,255); //Every 2nd bricks has a different color
- else glColor4ub(0,0,255,255); //Else it has normal blue color
- //The 4 edges of the current brick
- glTexCoord2d(0,0); glVertex2f(bricks[n].x,bricks[n].y);
- glTexCoord2d(1,0); glVertex2f(bricks[n].x+bricks[n].width,bricks[n].y);
- glTexCoord2d(1,1); glVertex2f(bricks[n].x+bricks[n].width,bricks[n].y+bricks[n].height);
- glTexCoord2d(0,1); glVertex2f(bricks[n].x,bricks[n].y+bricks[n].height);
- }
- }
- glDisable(GL_TEXTURE_2D);
- glEnd(); //End drawing
- glPopMatrix(); //End rendering phase
- SDL_GL_SwapBuffers();
- SDL_Delay(1); //Delay / pause
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement