Drawing Priorities

Started by
3 comments, last by EdwardDingle 14 years, 11 months ago
First time i've tried programming a 2d game where its not completely based on tiles. This has caused me some issues regarding drawing priority. If my hero for example, walks infront of a tree that is of different size than him, how can I go about making sure that the tree is drawn first, and then my hero? All solutions that i have developed for this goes along the lines of duck tapeish code, and am looking for the "right" approach on how to go about this. Graphics thread draws the tiles, then it needs to decide in which order to draw props, npcs, and pcs.
Advertisement
Have constants linked to numbers (an enum) that represent 'layers' to draw on.

ex. SKY(0), FOLIAGE (1), TERRAIN (2), CHARACTER (3)

Hold the drawable entities in a container based upon their draw order. First in the container is all items with SKY(back) priority, followed by FOLIAGE, and so on.

Simply call each entities Draw() function as you loop through the container from front to back.
But then the problem comes in where the character is behind the tree- yet the tree is tall enough to go over him slightly, but the character will now be drawn in the leaves, do you see my problem? I can post pictures if it will help.
If the base of the character is below the base of the tree then draw the tree first; if the base of the tree is below the base of the character then draw the character first.

To generalise this, let's assume that the position (x, y) of a sprite represents it's base (as opposed to maybe the top-left corner, or the center); you want to draw objects positioned "further away" first, this probably means drawing objects that are closer to the top of screen first, in other words you iterate them from top to bottom. Whether or not you need to sort based on you y-component first depends on your data structures.
for the love of god, thank you. You made me see that I forgot to fix the drawing based on bottom rather than top.

This topic is closed to new replies.

Advertisement