SFML community forums

Help => General => Topic started by: ThatOneGuyThatDoesStuff on March 17, 2020, 08:35:16 pm

Title: Is it possible to have an array/vector of Textures?
Post by: ThatOneGuyThatDoesStuff on March 17, 2020, 08:35:16 pm
Is it possible to have an array/vector of Textures or do they have to be Sprites?
Title: Re: Is it possible to have an array/vector of Textures?
Post by: Fx8qkaoy on March 18, 2020, 09:54:04 am
Is it possible to have an array/vector of Textures or do they have to be Sprites?

U can have array / vector of sf::Texture. Pay attention at how u use the copy constructor. U may want a vector of pointers

If I'm not wrong, theoretically u can have a vector of any SFML resource
Title: Re: Is it possible to have an array/vector of Textures?
Post by: Laurent on March 18, 2020, 11:17:06 am
You can have a vector of any movable or copiable type. A common issue, is that a std::vector may relocate its elements in memory when growing, which will result in undefined behaviour if you have sf::Sprite instances pointing to these textures.

If you have a specific issue with a vector of sf::Texture then describe it more precisely, otherwise then just experiment and see by yourself ;)
Title: Re: Is it possible to have an array/vector of Textures?
Post by: ThatOneGuyThatDoesStuff on March 27, 2020, 10:33:54 pm
You can have a vector of any movable or copiable type. A common issue, is that a std::vector may relocate its elements in memory when growing, which will result in undefined behaviour if you have sf::Sprite instances pointing to these textures.

If you have a specific issue with a vector of sf::Texture then describe it more precisely, otherwise then just experiment and see by yourself ;)

Thanks for the reply. I am having an issue with it. As an array, trying to assign a specific, or any, slot to a specific texture causes an instant crash at the point of assigment. Changing to vector creates the same problem. I can convert it to hold an array/vector of sf::Sprite with the Textures assigned, and then all is well. Any idea what the problem could be?

sf::Texture PlantTextures[plantTexNum];

int main(){
  PlantTextures[0] = carrotTex;
}
 
Title: Re: Is it possible to have an array/vector of Textures?
Post by: ThatOneGuyThatDoesStuff on March 28, 2020, 07:10:21 am
My brain worked on this while I was sleeping. The problem seems to lie with assigning an empty texture as a different texture. I changed PlantTextures[0] = carrotTex to PlantTextures[0].loadFromFile("Location");.

I would still appreciate explaining why this didn't work so I can know  better. May avoid future problems of that ilk.