In this fourth [page, second post] installment of the MGS: Pong article series, I am going to cover how to draw a background for the game. Before I start, I should tell you that there will be two more articles: one for creating the menu system and one for pulling all the classes together. Okay, let’s begin! The background class (Background.cs) is incredibly easy to create. First we need some private members to draw the background.
1
2
3
4
5
6
7
8
9
| public class Background : Microsoft.Xna.Framework.DrawableGameComponent
{
#region Private Members
private SpriteBatch m_spriteBatch;
private ContentManager m_conManager;
private Texture2D m_bgTexture;
private string m_texSource;
#endregion |
The next thing we need to do is handling how to load and unload content. This is nothing new, so no explanation is required.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| #region Constructor
public Background (Game game)
: base ( game )
{
// Instantiates a new Content Manager. The CM class
// is a lightweight object that allows us to load and
// process XNA content.
m_conManager = new ContentManager ( game.Services );
}
#endregion
#region Load / Unload Content
protected override void LoadGraphicsContent (bool loadAllContent)
{
base.LoadGraphicsContent ( loadAllContent );
// Load All Content is true when the Device is created
// or goes through a hard reset. This happens when the
// window is created and sometimes when moved to
// another monitor.
if ( loadAllContent )
{
// Instantiates a new SpriteBatch object.
m_spriteBatch = new SpriteBatch ( GraphicsDevice );
// Checks if the texture file exists, and if so
// loads the texture.
if ( File.Exists ( m_texSource + ".xnb" ) )
m_bgTexture = m_conManager.Load<Texture2D> ( m_texSource );
}
}
protected override void UnloadGraphicsContent (bool unloadAllContent)
{
base.UnloadGraphicsContent ( unloadAllContent );
// Much like loadAllContent, the value of unloadAllContent
// is true when the device is created or goes through a hard
// reset. This occurs before a LoadGraphicsContent(...) in
// the latter cases.
if ( unloadAllContent )
{
// Unloads all the data.
m_conManager.Unload ();
// Dispose of unmanaged memory and
// set the paddle texture to null
if ( m_bgTexture != null )
{
m_bgTexture.Dispose ();
m_bgTexture = null;
}
// Dispose of the spritebatch
// and set it to null.
if ( m_spriteBatch != null )
{
m_spriteBatch.Dispose ();
m_spriteBatch = null;
}
}
}
#endregion |
Next we handle drawing! Again, this is nothing new so I will just let you see the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #region Drawing
public override void Draw (GameTime gameTime)
{
base.Draw ( gameTime );
// If the texture doesn't exist, we can't draw!
if ( m_bgTexture == null )
return;
// Start drawing.
m_spriteBatch.Begin ( SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState );
// Draw the paddle.
m_spriteBatch.Draw ( m_bgTexture, Vector2.Zero, Color.White );
// End drawing.
m_spriteBatch.End ();
}
#endregion |
Finally we have a property so that we can set the texture source from outside of the class. As said before, the next article will cover the menu system.
1
2
3
4
5
6
7
8
9
10
| #region Properties
/// <summary>
/// Gets or Sets the source of the paddle's texture.
/// </summary>
public string TextureSource
{
get { return m_texSource; }
set { m_texSource = value; }
}
#endregion |
Tags: MGS, Pong, Tutorials, XNA
This entry was posted
on Friday, July 4th, 2008 at 1:32 pm and is filed under Tutorials, XNA.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
July 4th, 2008 at 3:11 pm
Hi John,
I am a long time Basic/VB/VB.NET developer. I use C# once in a while but our company has made the decision to stick with VB.NET(Using VS 2008). Long story short do you have any examples of XNA (PONG would be a great start) written in VB.NET? I would be interested and know that 100’s of Thousands of the boy and girls, men and women who use VB.NET throughout the world would be interested too.
From setting up the development environment to using your frameworks would be an awesome start.
Thanks,
Tom Harris
9d Interactive
tharris@9d.com