XNA – Draw Text

Another simple example, how to draw a text in XNA =D
Demonstrates how to import a SpriteFont into a project and draw text using DrawString.

Here’s the code :
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace DisplayingFont
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont Font1;
Vector2 FontPos;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 400;
graphics.PreferredBackBufferHeight = 200;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Font1 = Content.Load<SpriteFont>("Courier New");
// TODO: Load your game content here
FontPos = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2,
graphics.GraphicsDevice.Viewport.Height / 2);
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
#if XBOX
if (gamePadState.Buttons.Back == ButtonState.Pressed)
this.Exit();
#else
if (Keyboard.GetState().GetPressedKeys().Length != 0)
{
foreach (Keys k in Keyboard.GetState().GetPressedKeys())
{
if (k == Keys.Escape)
this.Exit();
}
}
#endif
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Blue);
spriteBatch.Begin();
// Draw Hello World
string output = "Hello XNA !!!";
// Find the center of the string
Vector2 FontOrigin = Font1.MeasureString(output) / 2;
// Draw the string
spriteBatch.DrawString(Font1, output, FontPos, Color.White,
0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
You can download the project here :
http://www.ziddu.com/download/5554583/DisplayingFont.zip.html
note from msdn :
As with most types of software, font files are licensed rather than sold. Font licenses vary from vendor to vendor, but most do not allow redistribution of the fonts. That includes redistribution of reproductions such as bitmaps containing the rasterized character set. This is true of many of the licenses covering fonts that Microsoft supplies with applications and the Windows operating systems. Therefore, be careful to ensure that you have the required license rights to redistribute any font you include as a bitmap containing the rasterized character set in your game!







There is no reason to block out the GamePad for XBox only. I prefer to use the GamePad on PC as well