XNA Foundation (02) Drawing Foundation

Source: Internet
Author: User

Drawing is the most basic part of all graphics engines, and this article describes the basics of drawing in the XNA Framework.

In XNA, we use SpriteBatch to draw. First, what do we need to draw with SpriteBatch? It's the Elf Sprite, right.

So what did Sprite do with it? is a texture, such as a 2D texture texture2d. Well, you can think of textures as Sprite, like a picture of a sprite we made, a texture.

How do we get a picture loaded into our game as a sprite texture? This will be through the material pipeline Content Pipeline. The so-called material, including our game to use the pictures, models, sounds, such as a texture picture is a kind of material. The material pipeline is easy to understand, it can import the material we need into our game to XNA the correct format that can be recognized. Furthermore, the recognition and conversion of the format is done at compile time.

In the new XNA project, there will be a default content folder, and usually we will put all the material in this place, and according to the kind of material we will create some subfolders under it, such as the image subfolder to store all the picture material, Audio Folders are used to store sound material, and so on.

When an object is not recognized as a material, there will be a unique asset name Assetname to mark it, we can view the material's property page to see the material's Assetname properties and modify it.

Here we can use the material Manager Contentmanager to import a material into the game:

Texture2D texture;

texture = Content.Load<Texture2D>(@"Image\logo"); //在上文提到的LoadContent方法中调 用

Most of the time, our picture needs to be transparent, so how to draw the picture as transparent when spritebatch? There are two ways to do this: either the picture has a transparent background, or the part that needs to be transparent when making the picture is set to pure magenta (255,0, 255). In addition, it is important to note that the Spriteblendmode pattern must be alphablend (this is also the default mode, which will be mentioned later) to achieve the transparency we expect.

In rendering, we also need to pay attention to the layer depth (Layer Depth). The so-called layer Depth refers to the level of depth at which you need to draw the target sprite. By default, SpriteBatch determines the relative value of layer depth according to the order in which you call it, for example, you first call SpriteBatch to draw Sprite A, and then call SpriteBatch in the same place to draw Sprite B, then, Sprite B will block Sprite a. If we rely on the order in which the spritebatch is drawn to determine the depth of the sprite, then it is too inflexible, and to do so, when invoking the SpriteBatch rendering method, you can specify a parameter (range 0~1) that represents the depth of the hierarchy. SpriteBatch the object in the order we want it to be drawn.

With the above basics, we can explain in detail the rendering process in XNA. Each rendering process is similar to the following pattern:

protected override void Draw(GameTime gameTime)
{
  GraphicsDevice.Clear(Color.Black);
  spriteBatch.Begin();
  spriteBatch.Draw(……);
  spriteBatch.End();
  base.Draw(gameTime);
}

(1) The Graphicsdevice.clear () method clears the screen background.

(2) The Spritebatch.begin () method is used to inform the video card to prepare the scene.

(3) Take turns calling Spritebatch.draw () to draw all components.

(4) The Spritebatch.end () method is used to tell the graphics card that the drawing has been completed.

(5) Finally, the graphics card displays the results of the drawing.

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.