• Sfml object system
    15 replies, posted
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
std::vector<Entitiy> ?
I looked into someone's source and it did the exact same thing. Just wondering if there could be other methods.
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.
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 ([url=http://www.boost.org/doc/libs/1_40_0/libs/libraries.htm#Containers]link[/url]) 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.
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.
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.
In my source code I used a vector of pointers which will leak memory. Use boost instead.
[QUOTE=conman420;17104422]In my source code I used a vector of pointers which will leak memory. Use boost instead.[/QUOTE] Not if you delete them at the end..?
The vector will delete the pointers, but not the objects pointed to by the container. That's natural behavior.
[QUOTE=conman420;17104422]In my source code I used a vector of pointers which will leak memory. Use boost instead.[/QUOTE] wouldn't leak if you deleted them genius.
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. [cpp] Tank *tank = new Tank; Player = tank; Entities.push_back(tank); [/cpp] You have this, how would I make this so I can pass on variables to my object ?
new Tank(5, 4, "string", 6.5f);
[QUOTE=efeX;17106410]wouldn't leak if you deleted them genius.[/QUOTE] 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.
i know. i would use a boost ptr container/smart ptr over a vector of pointers any day. just sayin'
Kay bro :D
Sorry, you need to Log In to post a reply to this thread.