首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建SDL_Textures数组并将其传递给c#中的函数的正确语法

创建SDL_Textures数组并将其传递给c#中的函数的正确语法
EN

Stack Overflow用户
提问于 2020-06-05 13:37:03
回答 1查看 409关注 0票数 0

我试图在main中创建一个纹理数组,然后将它传递到一个函数中以供使用。

我无法找到传递指向这个数组的指针的正确方法,或者我是否写错了数组。

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

总之,请有人告诉我创建和传递此数据类型数组的正确语法。

测试代码:

代码语言:javascript
复制
#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;
}

错误:

代码语言:javascript
复制
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]|

如有任何建议,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-05 13:47:06

数组类型错误。SDL_Texture是一种不透明的类型,因此终端用户不能仅通过指针按值(或取消引用指向它们的指针)来保存它们。

代码语言:javascript
复制
SDL_Texture textureArray[arElements]

这应该是一个SDL_Texture*数组。

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

代码语言:javascript
复制
//function that places array of textures across top of window
void processTexture(SDL_Renderer *renderer,
                    int arElements,
                    SDL_Texture *textureArray //correct pointer?
                    )

这应该是指向数组的第一个元素的指针,以启用索引/指针算法来到达其他元素。拿个SDL_Renderer**

代码语言:javascript
复制
//call function that uses array of textures
processTexture(renderer,
               arElements,
               &textureArray //correct pointer?
               );

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

代码语言:javascript
复制
//problem area?
SDL_RenderCopy(renderer,
               *textureArray[i], //correct pointer?
               NULL,
               textureRect
               );

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62216631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档