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

Author Topic: Sprite and texture problems inside constructor of a struct.  (Read 2273 times)

0 Members and 1 Guest are viewing this topic.

s3binator

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Hi!

We're trying to construct structure for frames that are being drawn onto the window. We're using sfml library, with their sprite and texture classes. In the constructor for the frameData structure we load a texture from file then apply it to the sprite using the .setTexture() method. We either get white block or a program crash depending on the machine when we compile and run our program.

If we create do a pointer reference to texture with .setTexture() outside the constructor for frameData, it works!

All if this is with an iterator structure that iterates through the different frames. Here is the relevant code, First this is in the header for the FrameData struct:

    private:
       struct FrameData
       {
          FrameData(const std::string& fileName, int frameDelay);
   
          sf::Texture texture;
          sf::Sprite sprite;
          unsigned int frameDelay;
       };

Second, here are the actual constructors, not working:

    const sf::Sprite& SpriteAnimator::currentFrame() const
    {
       return currentFrame_->sprite;
    }
   
    SpriteAnimator::FrameData::FrameData(const std::string& fileName, int frameDelay)
    : frameDelay(frameDelay)
    {
       texture.loadFromFile(fileName);
       sprite.setTexture(texture);
    }
     

And thirdly, here is what does work:


    const sf::Sprite& SpriteAnimator::currentFrame() const
    {
       currentFrame_->sprite.setTexture(currentFrame_->texture);
       return currentFrame_->sprite;
    }
   
    SpriteAnimator::FrameData::FrameData(const std::string& fileName, int frameDelay)
    : frameDelay(frameDelay)
    {
       texture.loadFromFile(fileName);
       sprite.setTexture(texture);
    }

Any ideas about why this happens, are we missing something? We want to have the constructor work properly, the current frame is returned a lot more frequently than the texture changing, so we don't need to update the texture every time the current frame is returned.

Thank you!
« Last Edit: June 21, 2012, 08:12:40 pm by Laurent »

s3binator

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Sprite and texture problems inside constructor of a struct.
« Reply #1 on: June 21, 2012, 07:58:23 pm »
Update, Someone on a different forum said he had the same problem and he implemented a repository for textures and just passed pointers. A work around it seems, or is it that .setTexture() only takes pointers properly? That doesnt make much sense.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sprite and texture problems inside constructor of a struct.
« Reply #2 on: June 21, 2012, 08:13:50 pm »
You probably need to define the copy constructor and assignment operator (think about what happens when you copy a sprite, and especially which texture it points to). See the last paragraph of the sprite tutorial.
Laurent Gomila - SFML developer

s3binator

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Sprite and texture problems inside constructor of a struct.
« Reply #3 on: June 22, 2012, 04:19:55 am »
Awesome thanks!

 

anything