I'm working on a Breakout project using C and SDL2. I need to create the wall of bricks like this . The main issue is my lack of understanding on how to pass the struct array throught different functions, as it's leading to a Segmentation Fault when rendering.
To start, I created a struct with:
typedef struct bricks
{
double x;
double y;
double x0;
double y0;
double width;
double height;
double spacing;
double columns;
double rows;
char display; //1 if shown, 0 if it was hit (so its
hidden)
double power;
} bricks;
In main I created a 2D array using it:
int rows = 5, columns = 10, x, y;
bricks bricks[columns][rows];
for (int j = 0; j <= rows - 1; j++)
{
for (int i = 0; i <= columns - 1; i++)
{
initializeBricks(&bricks[i][j], 15, 10, columns, rows, 0, 0, i, j);
printf("%f\n", bricks[i][j]);
}
}
InitializeBricks function:
void initializeBricks(bricks *br, double h, double s, double col, double row, char dis, char p, int i, int j)
{
br->spacing = s;
br->columns = col;
int totalSpace = s * (col + 1);
br->width = (winWidth - totalSpace) / col;
br->x0 = (-winWidth/2) + s + (br->width/2);
br->y0 = (winHeight/2) - s - (h/2);
br->display = dis;
br->power = p;
br->x = br->x0 + (i * (s + br->width));
br->y = br->y0 - (j * (s + h));
printf("%f\n", br->x);
}
The initializeBricks printf verifies that the calculations do work as when the program runs, it outputs all the correct x values. However, the printf in the main loop outputs 0.000000 every time.
I also want to visualise the bricks, but my method of rendering doesn't work.
In main:
render(&ball, &paddle, &bricks, winWidth, winHeight);
Render Functions:
void render(ball *b, paddle *p, bricks *br[][], int winWidth, int winHeight)
{
//draw the objects
for (int j = 0; j <= br[0][0].rows - 1; j++)
{
for (int i = 0; i <= br[0][0].columns - 1; i++)
{
drawBricks(&br[][], i, j);
}
}
}
void drawBricks(bricks *br, int i, int j)
{
glBegin(GL_QUADS);
glColor3f((double)j/3, (double)j/3 + 0.2, (double)j/6);
glVertex3d((br->x - (br->width/2)), (br->y + (br->height/2)), 0);
glVertex3d((br->x + (br->width/2)), (br->y + (br->height/2)), 0);
glVertex3d((br->x + (br->width/2)), (br->y - (br->height/2)), 0);
glVertex3d((br->x - (br->width/2)), (br->y - (br->height/2)), 0);
printf("hey");
glEnd();
glFlush();
}
I should also mention that I have generated the bricks in a previous test, here , but I think I need to use structs as I want to add collisions and power-up bricks with different properties.
In short, I really need help understanding structs, arrays and pointers in this situation, as other posts haven't helped me too much.
If someone could point out the problems I am making I would be very grateful, cheers.
bricks *br[][]
is equivalent to bricks ***br
, I think you aren't accessing it in the right way.
In the render function, when you write br[0][0].rows
it doesn't correct, br is a pointer to bricks[][]
, for access to what you want I think you should try this: (*br)[0][0]
You want to send bricks *
to the drawBricks
function, so you should access a bricks argument and then send his address:
(*br)[j][i]
- is the bricks argument, and to get his address - &((*br)[j][i])
for your code try something like that:
void render(ball *b, paddle *p, bricks *br[][], int winWidth, int winHeight)
{
//draw the objects
for (int j = 0; j <= (*br)[0][0].rows - 1; j++)
{
for (int i = 0; i <= (*br)[0][0].columns - 1; i++)
{
drawBricks(&((*br)[j][i]), i, j); // do you want to send the pointer of (*br)[j][i] to 'drawBricks' func ? tell me if i didn't get it right pls.
}
}
}
Please let me know if I didn't understand you right, hope it will be helpful!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.