Sign in to follow this  
EdwardDingle

Drawing Priorities

Recommended Posts

EdwardDingle    129
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.

Share this post


Link to post
Share on other sites
TriggerB    128
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.

Share this post


Link to post
Share on other sites
EdwardDingle    129
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.

Share this post


Link to post
Share on other sites
dmatter    4821
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.

Share this post


Link to post
Share on other sites

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