1
General / Re: White square issue after accesing resource from std::map
« on: October 05, 2016, 09:19:09 pm »
In your first example
Your texture is just available inside the function. After leaving the scope, it get's destroyed.
In your 2nd example:
You store the texture in your map. The map lives as long as the instance of your LocalTitleManager class. If you destroy your manager before drawing, you will have the same problem again.
void createGrassTitle( void ) {
sf::Texture texture;
texture.loadFromFile("media/textures/titles.png"); //one texture for simplicity
LocalTitle title;
title.create();
title.setTexture( texture ); //YOU GIVE THE FUNCTION A REFERENCE, but texture will be destroyed when this function returns
titles[LocalTitleType::Grass] = title;
} //texture gets destroyed here! Now there is "no more texture in memory"...
sf::Texture texture;
texture.loadFromFile("media/textures/titles.png"); //one texture for simplicity
LocalTitle title;
title.create();
title.setTexture( texture ); //YOU GIVE THE FUNCTION A REFERENCE, but texture will be destroyed when this function returns
titles[LocalTitleType::Grass] = title;
} //texture gets destroyed here! Now there is "no more texture in memory"...
Your texture is just available inside the function. After leaving the scope, it get's destroyed.
In your 2nd example:
void loadTextures( void ) {
sf::Texture texture;
texture.loadFromFile("title.bmp");
textures[LocalTitleType::Grass] = texture; //YOUR texture will live on beyond the scope.
}
//......
private:
std::map< LocalTitleType, LocalTitle > titles;
std::map< LocalTitleType, sf::Texture > textures;
sf::Texture texture;
texture.loadFromFile("title.bmp");
textures[LocalTitleType::Grass] = texture; //YOUR texture will live on beyond the scope.
}
//......
private:
std::map< LocalTitleType, LocalTitle > titles;
std::map< LocalTitleType, sf::Texture > textures;
void createGrassTitle( void ) {
LocalTitle title;
title.create();
title.setTexture( textures.at(LocalTitleType::Grass) ); //Here you access the stored texture and use it. And as long as your map exists, it will be able to use it.
titles[LocalTitleType::Grass] = title;
}
LocalTitle title;
title.create();
title.setTexture( textures.at(LocalTitleType::Grass) ); //Here you access the stored texture and use it. And as long as your map exists, it will be able to use it.
titles[LocalTitleType::Grass] = title;
}
You store the texture in your map. The map lives as long as the instance of your LocalTitleManager class. If you destroy your manager before drawing, you will have the same problem again.