SFML community forums

Help => General => Topic started by: Tojmak on December 10, 2019, 08:40:50 pm

Title: Vectors of texture and sprite
Post by: Tojmak 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! ;)
Title: Re: Vectors of texture and sprite
Post by: nogoodname 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. (https://stackoverflow.com/a/54004916)

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.
Title: Re: Vectors of texture and sprite
Post by: Tojmak on December 12, 2019, 12:03:47 pm
Thanks for respond. Good to know.