SFML community forums

Help => General => Topic started by: DropboxKenshiro on July 08, 2018, 07:23:48 pm

Title: [SOLVED] sf::Drawable managment
Post by: DropboxKenshiro on July 08, 2018, 07:23:48 pm
Hi there!

I'm doing SFML for a while, but I have a question. How to manage sf:Drawables in a game? I'll expand that:
I want to have something like that: I have a special object that stores all sf::Drawables in container, that object also has inherited draw(). All of this is to simply write one function to draw all the drawables. But i went to a problem well described here: https://en.sfml-dev.org/forums/index.php?topic=14262.0 (https://en.sfml-dev.org/forums/index.php?topic=14262.0). So, I'm thinking, how to do that. I would have system that registers any drawables automatically, in object constructor. Somebody suggested using std::map and ID's, but it is possible to just use Vector?
Title: Re: sf::Drawable managment
Post by: Laurent on July 08, 2018, 08:19:45 pm
Maybe you could tell what the problem is again, because that other thread is long and we probably don't need to read it entirely ;)
Title: Re: sf::Drawable managment
Post by: DropboxKenshiro on July 09, 2018, 11:41:32 am
Wow, Laurent here...

So, to summarize. My (and that guy's too) idea was to create std::vector of pointers to sf::Drawables. Idea was pretty nice, and everything was OK, until I went into deleting pointers. I created a typical loop using iterators, like this:
for(auto a = vector.begin(); a != vector.end();)
{
    if(**a == this->sprite)
    {
          //remove
     }
     else
     { ++i; }
}
 

The problem is, I cannot compare dereferenced iterator to our sprite. Visual Studio even gives weird error about sf::Sprite != sf::Sprite. And that's pretty much the case.

I thought about using some ID, but I don;t know if it is something good.
Title: Re: sf::Drawable managment
Post by: Laurent on July 09, 2018, 12:43:03 pm
If you store addresses in your vector, then compare addresses.

if (*a == &sprite)
{
    // remove
}
Title: Re: sf::Drawable managment
Post by: DropboxKenshiro on July 09, 2018, 12:56:09 pm
I had same idea, but some strange errors happened. I will investigate the thing, and edit this post if I don't solve it.

Btw. thanks Laurent for small, but important help.

Edit: okay, I got problem Solved.