Opinion on good draw function

Apr 16, 2011 at 3:26pm
I'm currently using sfml 1.6, trying to create a simple game engine. Ive been toying with a few ideas on how to organize a collection of sprites and then draw them to the screen.

I currently have an object class which holds a sprite. when I call
'object.spawn'
the sprite is passed into a
'std::list<sf::sprite*> Drawlist'


Basically the drawlist contains every sprite that needs to be drawn. When the object dies its sprite is removed from the draw list.

The problem with this is obviously the random access of the list and the need for 'object' to know where in the list its sprite is. Iterators are a pain too I have found. I've tried using a deque as well but a ran into a few other problems with random access.

I've heard about maps but am not too familiar with them. Do you think they would make for a better container?

any other thoughts?

-Thanks
Deftwun
Apr 16, 2011 at 4:17pm
Have you tried a std::set yet?
http://www.cplusplus.com/reference/stl/set/
You can insert your sprites into the set, find specific ones, etc, just read the above reference.
You can quite easily iterate through a set,
1
2
3
4
5
6
7
std::set<sf::sprite*> Drawlist;
//...
for(std::set<sf::sprite*>::iterator it = Drawlist.begin(); it != Drawlist.end(); ++it)
{
  //*it is an sf::sprite*
  //**it is an sf::sprite
}


Also, what do you mean 'random access'?
Last edited on Apr 16, 2011 at 4:26pm
Apr 16, 2011 at 8:53pm
No I had not. Set seems like it might do the trick. It sounds a little more complex and I'm not sure I understand the 'key' type entirely but I'll Have to read up and play around a little more.

I guess by random access I mean being able to quickly locate/insert/delete a specific element anywhere in the container.

Right now with list, I a have a variable 'DrawID' within the object class, that is set to the position of its sprite within the container. Any time i want to add/delete ONE element I have to increment/decrement EVERY objects 'DrawID'. Its cumbersome.

I liked using Deque because of the [] operator made this method more convenient but I received errors when I tried to erase objects in the middle of the container. Cant remember exactly what they were but thats not really the point ..

Just seems like there is a better way.



Topic archived. No new replies allowed.