制作 SDL_Textures 数组并将其传递给 c 中的函数的正确语法

correct syntax for making an array of SDL_Textures and passing it to a function in c

我正在尝试在 main 中创建一个纹理数组,然后将其传递给一个函数以供使用。

我想不出将指针传递给这个数组的正确方法,或者我是否写错了数组。

为了增加一层混乱,我读到我正在使用的数据类型 SDL_Texture 需要双指针,因为它的内容只能由库在内部访问。

总而言之,有人能告诉我创建和传递这种数据类型数组的正确语法吗?

测试代码:


#include <SDL.h>
#include <SDL_image.h>

//function that places array of textures across top of window
void processTexture(SDL_Renderer *renderer,
                    int arElements,
                    SDL_Texture *textureArray //correct pointer?
                    )
{
    for(int i = 0; i < arElements; ++i)
    {
        SDL_Rect textureRect = {i*10, 0, 10, 10}; //rectangle for placing textures

        //problem area?
        SDL_RenderCopy(renderer,
                       *textureArray[i], //correct pointer?
                       NULL,
                       textureRect
                       );
    }
}

int main(int argc, char *argv[])
{
    //set up, ignore this
    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_PNG);

    SDL_Window *window = SDL_CreateWindow("test",
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SDL_WINDOWPOS_UNDEFINED,
                                          100,
                                          100,
                                          0
                                          );
    SDL_Renderer *renderer = SDL_CreateRenderer(window,
                                                -1,
                                                0
                                                );

    SDL_Surface *surface = IMG_Load("test.png");
    SDL_Texture *testTexture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_FreeSurface(surface);

    //create single element test array, also problem area?
    int arElements = 1;                     //number of textures to place in array
    SDL_Texture textureArray[arElements];   //array of data type SDL_Texture with arElements number of elements. correct syntax?
    textureArray[0] = *testTexture;         //array element contains pointer to texture. correct syntax?

    //call function that uses array of textures
    processTexture(renderer,
                   arElements,
                   &textureArray //correct pointer?
                   );

    //clean up, ignore this
    IMG_Quit();
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

错误:

In function 'processTexture':|
17|error: invalid use of undefined type 'struct SDL_Texture'|
17|error: dereferencing pointer to incomplete type 'SDL_Texture {aka struct SDL_Texture}'|
19|error: incompatible type for argument 4 of 'SDL_RenderCopy'|
870|note: expected 'const SDL_Rect * {aka const struct SDL_Rect *}' but argument is of type 'SDL_Rect {aka struct SDL_Rect}'|
In function 'SDL_main':|
48|error: array type has incomplete element type 'SDL_Texture {aka struct SDL_Texture}'|
48|warning: unused variable 'textureArray' [-Wunused-variable]|

任何建议将不胜感激

您的数组类型错误。 SDL_Texture 是一种不透明类型,因此最终用户不能通过值(或取消引用指向它们的指针,同样的事情),只能通过指针来保存它们。

SDL_Texture textureArray[arElements]

那应该是一个SDL_Texture*的数组。


您在将指针传递给您自己的函数和从那里传递给 SDL 时遇到问题。

//function that places array of textures across top of window
void processTexture(SDL_Renderer *renderer,
                    int arElements,
                    SDL_Texture *textureArray //correct pointer?
                    )

这应该采用指向数组第一个元素的指针,以启用索引/指针算法以获取其他元素。取一个 SDL_Renderer**.

//call function that uses array of textures
processTexture(renderer,
               arElements,
               &textureArray //correct pointer?
               );

&textureArray 是指向整个数组的指针,但您的函数(现在)需要指向第一个元素的指针。写 &textureArray[0],或者只写 textureArray 并让衰减到指针处理它。


//problem area?
SDL_RenderCopy(renderer,
               *textureArray[i], //correct pointer?
               NULL,
               textureRect
               );

你不能在用户代码中写 *textureArray[i],因为它是一个不透明的类型,因此你不能取消引用它。因此,SDL 在这里不需要值,而是指针。因此,传递 textureArray[i]*(textureArray + i),即第 i 个元素(它本身是一个指针)。