XNA – Draw Text

Posted: July 11, 2009 in C#, Programming, XNA
Tags: , ,


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;
            // TODO: Add your initialization logic here

        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)
            if (Keyboard.GetState().GetPressedKeys().Length != 0)
                foreach (Keys k in Keyboard.GetState().GetPressedKeys())
                    if (k == Keys.Escape)

            // TODO: Add your update logic here


        protected override void Draw(GameTime gameTime)


            // 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);



You can download the project here :


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!

  1. Ziggy says:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s