XNA Tutorial – Base Application

In this Article I will be going through the basic process of creating a base XNA Application that can be used for future applications. I will be including a simple folder structure for laying out your content, and including server small sections of code that will assist in setting up the base functions.

Start by opening up Visual Studio 2005 note that this could be any of the different SKU’s of the product (Express, Standard, Pro or Team). When the application has started create a new project. When you are creating a project you will need to make sure that you create an XNA Project.

At the beginning you will have the choice of creating one of the following projects.

  1. Windows Game
  2. Windows Game Library
  3. Xbox 360 Game
  4. Xbox 360 Game Library
  5. Content Pipeline Extension Library
  6. Plus what ever Starter Kits you have installed.

With these different project types you will be able to create game applications that can be used on ether the Xbox 360 or on the Windows Machines. But you also have the choice of creating extension libraries for both the Content pipeline and your games.

Create a Windows Game and Call it “BaseApplication”, once you have selected the folder where you would like to put it the base code should open inside your Visual Studio Environment. With my code I will be using the Visual Studio Team Suite so some of the screens may look different but it will not effect any of the code or the way that the game is laid out.

Now you can run the application, if everything has gone through right you should see an application window appear on the screen with a Corn Flower Blue background.

Some people find that the Corn flower blue screen is not right for them, the easy way to change this is to modify a small section of code inside the draw call. Do a quick search through the code until you find the Draw method.

You will find the following section of code.

[code language=”C#”]

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

[/code]

This section of code clears the Games Graphic Device every single frame, this action will happen no matter what type of application you are putting together. If it didn’t you would find that you would be drawing one screen over the top of another and not clearing it out.

To change the color of the screen when it is cleared, just change the color value to a color of your choice.

The next Addition to the base application is to expand on the folder structure inside the Content project. Inside the Base Application you will see another small project called Content, this project is used to store your in game assets. What we are going to do is add a selection of folders that will break up your assets and make them easier to find.

Go to the solution explorer and right click on the content folder, you will be presented with an option to add a folder. What I would like you to do is add the following folders to the project.

  1. “Fonts” – Used to store the In Game Sprite Fonts.
  2. “Textures” – Used to Store the various Textures for the game, Including Sprites.
  3. “Models” – Used to Store the 3D Models for you game.
  4. “Effects” – Used to Store the custom Effects (.fx) that will be used in the games.
  5. “Audio” – Used to Store the Audio files stored in the Games.

The last part I would like to add to the base Application is the ability to change the Display resolution of the Application. To do this we need to add some code to the Games Initialization method.

Find this section of code inside your application.

[code language=”C#”]

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = “Content”;

}

[/code]

And change it so that it looks like this.

[code language=”C#”]

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = “Content”;

    graphics.PreferredBackBufferWidth = 1280;
    graphics.PreferredBackBufferHeight = 720;
    graphics.ApplyChanges();
}

[/code]

Now if you run the application you will see that the size of the Application window has changed.

Ok that will do me for now, I do plan on posting some more over the next few weeks as I update some of my code and site. If I can these posts should come rather quickly.

Technorati Tags: ,


Cross Post from www.virtualrealm.com.au

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>