1
\$\begingroup\$

Let's say I have a std::vector with sf::sprites and my maps are auto-generated.

Then you won't know which element is which sprite.


Example :

I want THAT tree sprite to be removed, but I have no idea which element that tree sprite is inside of my vector... :(


\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

Fundamentally you're going to have to decide on some kind of external representation of your sprites, something that other code can pass around and store that affords them quick access to your sprites. This kind of object, however implemented, is often referred to as a "handle."

You have several basic options:

  • Store the index of the sprite associated with the logical object (e.g., Tree has a field for m_sprite or more generally WorldObject has a field for m_sprite), so that when you want to destroy or otherwise manipulate the sprite for that object you already have a way to reference it.

  • Use an associative container (such as std::map) instead of std::vector to store your sprites, use the world object or some other useful handle object (as described initially) as the key.

  • Use an associative container as above, but mapping to indices in the sprites vector instead of directly mapping to the sprites (this allows you to preserve the contiguous nature of the sprites array).

For slightly more advanced scenarios, you may find Sean Middleditch's article on slot maps interesting.

\$\endgroup\$
6
  • \$\begingroup\$ I will definitely look into std::map, sounds a lot less confusing than the other ones. Also, not sure if I should mark this as an answer since I haven't tested it yet. So I don't know if this is what I'm looking for even though I'm 99% sure it is, should I still mark this as an answer? :3 \$\endgroup\$
    – BiiX
    Commented Jan 10, 2016 at 18:02
  • 3
    \$\begingroup\$ You can always unmark it later, but I would generally wait at least a few hours before selecting an answer in case other users have something to contribute (sometimes users are discouraged from posting if a question is already "answered"). \$\endgroup\$
    – user1430
    Commented Jan 10, 2016 at 18:19
  • \$\begingroup\$ What if I want to use it with a for loop? Doesn't seem to work like this : i.gyazo.com/c749bdc0df07553ae591dc7b964a2a51.png \$\endgroup\$
    – BiiX
    Commented Jan 10, 2016 at 20:34
  • \$\begingroup\$ It's not clear to me what "sprites" and "sprites[i]" are there; you may want to ask/post code in the chat. \$\endgroup\$
    – user1430
    Commented Jan 10, 2016 at 21:00
  • \$\begingroup\$ Not sure how to use the chat, but sprites is the name of my std::map. \$\endgroup\$
    – BiiX
    Commented Jan 10, 2016 at 21:13
2
\$\begingroup\$

I would suggest instead of using a std::vector, you use a std::map.

This is what I do in similar situations where I want to keep a "heap" of world objects, but still be able to identify any one of them in O(1) time. In this case, each sprite would maintain a unique identifying key value which you could use to access it in the map.

Keep in mind you can still iterate through a std::map the same way you would as a vector, so you can still use it the same way. But this way, if you want a specific sprite, you can refer directly to it by its key value.

\$\endgroup\$
2
\$\begingroup\$

Although Josh's answer cover's more cases, but in your special case you can do it with an easier (but slightly slower in some cases) approach.

Each sprite has a texture.. you can use that to your advantage in this case. When you want to remove all the trees from your scene, you can iterate over all the objects and mark any sprite using "tree texture" to be removed.

The same idea can also be expanded by sub classing the Sprite class, and adding a tag to each sprite. Then you can assign different tags to objects based on what ever feature you want to keep track of, and later make decisions using these tags.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .