Ericson Willians
Ericson Willians

Reputation: 7845

SDL 2.0 / C++ - How can I make an array of "SDL_Texture"s?

SDL_Texture *t0; SDL_Texture *t1;   
SDL_Texture *t[] = {t0,t1};

It compiles just fine, but if I try to render it (Considering that t0 and t1 are already defined, of course):

SDL_RenderCopy(ren,t[0],NULL,&someRect);
SDL_RenderCopy(ren,t[1],NULL,&someRect);

I get... nothing (By nothing I mean: Pure and infinite and unfathomable and useless darkness). And this:

SDL_RenderCopy(ren,t0,NULL,&someRect);
SDL_RenderCopy(ren,t1,NULL,&someRect);

Works just fine.

The problem is that, supposing that my game had 9999 textures, I want just to loop over the texture array, instead of doing boring manual work 9999 times. I really appreciate any help, for I don't know how SDL_Texture was implemented and have absolutely no idea why it just does not work inside an array.

Upvotes: 1

Views: 3808

Answers (1)

Philipp Lenk
Philipp Lenk

Reputation: 951

I just tried a simple example and had no problem when using an array for the SDL_Texture pointers, so I assume there is something wrong with the parts of your code not shown here.

How and where exactly did you define the t0 and t1 variables? If you did not assign them before initializing your array, as shown above, the pointers in the array will point to arbitrary values(to whichever the unitialized pointers t0 and t1 pointed) and changing the latter afterwards will not change the copies stored in the array. (This would imply you are not using the valid SDL_Texture* in your calls to SDL_RenderCopy when using the array).

The relevant part of my testing code was this:

SDL_Texture *tex0=LoadImage("test.bmp",renderer), *tex1=LoadImage("test1.bmp",renderer);
SDL_Texture *arr[]={tex0,tex1};
SDL_Rect dest0, dest1;
dest0.x=0;
dest0.y=0;
dest1.x=200;
dest1.y=200;

SDL_QueryTexture(tex0, NULL, NULL, &dest0.w, &dest0.h);
SDL_QueryTexture(tex1, NULL, NULL, &dest1.w, &dest1.h);

SDL_RenderClear(renderer);

SDL_RenderCopy(renderer,tex0,NULL,&dest0);
SDL_RenderCopy(renderer,tex1,NULL,&dest1);
//SDL_RenderCopy(renderer,arr[0],NULL,&dest0);
//SDL_RenderCopy(renderer,arr[1],NULL,&dest1);                                             

SDL_RenderPresent(renderer);
SDL_Delay(2000);

Both the commented and not commented calls to SDL_RenderCopy produced the same result. This piece of code is, of course, in no way ideal, but was merely meant to test the problem at hand.

I apologize for not being able to give a definite answer and hope i could be of a little help.

Upvotes: 4

Related Questions