Skip to main content
GameDev.net gamedev.net
🔒 Locked

Blitting order sort

Started by Nokame Oct 10, 2006 at 9:51 PM 3 replies 950+ views
Original Post
Nokame
Nokame
I need to know of the best sorting algorithm for blitting 2D characters on the screen... I need to do it so it looks like if the player is above a character, he's blitted behind him, and so forth, with like 40 characters in all :) thanks.
Zakwayda
Zakwayda
With only 40 characters or so I don't think you'll need to worry too much about the cost of the sort. Assuming C++, I would just store some sort of handle to the entities in a container such as a std::vector, and then run std::sort() on the container each update using a comparator functor keyed off of the entities' y values.

If somehow this proved not to be performant enough (unlikely, I think), you could update each entity's handle within the container only when its y position is modified. Typically this update will require very few swaps due to coherency. I can't imagine it getting a whole lot faster than this.

If the above solution(s) won't work for you for some reason, perhaps you could post some more info, such as programming language and graphics API.

[Edited by - jyk on October 10, 2006 10:10:26 PM]
Nokame
Nokame
yeah i'm sorry, i was pretty vague, i'm using c++ and using DirectDraw at the moment, so its nothing more than which order the characters are blitted to the screen. imagine an old rpg like final fantasy 3 or chrono trigger :) you know... its characters like these.
Sc4Freak
Sc4Freak
To do this correctly, you'd need to give each object a Z value, and sort them in descending order. Then just blit them all in order, and the furthest ones will be blitted first, the nearest ones blitted last.

Or if you just want the player to be blitted on top, and don't care about the other characters, just blit all the characters (ignoring the player) and then blit the player last. The characters will be all jumbled up, but the player will always be on top.
Zakwayda
Zakwayda
Quote:
Original post by Sc4Freak
To do this correctly, you'd need to give each object a Z value, and sort them in descending order. Then just blit them all in order, and the furthest ones will be blitted first, the nearest ones blitted last.
My interpretation of his initial post was that he already understands the above, and was just asking how to perform the sort optimally (could be wrong though).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.