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

Author Topic: Textures not loading properly in sprites in vectors  (Read 2900 times)

0 Members and 1 Guest are viewing this topic.

MediocreVeg1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Textures not loading properly in sprites in vectors
« on: December 20, 2020, 05:54:36 pm »
Sorry about the ambiguous title, it's a bit hard to explain :-[
I'm also a new to this forum and SFML in general, so please bear with me.
Basically, I created a class Npc which is a child class of sf::Sprite and has a few extra variables and objects in it, one of which is sf::Texture texture. I also made an std::vector<Npc> to contain multiple NPC sprites for a single map. However, when i run my program, only the texture of the last Npc object actually shows, while the others are simply a blank, white square. Since I've used a for loop to put the information (lilke position, texture file, etc.), the code for all NPCs is identical, so i fail to understand why only the last NPC's texture is showing.

The code for The vector is somewhat confusing to show since I've made a parser and used std::variant, but if you ask for it (or anything else like a screenshot of the game), I'll try my best to show only the necessary infromation. It's also night where I am, so it could take some hours before I respond in the morning or afternoon. Thanks in advance  :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Textures not loading properly in sprites in vectors
« Reply #1 on: December 20, 2020, 07:51:40 pm »
This is known as the white square problem. I'm sure you'll find many topics on this subject on the forum, it's even mentioned in the sprites tutorial.
Laurent Gomila - SFML developer

MediocreVeg1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textures not loading properly in sprites in vectors
« Reply #2 on: December 20, 2020, 08:00:55 pm »
Thanks for the reply, I'll look into it :)

MediocreVeg1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textures not loading properly in sprites in vectors
« Reply #3 on: December 20, 2020, 08:07:28 pm »
Hmm, i checked the tutorial and another page on the forum, and the issue in both seems to be that, due to the texture being in a loop/function, it is erased when the loop/function ends. However, by specifically putting the texture in the Npc class, meaning that the texture will last as long as the object, shouldn't this not be a problem in the first place?

MediocreVeg1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Textures not loading properly in sprites in vectors
« Reply #4 on: December 20, 2020, 08:11:42 pm »
Sorry for the constant replies, i now understand that it has to do with the copying of the textures in the vector. I'll try looking for more libraries with better resource management, thanks for the help.