Dream come true XNA (3)-SpriteSortMode, BlendState

Source: Internet
Author: User

[Index page]
[Download source code]

Dream come true XNA (3)-SpriteSortMode, BlendState

Author: webabcd

Introduction
XNA: SpriteSortMode and BlendState

  • SpriteSortMode-the sorting method drawn by the SpriteSortMode in the game window. The default value is SpriteSortMode. Deferred.
  • BlendState-the mix of the genie and the current game interface. Default Value: BlendState. AlphaBlend

Example
1. Used to demonstrate the SpriteSortMode example (press the D key to load this Demo)
Component/Sprite/SpriteSortModeDemo. cs

Using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Microsoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; namespace XNA. component. sprite {public class SpriteSortModeDemo: Microsoft. xna. framework. drawableGameComponent {// SpriteBatch _ spriteBatch; // sprite object Texture2D _ spriteSon; Texture2D _ spriteMVP; public SpriteSortModeDemo (Game game): base (Game) {} public override void Initialize () {base. initialize ();} protected override void LoadContent () {_ spriteBatch = new SpriteBatch (Game. graphicsDevice); _ spriteSon = Game. content. load <Texture2D> ("Image/Son"); _ spriteMVP = Game. content. load <Texture2D> ("Image/MVP");} public override void Update (GameTime gameTime) {base. update (gameTime);} public override void Draw (GameTime gameTime) {Game. graphicsDevice. clear (Color. cornflowerBlue);/** SpriteSortMode-the sorting method drawn by the genie in the game window. The default value is SpriteSortMode. deferred * SpriteSortMode. deferred-when the End () method is called, The SpriteSortMode is called. immediate-when you call the Draw () method, immediately Draw the corresponding genie (In this mode, in Begin... other SpriteBatch objects cannot exist between the End and other modes.) * SpriteSortMode. frontToBack-when you call End (), draw the SpriteSortMode from small to large based on the layer depth (layerDepth. backToFront-when you call End (), draw the SpriteSortMode from large to small Based on the layer depth (layerDepth. texture-when the End () is called, the Sprite is drawn based on the Texture priority */_ spriteBatch. begin (SpriteSortMode. deferred, null); _ spriteBatch. draw (_ spriteSon, Vector2.Zero, Color. white); _ spriteBatch. draw (_ spriteSon, new Vector2 (20, 0), Color. red); _ spriteBatch. end (); _ spriteBatch. begin (SpriteSortMode. immediate, null); _ spriteBatch. draw (_ spriteSon, new Vector2 (400, 0), Color. white); _ spriteBatch. draw (_ spriteSon, new Vector2 (420, 0), Color. red); _ spriteBatch. end (); _ spriteBatch. begin (SpriteSortMode. frontToBack, null); Draw (_ spriteSon, new Vector2 (20,200), Color. red, 0.2f); Draw (_ spriteSon, new Vector2 (0,200), Color. white, 0.1f); _ spriteBatch. end (); _ spriteBatch. begin (SpriteSortMode. backToFront, null); Draw (_ spriteSon, new Vector2 (420,200), Color. red, 0.1f); Draw (_ spriteSon, new Vector2 (400,200), Color. white, 0.2f); _ spriteBatch. end (); // a Demo drawn based on the texture priority. The following test shows that the Demo files with relatively small sizes will be drawn first. // official documentation description: we recommend that you use this mode when there is no need to consider spriteBatch overlap, because it is more efficient. begin (SpriteSortMode. texture, null); _ spriteBatch. draw (_ spriteSon, new Vector2 (20,400), Color. white); // 40.3 KB _ spriteBatch. draw (_ spriteMVP, new Vector2 (0,400), Color. white); // 5.36 KB _ spriteBatch. end (); base. update (gameTime);} private void Draw (Texture2D texture, Vector2 position, Color color, float layerDepth) {_ spriteBatch. draw (texture, position, null, color, 0f, Vector2.Zero, 1f, SpriteEffects. none, layerDepth );}}}

2. Demonstrate the BlendState example (press the keyboard E key to load this Demo)
Component/Sprite/BlendStateDemo. cs

Using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Microsoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; namespace XNA. component. sprite {public class BlendStateDemo: Microsoft. xna. framework. drawableGameComponent {// SpriteBatch _ spriteBatch; // sprite object Texture2D _ sprite; public BlendStateDemo (Game game): base (game) {} public override void Initialize () {base. initialize ();} protected override void LoadContent () {_ spriteBatch = new SpriteBatch (Game. graphicsDevice); _ sprite = Game. content. load <Texture2D> ("Image/Son");} public override void Update (GameTime gameTime) {base. update (gameTime);} public override void Draw (GameTime gameTime) {Game. graphicsDevice. clear (Color. cornflowerBlue);/** BlendState-a mix of the genie and the current game interface. The default value is BlendState. alphaBlend * BlendState. alphaBlend-make the Alpha value of the genie valid * BlendState. the Alpha value of the Additive-genie is valid, and the genie and the current game interface mix each other * BlendState. opaque-make the genie completely Opaque * BlendState. nonPremultiplied-If Content Pipeline does not perform Alpha preprocessing on the Content, it will promptly process the Alpha value during Draw * select a resource in the Content project and view its attributes, the default value of its Content Processor-> Premultiply Alpha is true, that is, Content Pipeline will perform Alpha preprocessing on the Content * If the Content Processor-> Premultiply Alpha is set to false, even in the BlendState. in AlphaBlend mode, the original Alpha value of the genie does not take effect. To make the Alpha value of the genie take effect, set it to BlendState. nonPremultiplied */_ spriteBatch. begin (SpriteSortMode. deferred, BlendState. alphaBlend); _ spriteBatch. draw (_ sprite, Vector2.Zero, Color. white); _ spriteBatch. end (); _ spriteBatch. begin (SpriteSortMode. deferred, BlendState. additive); _ spriteBatch. draw (_ sprite, new Vector2 (200, 0), Color. white); _ spriteBatch. end (); _ spriteBatch. begin (SpriteSortMode. deferred, BlendState. opaque); _ spriteBatch. draw (_ sprite, new Vector2 (400, 0), Color. white); _ spriteBatch. end (); base. update (gameTime );}}}

OK
[Download source code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.