Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top