In this tutorial, we will look at how content files are used in XNA and how we can use them in our game. Content is really one of the core concepts of XNA.
Content in XNA
'Content' refers to any sort of art asset that our game needs, aside from code. Content includes the basics, like 3D model files, music, and images. But the list doesn't just end there. Our game's content includes fonts, shader effects, and more. One of the most significant components of XNA is what is called the 'Content Pipeline'. The Content Pipeline manages all of your game's content and makes it easy to load up in your game.
The Content Pipeline is extensible. While it comes with a fairly large list of file types that it can manage and load, you can write code to extend it to work with any file type that you want, including file formats that you've created yourself. Of course, writing extensions for the content pipeline can get pretty time consuming. We will leave content pipeline extensions to another tutorial, and concentrate on how to use the existing functionalities of the Content Pipeline.
In our example, we will use it to load a 3D model, but the steps are the same for any type of content, even if you are making your own extension.
The first step for getting content into our game is to get the file that contains our content. For example, if you want to use a 3D model of a car in your game, you are first going to have to get a file that contains the 3D model in it. The Internet is a great resource for finding free content of virtually any type, and it also has software (often free) that allows you to create the content that you want.
If you are working for a real game development company, an artist will supply you with all of the content that you need for the game.
For the purposes of this tutorial, you can use any image you want, or you can use the image below:
Placing Content in the Program Directory
There are a couple of different ways to get your file into your project. We will look at the easiest and most common way.
In XNA Game Studio, look in the Solution Explorer for the 'Content' node in the tree.
This represents a directory inside of your project directory called 'Content'. This is probably where you want to put all of your content, since this is the default place that your game will look in. However, I would recommend putting your content in subdirectories inside of the Content directory, to keep things organized. For instance, make a directory called 'Models' inside of your 'Content' folder, and put all of your 3D models in there. Make another one for 'Music', and so on. There are lots of ways to organize your content, and depending on what you are doing, you might want to do other things. The idea is just that you have your content organized, rather than just dumping it all in one directory.
Creating a new Content Folder (if Desired)
To create a new directory, right click on the 'Content' node (or the parent directory), click on the 'Add' menu item on the pop-up menu, and then select 'New Folder'. It will then create the new folder for you and give you a chance to name it. I've named mine 'Images', since I'm about to add an image.
You should now be able to see your new folder in the Solution Explorer, like in the image below:
Adding the Content Files
We are now ready to add the content files to our project. To do this, right click on the folder you would like the content placed in (or the Content node if you just want it in the main content directory), and select Add > Existing Item from the popup menu. You will then be allowed to browse through your file system for the content files that you would like in your game. Once you have chosen a file, it will appear in your Solution Explorer, like in the image below.
Your content file is now ready to be used in your game.