Defining two SpriteSortModes?

不问归期 提交于 2019-12-12 04:57:26

问题


In DirectX it is possible to set the D3DXSPRITE parameters to be a combination of both:

D3DXSPRITE_SORT_DEPTH_BACKTOFRONT

and

D3DXSPRITE_SORT_TEXTURE

Meaning that sprites are sorted first by their layer depth and then secondly by the texture that they are on. I'm trying to do the same in XNA and i'm having some problems. I've tried:

SpriteBtch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront & SpriteSortMode.Texture, SaveStateMode.None);

But it doesn't work and just seems to do them in Texture ordering, ignoring the textures layer depth. Am I doing something wrong!? Or is it not even possible?


回答1:


SpriteSortMode is an enum and should be combined using the | operator:

SpriteSortMode.BackToFront | SpriteSortMode.Texture

Update: as this article mentions, your scenario is not possible in XNA:

sorting by depth and sorting by texture are mutually exclusive




回答2:


A possible solution :

Define a new object representing a sprite to draw

class Sprite
{
    public float Priority { get; set; }      // [0..1]
    public String TextureName { get; set; }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(TextureName, ..., Priority); // here Priority is useless because of the sort mode.
    }
}

Then add a "sprites to draw" list that you:

  1. Sort by drawing priority, reverse order so Priority == 1 is first
  2. Sort by texture when a.Priority == b.Priority (this is the tricky part but not THAT hard)

So in your Main class for example, you'll have :

private List<Sprite> spritesToDrawThisTick = new List<Sprite>();

And every tick, you :

  1. Add sprites to draw
  2. Do the sorting logic
  3. Call your SpriteBatch.Begin using SpriteSortMode.Immediate
  4. Do a foreach on your list to call every Sprite's Draw method
  5. important: empty your spritesToDrawThisTick list


来源:https://stackoverflow.com/questions/1503217/defining-two-spritesortmodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!