How I solve sortings:
1. Creating IDisplay interface and adding it to every class which handles displaying Draw(SpriteBatch batch)
2. Creating a DisplayManager
which has Add
,Remove
,Draw
methods and as many layers
(List of IDisplay objects) as you want. Personally, the mid layer is the layer where I sort my stuff. So I can put stuff behind sorted objects, and before sorted objects.
3. In the Main
class which handles the drawing, call the DisplayManager's Draw
function, which will run through the layers
(Lists) and IDisplay
items and call the Draw
function on every item.
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
DisplayManager.Draw(spriteBatch);
spriteBatch.End();
And then in the DisplayManager:
public static void Draw(SpriteBatch batch){
for (i = 0; i < bottom.Count; i++)
{
bottom[i].Draw(batch);
}
//Gets the model from the IDisplay item, and then it's Y position, and orders
//the sort layer(list) by that.
sort = sort.OrderBy(o => o.getModel.Position.Y).ToList();
for (i = 0; i < sort.Count; i++)
{
sort[i].Draw(batch);
}
for (i = 0; i < top.Count; i++)
{
top[i].Draw(batch);
}
}