newcyan

Hi all.
I wrote a simple program using XNA to test the differences of SpriteSortMode.BackToFront and SpriteSortMode.FrontToBack options. I created two Texture2Ds and call SpriteBatch.Draw() method and feed it with SpriteSortMode.BackToFront, the program run well. But every time I call SpriteBatch.Draw() with SpriteSortMode.FrontToBack, the program threw an unhandled exception called NotSupportedException with the message "Specified method is not supported." Is this an error in XNA framework, or had I made some mistake in my code Please help me.

Here is my code:

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace SpriteSortModeTest
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;

private Texture2D texture1, texture2;
private SpriteBatch spriteBatch;


public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}


/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}


/// <summary>
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
/// </summary>
/// <param name="loadAllContent">Which type of content to load.</param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// TODO: Load any ResourceManagementMode.Automatic content
texture1 = content.Load<Texture2D>("Tex1");
texture2 = content.Load<Texture2D>("Tex2");
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
}

// TODO: Load any ResourceManagementMode.Manual content
}


/// <summary>
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
/// </summary>
/// <param name="unloadAllContent">Which type of content to unload.</param>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}


/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}


/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here
spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
SpriteSortMode.Deferred | SpriteSortMode.FrontToBack | SpriteSortMode.Texture,
SaveStateMode.SaveState);
spriteBatch.Draw(texture1, Vector2.Zero, Color.White);
spriteBatch.Draw(texture2, new Vector2(texture1.Width / 2, texture1.Height / 2), Color.White);
spriteBatch.End();

base.Draw(gameTime);
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}

Thanks for reading. Sorry for my English.


Re: XNA Game Studio Express Error in SpriteBatch.Draw() call with SpriteSortMode enum

Peter D.

You appear to enable depth sorting and then use a draw call that doesn't specify a depth. Is there any reason for this as otherwise you could simply have called

spriteBatch.Begin(SpriteBlendMode.AlphaBlend)

other than this the code looks fine





Re: XNA Game Studio Express Error in SpriteBatch.Draw() call with SpriteSortMode enum

neogir

This code is intended to demonstrate sprite sorting. if the depth value has specified the same error is appearing as well, it doesn't concern to the sort direction but  just to the deferring mode.  In the immediate mode it seems working fine, but the deferred one can't manage forward-to-back sorting. Strange enough.  





Re: XNA Game Studio Express Error in SpriteBatch.Draw() call with SpriteSortMode enum

Shawn Hargreaves - MSFT

The sprite sort mode is not a Flags enum, so you can't combine different values like this. You get to choose just one of the supported values at a time. Just specify BackToFront or FrontToBack, no need to or the Deferred value in with this.





Re: XNA Game Studio Express Error in SpriteBatch.Draw() call with SpriteSortMode enum

neogir

What does theXNA Help mean  saying
"You may combine Deferred with BackToFront or FrontToBack, and Texture."

Ah, got it. It concerns to the nested blocks. 





Re: XNA Game Studio Express Error in SpriteBatch.Draw() call with SpriteSortMode enum

kalkatos



I didn't understand too. How to combine Deferred with BackToFront or FrontToBack and Texture




Re: XNA Game Studio Express Error in SpriteBatch.Draw() call with SpriteSortMode enum

Shawn Hargreaves - MSFT

The documentation is wrong about this.

The sprite sort modes are mutually exclusive: you cannot combine them in any way.