Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


#ActualBeerNutts

Posted 10 January 2013 - 04:32 PM

Phil, what did you not understand about this code I posted?

 
</p><div>struct TBrick {</div>
<div>  int XPosition;</div>
<div>  int YPosition;</div>
<div>  int XSize;</div>
<div>  int YSize;</div>
<div>  uint32_t Color;</div>
<div>  uint32_t ActiveState;  // 0 is deactive; If it takes multiple hits to kill a brick, it can start greater than 1</div>
<div>};</div>
<div> </div>
<div>struct TBall {</div>
<div>  int XPosition;</div>
<div>  int YPosition;</div>
<div>  int XSize;</div>
<div>  int YSize;</div>
<div>  int XSpeed;</div>
<div>  int YSpeed;</div>
<div>};</div>
<div> </div>
<div>#define BRICK_COLUMNS 20 // how many bricks in a column</div>
<div>#define BRICK_ROWS 10 // how many bricks ina  row</div>
<div>#define BRICK_START_X 60 // Where the bricks start on the x-axis</div>
<div>#define BRICK_START_Y 40 // Where the bricks start on the y-axis</div>
<div>#define BRICK_WIDTH 30 // brick width in pixels</div>
<div>#define BRICK_HEIGHT 15 // brickheight in pixels</div>
<div> </div>
<div>// make this a global array</div>
<div>TBrick BrickArray[BRICK_COLUMNS][BRICK_ROWS];</div>
<div> </div>
<div>// Initialize the Brick Array somewhere at the start of your code</div>
<div>for (int x = 0; x < BRICK_COLUMNS; x++) {</div>
<div>  for (int y = 0; y < BRICK_ROWS; y++) {</div>
<div>    BrickArray[x][y].XSize = BRICK_WIDTH;</div>
<div>    BrickArray[x][y].YSize = BRICK_HEIGHT;</div>
<div>    BrickArray[x][y].XLocation = BRICK_START_X + x*BRICK_WIDTH;</div>
<div>    BrickArray[x][y].YLocation = BRICK_START_Y + y*BRICK_HEIGHT;</div>
<div>    BrickArray[x][y].Color = 0xFFFFFFFF; // white</div>
<div>    BrickArray[x][y].ActiveState = 1; // 1 hit to turn off brick</div>
<div>  }</div>
<div>}</div>
<div>  </div>
<div>// in main loop do this:</div>
<div>// move Ball in X direction, then check collision</div>
<div>Ball.XLocation += Ball.XSpeed;</div>
<div>if (CheckCollision(Ball)) {</div>
<div>  // The ball hit something, move to original location and negate X speed</div>
<div>  Ball.XLocation -= Ball.XSpeed;</div>
<div>  Ball.XSpeed = -Ball.XSpeed;</div>
<div>}</div>
<div> </div>
<div>// Do same for Y movement</div>
<div>Ball.YLocation += Ball.YSpeed;</div>
<div>if (CheckCollision(Ball)) {</div>
<div>  // The ball hit something, move to original location and negate Y speed</div>
<div>  Ball.YLocation -= Ball.YSpeed;</div>
<div>  Ball.YSpeed = -Ball.YSpeed;</div>
<div>}</div>
<div> </div>
<div>RenderBricks();</div>
<div> </div>
<div>... // the rest of the main loop</div>
<div> </div>
<div> </div>
<div>// Here are those functions</div>
<div>bool CheckCollisions(TBall ball)</div>
<div>{</div>
<div>  // loop through every brick and see if we've hit it</div>
<div>  for (int x = 0; x < BRICK_COLUMNS; x++) {</div>
<div>    for (int y = 0; y < BRICK_ROWS; y++) {</div>
<div>     // Only check against bricks that are active</div>
<div>     if (BrickArray[x][y].ActiveState > 0) {</div>
<div>      if (ball.XLocation + ball.XSize < BrickArray[x][y].XLocation ||</div>
<div>          ball.YLocation  + ball.YSize < BrickArray[x][y].YLocation ||</div>
<div>          ball.XLocation > BrickArray[x][y].XLocation + BrickArray[x][y].XSize ||</div>
<div>          ball.YLocation  < BrickArray[x][y].YLocation + BrickArray[x][y].YSize) {</div>
<div>        // It's collided wit brick, decrment Active state of brick and return true</div>
<div>        BrickArray[x][y].ActiveState--;</div>
<div>        return true;</div>
<div>      }</div>
<div>    }</div>
<div>  }</div>
<div> </div>
<div>  // Check if we've hit the paddle and </div>
<div>  // Check if we've hit the wall (I'll leave this up to you)</div>
<div>}</div>
<div> </div>
<div>// This Draws the bricks</div>
<div>void RenderBricks()</div>
<div>{</div>
<div>  // loop through every brick and draw if active</div>
<div>  for (int x = 0; x < BRICK_COLUMNS; x++) {</div>
<div>    for (int y = 0; y < BRICK_ROWS; y++) {</div>
<div>     if (BrickArray[x][y].ActiveState > 0) {</div>
<div>       // Call you GL draw function, whatever it is.  it would be simple to do in SFML however</div>
<div>       GlDrawRect(BrickArray[x][y].XLocation, BrickArray[x][y].YLocation,</div>
<div>                           BrickArray[x][y].XSize, BrickArray[x][y].YSize, BrickArray[x][y].Color);</div>
<div>     }</div>
<div>   }</div>
<div>  }</div>
<div>}</div>
<div>

 

It gives a general idea of how to encapsulate brick's and the ball into it's own structure, and shows how to handle collisions and rendering.


#1BeerNutts

Posted 10 January 2013 - 03:49 PM

Phil, what did you not understand about this code I posted?

 

[code=auto:0] 

struct TBrick {
  int XPosition;
  int YPosition;
  int XSize;
  int YSize;
  uint32_t Color;
  uint32_t ActiveState;  // 0 is deactive; If it takes multiple hits to kill a brick, it can start greater than 1
};
 
struct TBall {
  int XPosition;
  int YPosition;
  int XSize;
  int YSize;
  int XSpeed;
  int YSpeed;
};
 
#define BRICK_COLUMNS 20 // how many bricks in a column
#define BRICK_ROWS 10 // how many bricks ina  row
#define BRICK_START_X 60 // Where the bricks start on the x-axis
#define BRICK_START_Y 40 // Where the bricks start on the y-axis
#define BRICK_WIDTH 30 // brick width in pixels
#define BRICK_HEIGHT 15 // brickheight in pixels
 
// make this a global array
TBrick BrickArray[BRICK_COLUMNS][BRICK_ROWS];
 
// Initialize the Brick Array somewhere at the start of your code
for (int x = 0; x < BRICK_COLUMNS; x++) {
  for (int y = 0; y < BRICK_ROWS; y++) {
    BrickArray[x][y].XSize = BRICK_WIDTH;
    BrickArray[x][y].YSize = BRICK_HEIGHT;
    BrickArray[x][y].XLocation = BRICK_START_X + x*BRICK_WIDTH;
    BrickArray[x][y].YLocation = BRICK_START_Y + y*BRICK_HEIGHT;
    BrickArray[x][y].Color = 0xFFFFFFFF; // white
    BrickArray[x][y].ActiveState = 1; // 1 hit to turn off brick
  
// in main loop do this:
// move Ball in X direction, then check collision
Ball.XLocation += Ball.XSpeed;
if (CheckCollision(Ball)) {
  // The ball hit something, move to original location and negate X speed
  Ball.XLocation -= Ball.XSpeed;
  Ball.XSpeed = -Ball.XSpeed;
}
 
// Do same for Y movement
Ball.YLocation += Ball.YSpeed;
if (CheckCollision(Ball)) {
  // The ball hit something, move to original location and negate Y speed
  Ball.YLocation -= Ball.YSpeed;
  Ball.YSpeed = -Ball.YSpeed;
}
 
RenderBricks();

... // the rest of the main loop


// Here are those functions
bool CheckCollisions(TBall ball)
{
  // loop through every brick and see if we've hit it
  for (int x = 0; x < BRICK_COLUMNS; x++) {
    for (int y = 0; y < BRICK_ROWS; y++) {
     // Only check against bricks that are active
     if (BrickArray[x][y].ActiveState > 0) {
      if (ball.XLocation + ball.XSize < BrickArray[x][y].XLocation ||
          ball.YLocation  + ball.YSize < BrickArray[x][y].YLocation ||
          ball.XLocation > BrickArray[x][y].XLocation + BrickArray[x][y].XSize ||
          ball.YLocation  < BrickArray[x][y].YLocation + BrickArray[x][y].YSize) {
        // It's collided wit brick, decrment Active state of brick and return true
        BrickArray[x][y].ActiveState--;
        return true;
      }
    }
  }
 
  // Check if we've hit the paddle and 
  // Check if we've hit the wall (I'll leave this up to you)
}
 
// This Draws the bricks
void RenderBricks()
{
  // loop through every brick and draw if active
  for (int x = 0; x < BRICK_COLUMNS; x++) {
    for (int y = 0; y < BRICK_ROWS; y++) {
     if (BrickArray[x][y].ActiveState > 0) {
       // Call you GL draw function, whatever it is.  it would be simple to do in SFML however
       GlDrawRect(BrickArray[x][y].XLocation, BrickArray[x][y].YLocation,
                           BrickArray[x][y].XSize, BrickArray[x][y].YSize, BrickArray[x][y].Color);
     }
   }
  }[/code]

 

It gives a general idea of how to encapsulate brick's and the ball into it's own structure, and shows how to handle collisions and rendering.


PARTNERS