简体   繁体   English

如何将 2D 结构 arrays 传递给不同的函数 [SDL2/OpenGL Breakout 项目]

[英]How do I pass 2D struct arrays to different functions [SDL2/OpenGL Breakout project]

I'm working on a Breakout project using C and SDL2.我正在使用 C 和 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:在 main 中,我使用它创建了一个 2D 数组:

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: 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. initializeBricks printf 验证计算是否在程序运行时正常工作,它会输出所有正确的 x 值。 However, the printf in the main loop outputs 0.000000 every time.但是主循环中的printf每次输出0.000000。

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.简而言之,在这种情况下,我真的需要帮助理解结构、arrays 和指针,因为其他帖子对我没有太大帮助。

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. bricks *br[][]相当于bricks ***br ,我认为您没有以正确的方式访问它。

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]在渲染 function 中,当您编写br[0][0].rows时它不正确,br 是指向bricks[][]的指针,用于访问您想要的内容我认为您应该尝试以下操作: (*br)[0][0]

You want to send bricks * to the drawBricks function, so you should access a bricks argument and then send his address:您想将bricks *发送到drawBricks function,因此您应该访问砖块参数,然后发送他的地址:

(*br)[j][i] - is the bricks argument, and to get his address - &((*br)[j][i]) (*br)[j][i] - 是砖的参数,并得到他的地址 - &((*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!如果我没有正确理解您,请告诉我,希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过函数将指针传递给c中未声明的可修改1 / 2D数组并保持其值? - How do I pass pointers to undeclared modifiable 1/2D arrays in c through a function and keep their values? 我如何通过将二维 arrays 转换为两个一维 arrays 来实现 plot 二维阵列? - How do i plot 2d array by coverting 2d arrays into two one 1d arrays? 打印2D数组时如何插入“ - ”和“=” - How do I insert “-” and “=” when printing 2D arrays 如何将数组拆分为 10 个单独的二维数组? - How do I split an Array into 10 individual 2D arrays? 如何并排打印两个2D阵列? - How do I print two 2D arrays side by side? 如何在Java中减去Arraylist的两个2D数组? - How do i subtract two 2d arrays of Arraylist in java? 如何创建二维数组数组? - How do I create a 2D array of arrays? 如何将2D数组拆分为较小的2D数组(块)的列表? - c# How do i split a 2D array into a list of smaller 2D arrays (chunks)? 如何在 java 中创建由 2d int arrays 组成的 2d 数组? - How do I create a 2d array made of 2d int arrays in java? Java Arrays.sort()方法需要一维数组,但我也可以传递一个二维数组,那为什么我不能做int [] a = b(其中b是一个二维数组)? - Java Arrays.sort() method takes 1D arrays, but I can pass a 2D array as well then why can't I do int[] a=b(where b is a 2D array)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM