Page 1 of 1

Array of SDL_Surfaces??

Posted: Sun Mar 08, 2009 4:47 pm
by ibly31
I'm new to SDL, and I was testing to see if I could display a line of 5 textures I made. It won't let me have an array of surfaces, this is my code:

Code: Select all

std::string tilefile[5] = {"Textures/picket_fence","Textures/picket_fence","Textures/picket_fence","Textures/picket_fence","Textures/picket_fence"};
SDL_Surface *tiles[5]= NULL;

That is the buggy part, but in case you are wondering, I'm going to display them like this:

Code: Select all


for(int i = 0; i < 5; i++){
apply_surface(i*32-32,0,tiles[i],screen);
}
Tiles should be an array of SDL_Surfaces, so I can just blit them.
Tilefile is just the file names of the tiles. I have 29 of them, and I'm going to make a tilesheet soon instead, but This is just a test.

Re: Array of SDL_Surfaces??

Posted: Sun Mar 08, 2009 4:50 pm
by Ginto8
it may be a good idea to make

Code: Select all

SDL_Surface *tiles[5]= NULL;
into

Code: Select all

SDL_Surface *tiles[5] = { NULL, NULL, NULL, NULL, NULL };
so there's no problems with undefined SDL_Surface*'s.

Re: Array of SDL_Surfaces??

Posted: Mon Mar 09, 2009 11:48 am
by programmerinprogress
ibly31 wrote:I'm new to SDL, and I was testing to see if I could display a line of 5 textures I made. It won't let me have an array of surfaces, this is my code:

Code: Select all

std::string tilefile[5] = {"Textures/picket_fence","Textures/picket_fence","Textures/picket_fence","Textures/picket_fence","Textures/picket_fence"};
SDL_Surface *tiles[5]= NULL;
[/quote]

are you adding file extensions to the filenames when you load something into the surfaces? 

and I would initialise all of the surfaces to null, as they are pointers, and it is implied that NULL shall not be used by anything, therefore initialising the pointers to NULL prevents any time bombs

Re: Array of SDL_Surfaces??

Posted: Mon Mar 09, 2009 12:30 pm
by avansc

Code: Select all

SDL_Surface **tiles = (SDL_Surface**)malloc(sizeof(SDL_Surface*));

for(int a = 0;a < 5;a++)
{
    tiles[a] = (SDL_Surface*)malloc(sizeof(SDL_Surface));
    // and on with what ever.
}

Re: Array of SDL_Surfaces??

Posted: Mon Mar 09, 2009 4:03 pm
by ibly31
what does **tiles do? I thought pointers were only one * ...?

Edit: and I thought malloc and sizeof were only used in C, I'm using C++

Re: Array of SDL_Surfaces??

Posted: Mon Mar 09, 2009 4:05 pm
by avansc
ibly31 wrote:what does **tiles do? I thought pointers were only one * ...?
its pointers to pointers.

its for dynamic amount of surfaces.

Re: Array of SDL_Surfaces??

Posted: Mon Mar 09, 2009 7:53 pm
by ibly31
IS each surface an image or am I drawing 5 diefferent surfaces when it could just be one?

Oh, and it works, thanks!1one one