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

Author Topic: [Solved] Elements in list are all displaying the same texture  (Read 993 times)

0 Members and 1 Guest are viewing this topic.

andrasveto

  • Newbie
  • *
  • Posts: 3
    • View Profile
[Solved] Elements in list are all displaying the same texture
« 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.
« Last Edit: August 30, 2013, 04:06:59 am by andrasveto »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Elements in list are all displaying the same texture
« Reply #1 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

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.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1404
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

andrasveto

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Elements in list are all displaying the same texture
« Reply #3 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!
« Last Edit: August 24, 2013, 08:46:18 pm by andrasveto »