Skip to content

Commit 61b3825

Browse files
Add background drawing
1 parent 2ff6e82 commit 61b3825

File tree

6 files changed

+143
-20
lines changed

6 files changed

+143
-20
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: Drawing a Masked Sprite over a Background
3+
description: Demonstrates how to draw a foreground and background sprite using the SpriteBatch class, where only part of the foreground sprite masks the background.
4+
requireMSLicense: true
5+
---
6+
7+
## Overview
8+
9+
In this example, you will draw a background texture, followed by another sprite on top of the background with its transparent elements not showing by using a [Blend State](xref:Microsoft.Xna.Framework.Graphics.BlendState) that supports alpha blending.
10+
11+
### End result
12+
13+
![The output of this tutorial](./images/HowTo_DrawSpriteBackground_Final.png)
14+
15+
## Requirements
16+
17+
The example assumes the texture you are loading contains multiple images, one for the background and one for the foreground ship texture.
18+
19+
- ![starfield](images/starfield.png)
20+
- ![ship texture](images/ship.png)
21+
22+
Save the textures to your content project and name it "**AnimatedCharacter**" (this name will used to reference it in the project).
23+
24+
> [!IMPORTANT]
25+
> The foreground sprite in this example must include masking information, e.g. a PNG or DDS file that supports transparency / an alpha channel.
26+
27+
## Drawing a Foreground and Background Sprite
28+
29+
1. Follow the steps of [How To: Draw a Sprite](HowTo_Draw_A_Sprite.md).
30+
A good first step to understanding the loading and drawing of textures and setting up your project.
31+
32+
1. Add some variables and update the **LoadContent** method to load and initialize the content.
33+
34+
```csharp
35+
// Position of foreground sprite on screen
36+
private Vector2 ViperPos;
37+
38+
// The texture for the ship
39+
private Texture2D shipTexture;
40+
41+
// The texture for the background
42+
private Texture2D starTexture;
43+
44+
protected override void LoadContent()
45+
{
46+
// Create a new SpriteBatch, which can be used to draw textures.
47+
spriteBatch = new SpriteBatch(GraphicsDevice);
48+
49+
starTexture = Content.Load<Texture2D>("starfield");
50+
shipTexture = Content.Load<Texture2D>("ship");
51+
Viewport viewport = graphics.GraphicsDevice.Viewport;
52+
53+
ViperPos.X = viewport.Width / 2;
54+
ViperPos.Y = viewport.Height - 100;
55+
}
56+
```
57+
58+
1. In [Game.Draw](xref:Microsoft.Xna.Framework.Game#Microsoft_Xna_Framework_Game_Draw_Microsoft_Xna_Framework_GameTime_) method of your game class, call [SpriteBatch.Begin](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_Begin_Microsoft_Xna_Framework_Graphics_SpriteSortMode_Microsoft_Xna_Framework_Graphics_BlendState_Microsoft_Xna_Framework_Graphics_SamplerState_Microsoft_Xna_Framework_Graphics_DepthStencilState_Microsoft_Xna_Framework_Graphics_RasterizerState_Microsoft_Xna_Framework_Graphics_Effect_System_Nullable_Microsoft_Xna_Framework_Matrix__) for the [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch).
59+
60+
1. Specify [BlendState.Opaque](xref:Microsoft.Xna.Framework.Graphics.BlendState).
61+
62+
> [!NOTE]
63+
> This will tell the [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) to ignore alpha color values when drawing sprites. By default, the z-order of sprites is the order in which they are drawn.
64+
65+
1. Call the [Draw](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) method, passing in the `starTexture`. Then call [SpriteBatch.End](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_End).
66+
67+
```csharp
68+
public override void Draw (GameTime game)
69+
{
70+
GraphicsDevice.Clear(Color.CornflowerBlue);
71+
72+
_spriteBatch.Begin(blendState: BlendState.Opaque);
73+
_spriteBatch.Draw (starTexture);
74+
_spriteBatch.End();
75+
}
76+
```
77+
78+
1. After this code, call [SpriteBatch.Begin](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_Begin_Microsoft_Xna_Framework_Graphics_SpriteSortMode_Microsoft_Xna_Framework_Graphics_BlendState_Microsoft_Xna_Framework_Graphics_SamplerState_Microsoft_Xna_Framework_Graphics_DepthStencilState_Microsoft_Xna_Framework_Graphics_RasterizerState_Microsoft_Xna_Framework_Graphics_Effect_System_Nullable_Microsoft_Xna_Framework_Matrix__) for the [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) again. This time, specify [BlendState.AlphaBlend](xref:Microsoft.Xna.Framework.Graphics.BlendState).
79+
80+
This will cause pixels on the sprite with an alpha value less than 255 to become progressively transparent based on the magnitude of the alpha value. An alpha of 0 will make the pixel completely transparent.
81+
82+
> [!IMPORTANT]
83+
> Calling [SpriteBatch.Begin](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_Begin_Microsoft_Xna_Framework_Graphics_SpriteSortMode_Microsoft_Xna_Framework_Graphics_BlendState_Microsoft_Xna_Framework_Graphics_SamplerState_Microsoft_Xna_Framework_Graphics_DepthStencilState_Microsoft_Xna_Framework_Graphics_RasterizerState_Microsoft_Xna_Framework_Graphics_Effect_System_Nullable_Microsoft_Xna_Framework_Matrix__) with no parameters causes [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) to **default to [BlendState.AlphaBlend](xref:Microsoft.Xna.Framework.Graphics.BlendState)**.
84+
85+
1. Next in the [Draw](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) method, we draw the `shipTexture`, `ViperPos` with [Color.White](xref:Microsoft.Xna.Framework.Color), finishing off with a call to [SpriteBatch.End](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_End).
86+
87+
```csharp
88+
public override void Draw (GameTime game)
89+
{
90+
_spriteBatch.Begin(blendState: BlendState.Opaque);
91+
_spriteBatch.Draw (starTexture);
92+
_spriteBatch.End();
93+
94+
_spriteBatch.Begin(blendState: BlendState.AlphaBlend);
95+
_spriteBatch.Draw (shipTexture, ViperPos, Color.White);
96+
_spriteBatch.End();
97+
}
98+
```
99+
100+
The end result is a fixed / opaque background with a semi-transparent ship drawn on top for the player. You can of course experiment with layers / parallax transparent backgrounds behind the player too, the choice is up to you.
101+
102+
### Extra Credit
103+
104+
Try using this technique on top of the [How To Make A Scrolling Background](HowTo_Make_Scrolling_Background.md) guide for the beginnings of your very own space shooter :D
105+
106+
## See Also
107+
108+
- [Drawing a Sprite](HowTo_Draw_A_Sprite.md)
109+
- [How To Make A Scrolling Background](HowTo_Make_Scrolling_Background.md)
110+
111+
### Concepts
112+
113+
- [What Is a Sprite?](../../whatis/graphics/WhatIs_Sprite.md)
114+
115+
### Reference
116+
117+
- [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch)
118+
- [SpriteBatch.Draw](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_Draw_Microsoft_Xna_Framework_Graphics_Texture2D_Microsoft_Xna_Framework_Vector2_Microsoft_Xna_Framework_Color_)
119+
- [Texture2D](xref:Microsoft.Xna.Framework.Graphics.Texture2D)

articles/getting_to_know/howto/graphics/HowTo_Make_Scrolling_Background.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ Save the texture to your content project and name it "**Starfield**" (this name
154154

155155
## See Also
156156

157-
- [Drawing a Sprite](HowTo_Draw_A_Sprite.md)
157+
- [Drawing a Sprite](HowTo_Draw_A_Sprite.md)
158158
- [How to animate a sprite](HowTo_Animate_Sprite.md)
159-
- [Drawing a Masked Sprite over a Background](HowTo_Draw_Sprite_Background.md)
159+
- [Drawing a Masked Sprite over a Background](HowTo_Draw_Sprite_Background.md)
160160

161161
### Concepts
162162

Loading
Loading
Loading

articles/getting_to_know/howto/graphics/index.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,45 @@ This section demonstrates several graphical concepts divided into the following
1717

1818
This section walks through several core concepts related to sprite rendering, including drawing text to the screen.
1919

20-
[How To Draw A Sprite](HowTo_Draw_A_Sprite.md)
20+
- [How To Draw A Sprite](HowTo_Draw_A_Sprite.md)
2121

22-
Demonstrates how to draw a sprite by using the SpriteBatch class.
22+
Demonstrates how to draw a sprite by using the SpriteBatch class.
2323

24-
[How To Tint A Sprite](HowTo_Tint_Sprite.md)
24+
- [How To Draw A Sprite Background](HowTo_Draw_Sprite_Background.md)
2525

26-
Demonstrates how to tint a sprite using a Color value.
26+
Demonstrates how to draw a foreground and background sprite using the SpriteBatch class, where only part of the foreground sprite masks the background.
2727

28-
[How To Rotate A Sprite](HowTo_Rotate_Sprite.md)
28+
- [How To Tint A Sprite](HowTo_Tint_Sprite.md)
2929

30-
Demonstrates how to rotate a sprite around its center.
30+
Demonstrates how to tint a sprite using a Color value.
3131

32-
[How To Rotate A Sprite Group](HowTo_Rotate_Sprite_Group.md)
32+
- [How To Rotate A Sprite](HowTo_Rotate_Sprite.md)
3333

34-
Demonstrates how to rotate a group of sprites around a single point.
34+
Demonstrates how to rotate a sprite around its center.
3535

36-
[How To Scale A Sprite](HowTo_Scale_Sprite.md)
36+
- [How To Rotate A Sprite Group](HowTo_Rotate_Sprite_Group.md)
3737

38-
Demonstrates how to scale a sprite using a uniform scale.
38+
Demonstrates how to rotate a group of sprites around a single point.
3939

40-
[How To Scale A Sprite using A Matrix](HowTo_Scale_Sprites_Matrix.md)
40+
- [How To Scale A Sprite](HowTo_Scale_Sprite.md)
4141

42-
Demonstrates how to scale sprites using a matrix that is created based on the viewport width.
42+
Demonstrates how to scale a sprite using a uniform scale.
4343

44-
[How To Tile Sprites](HowTo_Tile_Sprites.md)
44+
- [How To Scale A Sprite using A Matrix](HowTo_Scale_Sprites_Matrix.md)
4545

46-
Demonstrates how to draw a sprite repeatedly in the x and y directions in one Draw call.
46+
Demonstrates how to scale sprites using a matrix that is created based on the viewport width.
4747

48-
[How To Animate A Sprite](HowTo_Animate_Sprite.md)
48+
- [How To Tile Sprites](HowTo_Tile_Sprites.md)
4949

50-
Demonstrates how to animate a sprite from a texture using a custom class.
50+
Demonstrates how to draw a sprite repeatedly in the x and y directions in one Draw call.
5151

52-
Demonstrates how to import a SpriteFont into a project and to draw text using the DrawString method.
52+
- [How To Animate A Sprite](HowTo_Animate_Sprite.md)
5353

54-
[How To Make A Scrolling Background](HowTo_Make_Scrolling_Background.md)
54+
Demonstrates how to animate a sprite from a texture using a custom class.
55+
56+
- [How To Make A Scrolling Background](HowTo_Make_Scrolling_Background.md)
57+
58+
Demonstrates how to draw a scrolling background sprite using the SpriteBatch class.
5559

5660
> More Coming soon
5761

0 commit comments

Comments
 (0)