Sign in to follow this  
ChainedHollow

SDL_Rect array passed to function not working correctly?

Recommended Posts

I have a class that has an SDL_Rect array. The function that sets it's values is:

[source lang="cpp"]void Block::set_collisionBox()
{

int countX = 0;
int countY = 0;
bool found = false;
int blockNum = 0;

for(int col = 0; col < 4; col++)
{
for(int row = 0; row < 4; row++)
{
if(shape[row][col] == 2)
countX++;
else if(shape[row][col] == 1)
{
blockBox[blockNum].x = (int)x+(20*countX);
blockBox[blockNum].y = (int)y+(20*countY);
blockBox[blockNum].w = 20;
blockBox[blockNum].h = 20;
found = true;

countX++;
}
}
countX = 0;

if(found == true)
countY++;
}
}[/source]

where x and y are the offset values in the class. This function is called every time after the Block object moves. I use the same loops in another function that displays 4 blocks at the appropriate position and it works just fine.

Now, I pass the blockBox array to a function from another class which is:

[source lang="cpp"] #pragma region Set_Board

void Board::set_board(int shape, SDL_Rect box[])
{
int indexX = 0;
int indexY = 0;

for(int i = 0; i < 4; i++)
{
indexX = (box[i].x - 200)/20;
indexY = (box[i].y - 20)/20;

board[indexX][indexY] = shape;
}
}[/source]
board is a matrix is size [10][24] inside the Board class. So it should take the x and y offsets of each collision box of the Block object and determine which cell of the Board matrix to change to the value of shape.
This is called after the Block stops moving(hits the ground or another block) to tell the board where to apply the blocks, so after there should be 4 spaces filled in the appropriate shape.
However, only 1 block is being field in. I can drop the block to the ground multiple times and it will fill one spot only each time so I'm highly confused! Im guessing that somehow only 1 of the boxes is being accessed but why?

This is the function call:

game_board.set_board(3, tetramino.getBox());

and getBox() is just

SDL_Rect* Block::getBox()
{
return blockBox;
}


I hope someone can explain why this would be happening. I think if I can get this part working, it work be a major achievement for this Tetris game. Thank you

Share this post


Link to post
Share on other sites
In set_collisionBox, blockNum appears to always be 0.

I am curious as to why you are doing the collision in pixels. In tetris, you can use the grid indices directly for collision. You only need to generate rectangles to render, and can do that on the fly.

It would be nicer to use an enumeration, or even named constants, over the magic numbers "1" and "2" for shape (presumably meaning "filled" and "empty"), and the value "20" (your current block size).

If all the values in the "shape" array are either 1 or 2, you can simply from this:
[code]
if(shape[row][col] == 2)
countX++;
else if(shape[row][col] == 1)
{
// Stuff...
countX++;
}
[/code]
To this:
if(shape[row][col] == 1)
{
// Stuff...
}
countX++;
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this