1

In my XNA game I'm using spritesortmode.texture a lot as its supposed to be a fairly cheap way of drawing. My problem is that things draw the right way most of the time but sometimes things are wrong (ie. the background is drawn over the foreground) This seems to be affected by what resolution the desktop is, whether it is fullscreen, or whether a 2nd monitor is plugged into my laptop.

Heres part of my draw block showing where these things that get mixed up sometimes are drawn.

 //============SPritebatch for background LINEAR WRAP textures===============

        spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Opaque, SamplerState.LinearWrap,
               DepthStencilState.Default,
                RasterizerState.CullNone, null, _camera.Transform);

            Globals.CurrentLevel.CurrentRoom.DrawBg(spriteBatch);

            spriteBatch.End();
        }

  //===========SPritebatch for foreground objects, blocks etc.=================
        spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Opaque, SamplerState.LinearClamp,
               DepthStencilState.Default,
               RasterizerState.CullNone, null, _camera.Transform);

        //Draw Currentroom

        Globals.CurrentLevel.CurrentRoom.Draw(spriteBatch);

        spriteBatch.End();

And heres where I load the textures.. (I load them all at the same time)

    namespace Artefact001
    {
        class Textures
        {
            public Texture2D TiledBG01Texture { get; private set; }
            public Texture2D Blocktxture { get; private set; }
            public Texture2D ProjectileTxture { get; private set; }
            public Texture2D PointLightTexture32 { get; private set; }

            public Textures(Game game)
            {          
                PointLightTexture32 = LightTextureBuilder.CreatePointLight(game.GraphicsDevice, 32);
                Blocktxture = game.Content.Load<Texture2D>(".\\WallGraphics\\blocks");
                ProjectileTxture = game.Content.Load<Texture2D>(".\\splosions\\projectile01");
                TiledBG01Texture = game.Content.Load<Texture2D>(".\\BGgraphics\\bgtile01");
            }
        }
    }

Ive read in a few places that spritesortmode.texture uses the order that the textures are loaded in to determine the draw order but it was never explained explicitly how you use this to get the correct order. If someone could explain how this is actually supposed to work that'd be great!

Cyral
  • 13,999
  • 6
  • 50
  • 90
Guye Incognito
  • 2,726
  • 6
  • 38
  • 72
  • Try using texture atlases to reduce texture swaps instead. See [this answer](http://gamedev.stackexchange.com/a/9289/288). – Andrew Russell Apr 28 '13 at 08:04
  • I'm using atlases. As you can see I am only loading 4 textures for my whole game! The stuff in the 4 different textures will always be drawn using different blendstates or samplerstes so I think having them in differnt textures makes no difference. – Guye Incognito Apr 28 '13 at 15:25

1 Answers1

4

Basicly, no matter what order you call spriteBatch.Draw() within a SpriteBatch, SpriteSortMode.Texture will sort it by texture to try and speed up rendering. You can get nearly the same speed but without the sorting by texture that is giving you this behaviour by using SpriteSortMode.Deferred.

Other SpriteSortModes can be found on here on MSDN.

Cyral
  • 13,999
  • 6
  • 50
  • 90
  • Thx! I *want* to sort by texture though. (ie. I have background textures and foreground textures) In the official documentation they mention that this can be done but dont mention how – Guye Incognito Apr 28 '13 at 15:27
  • By sorting I don't mean having backgrounds and forgrounds, I mean that the graphics card AUTOMATICLY sorts them without knowing which is foregound and which is background. – Cyral Apr 28 '13 at 15:29
  • Ok. I was under the impression that you could order your textures somehow.. Strangely in its current form the draw order is correct *almost* all the time – Guye Incognito Apr 28 '13 at 15:39
  • Yeah deferred is fine... I want the best performance though!! I am like that... I will try immediate and just make sure I am only using 1 texture at a time. according to this it is the best http://blogs.msdn.com/b/shawnhar/archive/2006/12/13/spritebatch-and-spritesortmode.aspx – Guye Incognito Apr 28 '13 at 15:47
  • 2
    @GuyeIncognito **The modes got changed around in XNA 4.0, `Immediate` is now the *slowest*** (it doesn't batch at all, it transmits each `Draw` to the GPU individually). `Deferred` now provides the best performance if you already have minimal texture swaps. – Andrew Russell Apr 29 '13 at 11:27