SFML community forums

Help => Graphics => Topic started by: andrasveto on August 24, 2013, 06:48:24 pm

Title: [Solved] Elements in list are all displaying the same texture
Post by: andrasveto on August 24, 2013, 06:48:24 pm
Hi,

I'm trying to draw all the members in a list<list<Brick> > where a Brick is one of my classes. The problem is that if I use a list, all of the bricks have the color of the first brick in the first list of bricks. If I use a vector, all of them have the color of the last brick. This happens even if I just use a list<Brick> and push_back all of the bricks. I've pasted the relevant code below.

Thanks for your time and any help you can provide! Please let me know if there is anything else I should add.

// the code where I initialize the lists of bricks
list<Brick> blueBricks (window.getSize().x / 96 - 1, Brick("blueBrick.png"));
list<Brick> goldBricks (window.getSize().x / 96 - 1, Brick("goldBrick.png"));
list<Brick> greenBricks (window.getSize().x / 96 - 1, Brick("greenBrick.png"));

list<list<Brick> > bricks;
bricks.push_back(blueBricks);
bricks.push_back(goldBricks);
bricks.push_back(greenBricks);

// the brick class has private data members brickTexture and brickSprite

// this is the constructor
Brick::Brick(const string& file)
{
    brickTexture.loadFromFile(file);
    brickSprite.setTexture(brickTexture);
    brickSprite.setOrigin(brickSprite.getLocalBounds().width / 2,
        brickSprite.getLocalBounds().height / 2);
    inPlay = true;
}

// this is how I draw the bricks on the window
for (list<list<Brick> >::iterator i = bricks.begin(); i != bricks.end(); ++i)
    for (list<Brick>::iterator j = i->begin(); j != i->end(); ++j)
        j->draw(window);

void Brick::draw(sf::RenderWindow& window)
{
    if (inPlay)
    {
        brickSprite.setPosition(position);
        window.draw(brickSprite);
    }
}
 

I'm using SFML 2.1 with VS 2012 on Windows 7.
Title: Re: Elements in list are all displaying the same texture
Post by: The Hatchet on August 24, 2013, 07:35:21 pm
My guess is it would have to do with when inserting into a list essentially a new list is made, contents copied over and the old list destroyed.  When this happens sprites can lose the memory location of the texture.  This thread is an example of it: http://en.sfml-dev.org/forums/index.php?topic=12583.0 (http://en.sfml-dev.org/forums/index.php?topic=12583.0)

Don't insert the 3 lists of bricks into a single one and try drawing all 3 individually, they should draw just fine.  You then may have to set your texture after your lists of bricks are added to a single list if you still want to use a list of lists.
Title: Re: Elements in list are all displaying the same texture
Post by: binary1248 on August 24, 2013, 08:29:49 pm
Same idea as here: http://en.sfml-dev.org/forums/index.php?topic=12583.0
Title: Re: Elements in list are all displaying the same texture
Post by: andrasveto on August 24, 2013, 08:38:56 pm
Thanks a lot for your help guys, it works now! Sorry that I made a redundant thread, I didn't look at the one you guys mentioned. Thanks again for the help!