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!