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

Author Topic: Vectors of texture and sprite  (Read 2011 times)

0 Members and 1 Guest are viewing this topic.

Tojmak

  • Newbie
  • *
  • Posts: 4
    • View Profile
Vectors of texture and sprite
« on: December 10, 2019, 08:40:50 pm »
Hello, i have a function which send by reference, textures and sprites to vectors.  First time i made this function i had a problem. Generally speaking by tapping "i", sprites that are inside my vector of sprites should be displayed. What i get is first sprite (usually) displayed correctly and rest of them (again... usually) are white squares. After that by tapping "i" again sprites are cleared, and than again displayed by pressing the same button. But after that second time, they are displaying correctly. Below is fragment of my code:

        vector<Items*> YourItems = hero->openBackpack();

                sf::Texture tempTItems;
                sf::Sprite tempSItems;

         for (int i = 0; i < YourItems.size(); i++)
                {
                        if (!(THeroItems.size() == YourItems.size()))
                        {
                                THeroItems.push_back(tempTItems);
                                SHeroItems.push_back(tempSItems);
                        }
                        YourItems[i]->getLook(THeroItems[i], SHeroItems[i]);

                }
 

Then i remake this function. I've come up (a little randomly) with something like that:

                if (!(THeroItems.size() == YourItems.size()))
                {
                        int tempSize = THeroItems.size();
                        for (int i = 0; i < YourItems.size() - tempSize; i++)
                        {
                                THeroItems.push_back(tempTItems);
                                SHeroItems.push_back(tempSItems);
                        }
                }
               
                for (int i = 0; i < YourItems.size(); i++)
                {
                        YourItems[i]->getLook(THeroItems[i], SHeroItems[i]);
                }
 

And... that works... After some debugging i found out that pushing back vector is changing elements of sprite vector. Because of that sprites are pointing some rubbish things. And i don't have any clue why. I will be very gratefull for help. I'm not sure if amount of code provided is enaugh. If not, than i will paste more. Please help! ;)
« Last Edit: December 10, 2019, 08:56:33 pm by Tojmak »

nogoodname

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Vectors of texture and sprite
« Reply #1 on: December 11, 2019, 04:23:59 am »
Sometimes when you call push_back on an std::vector it causes reallocation, so the elements move to another location. 
If there were some pointers pointing to those elements, those pointers will become invalid. 
You can check this stackoverflow answer on iterator invalidation.

Because of this, I don't think std::vector is a good choice for storing textures, since you'll have pointers to elements. 
You may want to choose a container that doesn't invalidate pointers to elements when inserting, e.g. list or map.
« Last Edit: December 11, 2019, 04:38:28 am by nogoodname »

Tojmak

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Vectors of texture and sprite
« Reply #2 on: December 12, 2019, 12:03:47 pm »
Thanks for respond. Good to know.