我试图在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]|如有任何建议,将不胜感激。
发布于 2020-06-05 13:47:06
数组类型错误。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),即ith元素(它本身是一个指针)。
https://stackoverflow.com/questions/62216631
复制相似问题