Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is it possible to have an array/vector of Textures?  (Read 3757 times)

0 Members and 1 Guest are viewing this topic.

ThatOneGuyThatDoesStuff

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Is it possible to have an array/vector of Textures?
« 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?

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: Is it possible to have an array/vector of Textures?
« Reply #1 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is it possible to have an array/vector of Textures?
« Reply #2 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 ;)
Laurent Gomila - SFML developer

ThatOneGuyThatDoesStuff

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Is it possible to have an array/vector of Textures?
« Reply #3 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;
}
 

ThatOneGuyThatDoesStuff

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Is it possible to have an array/vector of Textures?
« Reply #4 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.