mort8088 Just another WordPress site

6Mar/11Off

XNA 4.0 – Tutorial 1 – Fonts

XNA Game Studio

Originally posted 2009 - An XNA take on the old “Hello World!” code, nothing too dificult about it but it does cover adding and loading our first bit of content and the first output on screen. I've updated things to reflect using VS-2010 and XNA 4.0

 

Ok so you want to do more in your game than look at a cornflower blue window. We'll add some text so I present to you "Hello World!" XNA style.

Create a new project...

Right click on the Content Sub Project in the Solution Explorer, and select the Add>>New Item...

from the Add New Item Dialog box select the Sprite Font Option...

VS2010 will add the new file under the Content Sub Project. The file created by Visual Studio is really well commented, and explains the different options in quite some detail.

Open the Game1.cs file, under where the code defines the GraphicsDeviceManager & SpriteBatch We're going to add a variable to store our font object like so :-

1
2
3
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font;// new font variable

now scroll down to the method called LoadContent(). this is where we will load the data for the font:-

1
2
3
4
5
6
7
8
protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
 
    // Load the SpriteFont into the variable
    font = Content.Load<SpriteFont>("SpriteFont1");
}

Your game class has a Content object that was inherited from the Game class, so you don't have to define it in your own class. Content uses generics to load data from the hard drive, we'll go into The Content Pipeline in another Tutorial, the parameter that gets passed to the load method is the name of the asset. Remember in XNA 101 I said that the Content object had it's RootDirectory set in the class constructor, well this is where we see that in action. When the game is compiled the files in the Content Sub project gets put in a directory called Content with an extension of xnb.

So as you can see our SpriteFont1.spritefont is now called SpriteFont1.xnb when we tell Content.Load to fetch the file we give it the filename without the extension.

Now that we have the font loaded we will use it to display some text, update the Draw() method as show below :-

1
2
3
4
5
6
7
8
9
10
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
 
    spriteBatch.Begin();
    spriteBatch.DrawString(font, "Hello World!", new Vector2(100,100), Color.White);
    spriteBatch.End();
 
    base.Draw(gameTime);
}

Hear you can see we've added three lines, all using methods in the spriteBatch object.

  • spriteBatch.Begin()
    prepares the graphics device for drawing sprites.
  • spriteBatch.DrawString()
    Adds a sprite string to the batch of sprites to be rendered, specifying our font, the output text, screen position by way of a 2D vector object and the color we want.
  • spriteBatch.End()
    Flushes the sprite batch and restores the device state to how it was before Begin was called.

Conclusion

Where as we're still not doing anything earth shatteringly amazing with the code, writing text to the game screen is one of the most useful things we can do. Text on screen can be use to give you the developer feed back on the state of an object when you're debugging code, you can prompt the player for input or just show them the high score table. You're going to want to show words for some reason and now you can.

Related Posts:

Posted by mort8088

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.