1. Post #1
    quincy18's Avatar
    September 2007
    1,001 Posts
    Whats the best way to make a system that can store all the entities that you create and call the draw function for each of the entities ?

    c++
    Sfml

  2. Post #2
    Gold Member
    bean_xp's Avatar
    June 2006
    196 Posts
    std::vector<Entitiy> ?

  3. Post #3
    quincy18's Avatar
    September 2007
    1,001 Posts
    I looked into someone's source and it did the exact same thing. Just wondering if there could be other methods.

  4. Post #4
    Gold Member
    bean_xp's Avatar
    June 2006
    196 Posts
    Sure, there are always other methods. If you know you will always have a fixed number of something, you can use a straight array.

    A vector can dynamically resize, so it can work with any number of entities. You could implement your own "vector" or list container if you felt inclined, but std::vector should serve you well enough. You can write a wrapper for your entities list, so you can call entities->Draw(); which would iterate through the vector, and entities->Add( Entitiy ) to add to the vector, which looks a little tidier than directly using the vector.
    Reply With Quote Edit / Delete Reply Show Events Agree Agree x 1 (list)

  5. Post #5
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    You should look at the advantages and disadvantages (e.g. how they store the object) of every STL container.
    Boosts containers should also be of some interest (link) and the pool memory allocator defiantly is worth a look - if you are ok with using Boost.

    I haven't really gotten into it myself, but many sources say that you should not use the default provided allocator for the STL containers if you care much about performance; you can easily change that by passing another class as the second template parameter.

  6. Post #6
    Gold Member
    gparent's Avatar
    January 2005
    3,949 Posts
    The second template parameter isn't a viable way to pass your memory allocator, because according to the standard, you're not obligated to use the parameter in the first place. Scott Meyers touches this in Effective STL, it's a fascinating read.
    Reply With Quote Edit / Delete Reply Show Events Dumb Dumb x 1 (list)

  7. Post #7
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    Just because you are not obligated to, you can still do it. What would be wrong with it?
    I can imagine that the default allocator is written to be fitting for a special case - as I said, I haven't gotten into it myself much - but I've often read, that concerning games the default allocator is too slow.

  8. Post #8
    Gold Member
    conman420's Avatar
    January 2007
    1,794 Posts
    In my source code I used a vector of pointers which will leak memory. Use boost instead.
    Reply With Quote Edit / Delete Reply United Kingdom Show Events Dumb Dumb x 4Disagree Disagree x 1 (list)

  9. Post #9
    Gold Member
    Vampired's Avatar
    February 2005
    374 Posts
    In my source code I used a vector of pointers which will leak memory. Use boost instead.
    Not if you delete them at the end..?
    Reply With Quote Edit / Delete Reply Show Events Agree Agree x 3 (list)

  10. Post #10
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    The vector will delete the pointers, but not the objects pointed to by the container. That's natural behavior.
    Reply With Quote Edit / Delete Reply Show Events Agree Agree x 1 (list)

  11. Post #11
    Gold Member
    efeX's Avatar
    April 2009
    2,332 Posts
    In my source code I used a vector of pointers which will leak memory. Use boost instead.
    wouldn't leak if you deleted them genius.
    Reply With Quote Edit / Delete Reply United States Show Events Late Late x 1 (list)

  12. Post #12
    quincy18's Avatar
    September 2007
    1,001 Posts
    conman420, I need some help. you game me your tanksrc for helping me. Only you don't need to pass on variables to the object creation.

    		Tank *tank = new Tank;
    		Player = tank;
    		Entities.push_back(tank);
    

    You have this, how would I make this so I can pass on variables to my object ?

  13. Post #13
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    new Tank(5, 4, "string", 6.5f);

  14. Post #14
    Gold Member
    gparent's Avatar
    January 2005
    3,949 Posts
    wouldn't leak if you deleted them genius.
    Or you can just use a boost ptr container or smart pointers to avoid the problem altogether. And end up with shorter, easier to read, cleaner code.
    Reply With Quote Edit / Delete Reply Show Events Agree Agree x 1Late Late x 1 (list)

  15. Post #15
    Gold Member
    efeX's Avatar
    April 2009
    2,332 Posts
    i know. i would use a boost ptr container/smart ptr over a vector of pointers any day. just sayin'
    Reply With Quote Edit / Delete Reply Show Events Late Late x 1 (list)

  16. Post #16
    Gold Member
    gparent's Avatar
    January 2005
    3,949 Posts
    Kay bro :D
    Reply With Quote Edit / Delete Reply Show Events Bad Spelling Bad Spelling x 1Friendly Friendly x 1 (list)