Drawing a Basic Background in XNA
I have finally finished installing the system on both my work and home machines and thought it was time that I did some playing.
After playing with the Space War starterkit (Looking forward to some more) I decided to work on a simple sample that would just load a texture to the background.
The first step is to create a basic application using the main template and making sure that it ran on my systems. When it did run and I got the good old blue screen I wanted to know what the size of the main window was so that I could make sure that my background would draw correctly. To do this I added the following code.
this.Window.Title = this.Window.ClientBounds.Width.ToString() + "x" +
this.Window.ClientBounds.Height.ToString();
The above section of code was just added to the Update call and changes the windows title to the dimensions of the window. When running the application I found that it was set to be 800x600. But I want to make sure that it uses this size, to do this I used the following lines of code to adjust the back buffer size of the device and therefor force the application windows to run at 800x600.
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
this.graphics.PreferredBackBufferWidth = 800;
this.graphics.PreferredBackBufferHeight = 600;
}
I made the above changes to the Game1 call to set the PreferredBackBufferWidth, BTW If I am doing this wrong please comment. :)
Now to the Content..
Using the new Content Pipeline functions I created a new directory for the project called Content and also put another inside called Textures. To this directory I added the background texture that I am using for the background. Because the background file that I am using has a large file name I went into the properties for the Background Texture in the solution explorer and changed the Asset name to Just Background. You will find that by default the name defaults to the full name of the file minus the Path and the extension.
Next I added the following two lines just under the setup for the main components.
GraphicsDeviceManager graphics;
ContentManager content;
private SpriteBatch mainSpriteBatch;
private Texture2D backgroundTexture;
With the sprite batch and texture objects set up I now needed to assign the values to them and drop them in. to load the content Add the following lines of code to the Load Graphics Content Call.
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// TODO: Load any ResourceManagementMode.Automatic content
this.mainSpriteBatch = new SpriteBatch(graphics.GraphicsDevice);
this.backgroundTexture = content.Load<Texture2D>(@"Content\Textures\Background");
}
// TODO: Load any ResourceManagementMode.Manual content
}
Now that we have the Content Loaded we now need to be able to draw it to the screen, Add the following section of code to the Draw Call.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
this.mainSpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
Vector2 pos = new Vector2(0, 0);
this.mainSpriteBatch.Draw(this.backgroundTexture, pos, Color.White);
this.mainSpriteBatch.End();
base.Draw(gameTime);
}
Ok, That's it, you should now be able to run your application and see your background texture drawn as the background to the application. As I mentioned earlier if any one has some suggestions on how I am doing this please post or contact me.
BTW: For those who live Downunder I am still working on setting up an Australian XNA User Group, If you are interested you can get in touch with me through my Site www.virtualrealm.com.au
Cross Post from
Virtual Realm - Mykre's Space