Sign in to follow this  

Drawing with ZOrder / Depth in 2D (SOLVED)

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right now, I am making a simple game engine, and I would like to know the most efficient way of drawing with ZOrder (the order to draw the objects). The game setup is similar to that of Game Maker's, which is: There is a parental class named 'instance'. All objects that are to be 'list-executed' are going to inherit this class. There is a list of 'instance' pointers in each room class 'CRoom', which is: 'vector<instance*> object;' using Polymorphism in C++. This list contains a certain amount of these instances (for an example, let's say around 10000). In the main game function, these instance pointers are used to execute the virtual 'Step()' function, where all of the movement and such is implemented. Now, here's what I want to know: Right now, all of the instances are drawn with the depth of their index in the array. I want to know how I would sort or organize this efficiently enough to where it doesn't take too much memory in the loops and such. NOTE: Each instance has it's own virtual 'Draw()' function, which of couse is where all of the drawing material for the object goes. EDIT: Forgot to say, but each instance has a integer called 'depth', which is how they are to be sorted and drawn. [Edited by - deadimp on January 19, 2005 9:20:40 PM]

Share this post


Link to post
Share on other sites
If I understand you correctly, I would just order the items in the list based on the Z order and then draw them. I have a simple 2D engine that I'm working with that keeps track of all sprites in the game and here's how I keep track of the inserts:



vector<Sprite*>::iterator SpriteIter;
for (SpriteIter = m_vSprites.begin(); SpriteIter != m_vSprites.end(); SpriteIter++)
if (PassedSprite->GetZOrder() < (*SpriteIter)->GetZOrder())
{
// Insert the sprite into the sprite vector
SpriteList.insert(SpriteIter, PassedSprite);
return;
}
}

// The sprite's z-order is highest, so add it to the end of the vector
SpriteList.push_back(PassSprite);
}



Where SpriteList is an instance variable in the GameEngine class and PassedSprite is passed to this function (AddSprite).

Share this post


Link to post
Share on other sites
But I'm using 'objects' with individual 'Draw()' functions (which are virtual). I need a way to merely draw them according to their 'depth'.
Should I try this technique of run-time sorting? It's gonna be slow though...

Share this post


Link to post
Share on other sites
If you already have a list of all objects, then sort on insert. For example, if an object moves, pop it from the list and then reinsert it based on the z order. This way the list only changes when something moves and you don't have to worry about it drawing out of order. Since objects don't move much per frame it is probably best to use the objects last position in the list as a reference of where to start searching to reinsert it.

Share this post


Link to post
Share on other sites
Actually, I just solved it. I had to make my own little structure called 'INSTANCE_LIST', which contained a pointer to my instance. I did this because I had to make my own operator overload for the STL 'sort' algorithm, by inserting the existant objects into an INSTANCE_LIST into a vector and then sorting them. Surprisingly, I didn't detect too much of a slow-down.

Share this post


Link to post
Share on other sites

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this