SFML community forums

Help => Graphics => Topic started by: aegir on June 24, 2015, 10:59:31 pm

Title: sf::Sprite question?
Post by: aegir on June 24, 2015, 10:59:31 pm
I'm not sure what to google for this problem so it might already be answered, my apologies in advance.

Im drawing lots of tiles, so i draw floor tiles under player/monster (to avoid black square when i move). First floor sprite then on the same position player/monster sprite.
It all works except player sprite disappears when i move onto original enemy position but not original player position like so:
 http://imgur.com/a/wnGwV (http://imgur.com/a/wnGwV)

code for setting sprites is:
            temp.setTexture(holder["grass"]);
                        temp.setPosition(i.second);
                        sprites.push_back(std::make_unique<sf::Sprite>(temp));

                        temp.setTexture(holder["monster"]);
                        temp.setPosition(i.second);
                        sprites.push_back(std::make_unique<sf::Sprite>(temp));
 
Code is exactly the same when drawing player.

I have no idea how to approach fixing that so help :D


Title: Re: sf::Sprite question?
Post by: Nexus on June 24, 2015, 11:07:40 pm
Have you tried to use the debugger? Such problems can usually be found rather quickly :)

For us, who don't know the code, it's very difficult to help without a complete/minimal example (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368). And it also takes much longer...
Title: Re: sf::Sprite question?
Post by: aegir on June 24, 2015, 11:52:32 pm
Thanks for reply here is as minimal example as i managed to get it, in attachment are required files and cpp. To reproduce just move to position of first npc to the left.

(click to show/hide)
Title: Re: sf::Sprite question?
Post by: Arcade on June 25, 2015, 12:17:11 am
for (unsigned int i = 0; i < sprites.size(); ++i)
   window.draw((*sprites[i]));
 

The draw order is being determined by the order you put sprites into your vector. The last sprite in your vector will be the last one drawn, so If two sprites are at the same location, the last one drawn will be the one on top. I haven't looked at your code very closely, but what is likely happening is that you are drawing your player and its grass tile before you are drawing the monster and its grass tile. Therefore, the monster's grass tile will be in front of your player because it will be farther back in your vector.

The solution would be to draw all of your background grass tiles before drawing anything else.
Title: Re: sf::Sprite question?
Post by: aegir on June 25, 2015, 12:24:22 am
Thank you :D I will attempt to do that tomorrow.