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

Author Topic: sf::Texture and std::vector error  (Read 2566 times)

0 Members and 1 Guest are viewing this topic.

eglomer

  • Newbie
  • *
  • Posts: 13
    • View Profile
sf::Texture and std::vector error
« on: January 12, 2013, 07:51:54 pm »
Hi! I'm newbie in STL use and I'm doing some tests with a vector of my own class. The code:
class Animation{
        sf::Texture m_texture;
        sf::Sprite m_image;
        // Other stuff
       
public:
        // Constructors
        Animation(){}
        Animation (std::string file){
                m_texture.loadFromFile(file);
                m_image = sf::Sprite(m_texture);
                // ...
        }
        Animation (const Animation& anim){ // Copy constructor
                m_texture = sf::Texture(anim.m_texture);
                m_image = sf::Sprite(anim.m_image);
                // ...
        }
       
        // get the sprite to draw      
        sf::Sprite& getSprite(){
                return m_image;
        }
};
 

class Game{
        std::vector <Animation> m_player;
        // ...
       
public:
        Game(){}
       
        void run(sf::RenderWindow *m_window){
                loadLevel(); // Load players and so on...
               
                // Load players, method 2 (with this game works fine)
                /*Animation a("001.png"), b("002.png");
                m_player.push_back(a);
                m_player.push_back(b);*/

               
                while (m_window->isOpen()){
                        sf::Event event;
                        while(m_window->pollEvent(event)){
                                if(event.type == sf::Event::Closed || (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape)){
                                        m_window->close();
                                }
                        }
                       
                        m_window->clear();
                        for (std::vector<Animation>::iterator i = m_player.begin(); i < m_player.end(); i++){ // Draw the animations
                                m_window->draw((*i).getSprite()); // Here I get the error
                        }
                        m_window->display();
                }
               
                unloadLevel();
        }
       
       
        void loadLevel(){
                Animation a("001.png");
                Animation b("002.png");
                m_player.push_back(a);
                m_player.push_back(b);
        }
       
       
        void unloadLevel(){
                m_player.clear();
        }
};

int main(int argc, char **argv){
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
        Game game;
        game.run(&window);
       
        return 0;
}

The problem is that, when I use "loadLevel()" to load my animations, I get an error when I try to show them, or get an empty texture (the shape of the image filled in blank), but when I use the "method 2" instead of "loadLevel()" it works fine...

Any idea?

I'm using SFML 2.0 with CodeLite 5570 (Win7 x64).

Thank you.

PS: You can download the project from HERE

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Texture and std::vector error
« Reply #1 on: January 12, 2013, 08:06:06 pm »
std::vector can reallocate the memory and copy the textures to another location, when you insert new elements. Therefore, pointers to the texture (as the one inside sf::Sprite) become invalid. If you know the number of elements in advance, call std::vector::reserve(), otherwise take a different container.

By the way, this problem has appeared dozens of times in this forum, with the search function you would have found it very quickly. Also, try to follow the guidelines stated here (minimal code), no one will download whole projects.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eglomer

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: sf::Texture and std::vector error
« Reply #2 on: January 12, 2013, 08:20:23 pm »
I see... then I'll move it to another container. Thank you Nexus!

PS: I've tryied to search it, but I don't found nothing (maybe I haven't know how to search it...), and I'll try to follow the rules better next time.  :-[


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Texture and std::vector error
« Reply #3 on: January 12, 2013, 08:31:00 pm »
You can also use Google to search the forum, sometimes it yields better results. For example, the search for
Code: [Select]
texture vector site:en.sfml-dev.org/forums reveals many topics with that problem :)
« Last Edit: January 12, 2013, 08:37:03 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: