In 2D games, what's the best way to handle the order of drawing objects? Because most games have a background, tiles to be drawn behind the player, perhaps tiles to be drawn covering up the player, etc. My point is, with my current setup of simply looping through all objects and drawing, I have no control over what objects are drawn on top of or behind the others. My best idea so far is to hold a vector of object pointers, each vector representing a different "visibility level", like so:
1 2 3 4 5 6 7 8
class Level{
//...
std::vector<Object> allObjectInstances;
std::vector<Object*> visibilityOne; //background objects
std::vector<Object*> visibilityTwo; //objects in front of background but not necessarily all the way in front
//and so on for more objects
};
If I go through with this, I'm wondering how I could loop through all my objects and add them to each vector, then shorten whatever I have to loop through for subsequent visibility vectors. Or does anybody have any better ideas for handling the order of drawing objects?
multimap<level, Object*>
You want to use an Object* because the map probably doesn't own the object. Be sure to add/remove objects from the multimap as you create/destroy them. Since maps are (by default) sorted by the key value, you can just iterate through the map drawing the objects.
Alternatively, assuming you have a orthogonal projection set up, you can just assign each sprite a different Z level and let the hardware sort it out.
The downside to this is that alpha blending doesn't really work.
That all made very little sense to me. Keep in mind this is just a cheap little SFML project.
multimap<level, Object*>
You want to use an Object* because the map probably doesn't own the object. Be sure to add/remove objects from the multimap as you create/destroy them. Since maps are (by default) sorted by the key value, you can just iterate through the map drawing the objects
Okay, that makes sense, so just map my object pointers to a certain Z axis level?