Skip to main content
GameDev.net gamedev.net
🔒 Locked

Cant store SDL_Surface* at std::vector :\

Started by skwee Mar 5, 2007 at 9:51 AM 6 replies 1.5k views
Original Post
skwee
skwee
Hello. I created a vector: #define MAXTILES 3 typedef std::vector Tileset; then i tried to load some picture into my vector: Tileset tileset(MAXTILES); int i; char str[32]; for(i = 0; i < MAXTILES; i++) { memset(str, '\0', 32); sprintf(str, "graphics/texture%d.png", i); tileset.push_back(IMG_Load(str)); } And when I'm trying to Blit some of these picture i get a black screen, nothing :something like SDL_BlitSurface(tilset[0/1/2], NULL, screen, &offset); I Tried also SDL_BlitSurface(tilset.at(0/1/2), NULL, screen, &offset); Am i doing something wrong? BTW its my first experience with vectors.
I would love to change the world, but they won’t give me the source code.
bubu LV
bubu LV
Try:
SDL_BlitSurface(tilset[3/4/5], NULL, screen, &offset);
skwee
skwee
Ok i tried and nothing, the same black screen.
I also tried to write
std::cerr<and at my stderr.txt
i got
000
I mean some why this block of code
int i;
char str[32];
for(i = 0; i < MAXTILES; i++)
{
memset(str, '\0', 32);
sprintf(str, "graphics/texture%d.png", i);
tileset.push_back(IMG_Load(str));
}
dont work correct :\
I would love to change the world, but they won’t give me the source code.
Kylotan
Kylotan
Check the results of IMG_Load, don't just assume it works.

You've also not specified how you got the 'screen' value, or what you're doing to the screen after you've done the blit. (Hint: you typically need to call SDL_Flip().)
skwee
skwee
Few updates:
this is screen
SDL_Surface *screen = NULL;

this is blitting
SDL_BlitSurface(tileset[0], NULL, screen, NULL);
SDL_Flip(screen);

i realized that IMG_Load return null/0 so i tried to copy the error to stderr.txt
std::cerr<<"IMG_Load() error: "<
this is how my stderr.txt looks now:
IMG_Load() error: IMG_Load() error: IMG_Load() error:

No error i cant understand what is wrong?!

Here is the full source of the project:
#define MAXTILES 3typedef std::vector<SDL_Surface*> Tileset;SDL_Surface *screen = NULL;void LoadTiles(Tileset tileset);void UnloadTiles(Tileset tileset);int main(int argc, char **argv){        SDL_Init(SDL_INIT_VIDEO);	screen = SDL_SetVideoMode(WSCREEN, HSCREEN, BPP, SDL_SWSURFACE);	Tileset tileset(MAXTILES);	LoadTiles(tileset);	SDL_BlitSurface(tileset[0], NULL, screen, NULL);	SDL_Flip(screen);	SDL_Delay(1000);	UnloadTiles(tileset);	SDL_Quit();	return 0;}void LoadTiles(Tileset tileset){	int i;	char str[32];	for(i = 0; i < MAXTILES; i++)	{		memset(str, '\0', 32);		sprintf(str, "graphics/texture%d.png", i);		tileset.push_back(IMG_Load(str));	}}void UnloadTiles(Tileset tileset){	for(Tileset::size_type i = 0; i < tileset.size(); i++)		SDL_FreeSurface(tileset.at(i));	tileset.clear();}


One more update:
I also tried to convert the png's to bmp's and load them using SDL_LoadBMP() nothing the same;

[Edited by - Minios on March 5, 2007 11:55:27 AM]
I would love to change the world, but they won’t give me the source code.
Agony
Agony
Instead of saying
tileset.push_back(IMG_Load(str));

you should use
tileset = IMG_Load(str);

Notice that when you create the tileset vector, you specify MAXTILES as the size. That means that those elements already exist. When you call .push_back(), it increases the size even further, but leaves those origial MAXTILES elements the way they are (uninitialized).
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
skwee
skwee
FIXED.
Thanks a lot :)
I would love to change the world, but they won’t give me the source code.
Agony
Agony
When you pass the tileset variable into your functions, you pass it by value, which means a copy is made, and the function can read from and write to the copy only. The original doesn't get changed.

So when you load the images into the tileset vector, they only get loaded into the copy. When the program flow returns to the main function, the copy gets destroyed, and the original is still empty. (Well, not empty, but it is uninitialized.)

To fix this, you can pass the tileset vector by reference, which means that instead of making a copy, the function receives a reference to the original. Thus any changes made to the tileset vector inside the function will also effect the original. (And any changes to the original will be visible inside the function, although this doesn't matter in this particular case.)

To do this, add an ampersand (&) after the type name, in the parameter list of the function.

//At the top where you forward-declare your functions:void LoadTiles(Tileset& tileset);void UnloadTiles(Tileset& tileset);//And below where you define them:void LoadTiles(Tileset& tileset){  //...}void UnloadTiles(Tileset& tileset){  //...}

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.