Game Development Reference
In-Depth Information
The basic windows game template provided with this topic does not deal explic-
itly with content references. The way to add your own content to a game is to create
a Content project yourself. You can do this by going to
File
→
New
→
Project
and
then select the 'Empty Content Project' template. You can give this content project
a name and a location, just like any other project. Once you have done this, the con-
tent project will appear in your solution. Now you can link your own project with
this content project by right clicking on the project in the solution explorer and se-
lecting 'Add Content Reference'. You can then choose to link your content project
with your game project. Adding assets to a content project is very easy. You can
simply drag images or sounds to the content project and they will be added.
When you open the
Painter
content project, you see that it contains many different
image and sound files. This is because that content project is also used for the first
complete game that we will develop as a part of this topic. Whenever a project has
a reference to this content project, you can use all of these assets in the project.
If you look again at the content project title, you will note that after the name of
the project, there is another name (
Content
) between parentheses. This is the relative
location where the assets will be available to the game project that refers to this
content project. In this case, it is the directory 'Content'. When a project that refers
to this content project is compiled, the assets will be automatically copied into a
directory local to the program called 'Content'. However, we also need to indicate
in our
source code
where the games assets are located.
As an example, have a look at the program
SpriteDrawing
in Listing
4.2
.On
line 19, you will find the code that tells the compiler in what directory the game
assets are located:
Content.RootDirectory = "Content";
We are accessing a property called
Content
from the
Game
class. Since we are already
inside the
Game
class (or actually a special version of it), we do not need to write
anything in front of the property. The
Content
property, in turn, contains another
property called
RootDirectory
, and we can assign a
text
value to it. In this case, the
text is the name of the content folder. Here you see a value of a type we have not
discussed before, namely a
string
, which is used to represent text. When we want
to store text in a variable, we need to enclose the text by double quotation marks as
you can see when we assign the value “Content” to the property
RootDirectory
.
4.3.3 Loading Sprites
Now that we have indicated the location of sprites, we can use XNA classes and
methods to load a sprite and draw it. These classes are available in the library
Microsoft.Xna.Framework.Graphics
which we import with a
using
statement at the be-
ginning of the program:
using
Microsoft.Xna.Framework.Graphics;