www.xbdev.net xbdev - software development
Friday December 14, 2018
home | about | contact | Donations

     
 

XNA & 360

The bits and bytes of XNA and C# for your 360...

 

 

Adding text to your new xna project is just a few clicks away!  Yup, clicks....

 

  • Create new project

  • Right click on the project in the solution explorer.

  • Select Add | New Item

  • Click on Sprite Font Template

  • Give it a snazzy name (e.g. myFont)

  • Click OK, and your done!

 

Now if you followed the steps you'll have added a font template to your project.  Basically a XML file..and you can select it in your solution explorer, and you'll notice lots of parameters in there.  You can manually edit some of them.  Change the FontName to something like Arial or Time Roman.

 

myFont.spritefont

...etc..etc

<!-- One of the fonts from the C:\Windows\Fonts\ Folder -->

           <FontName>Times New Roman</FontName>

.....etc..etc

 

Just remember the font you select has to be on your computer or the project has to at least know how to find it!!!..i.e. Have a look in the C:\windows\fonts\ folder for a list.

 

Feel free to just jiggle with some of the values, like the font size, ...make it massive...100 or turning Kerning on and off....you can change most of the parameters at runtime, but if you set any of them there, for example the font size, its the default one.

 

Below I've shown the whole program...with some block comments for which are the parts of code that actually do the magic of drawing your font on screen.  Basically anything 2D, sprites, textures is done with the SpriteBatch class...which you'll usually have by default when you start a new project.

 

So when you want to use your font...you sort of use it through the SpriteBatch...very easy to see below in the draw function...hopfully you'll take a looksy at some of the extra overloaded functions on the DrawMethod, such as scaling, rotation, spriteeffects, and you can even set different draw depths, so different text is on top of other text!

 

Game.cs  [Download Full Source]

using System;

using System.Collections.Generic;

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.Net;

using Microsoft.Xna.Framework.Storage;

 

 

public class GameText : Microsoft.Xna.Framework.Game

{

    GraphicsDeviceManager   graphics;

    SpriteBatch             spriteBatch;

 

    //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

    //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

    SpriteFont              spriteFont;

 

    public GameText()

    {

        graphics = new GraphicsDeviceManager(this);

        Content.RootDirectory = "Content";

    }

 

    protected override void LoadContent()

    {

        // Create a new SpriteBatch, which can be used to draw textures.

        spriteBatch = new SpriteBatch(GraphicsDevice);

 

        //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

        //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

        spriteFont = Content.Load<SpriteFont>("myFont");

        //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

        //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

    }

 

    protected override void Draw(GameTime gameTime)

    {

        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

 

        //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

        //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

        spriteBatch.Begin();

        spriteBatch.DrawString(spriteFont,              // spriteFont

                               "Hello World!!!!",       // String

                               new Vector2(200, 200),   // ScreenPos

                               Color.Tomato);           // Colour

        spriteBatch.End();

 

        //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

        //**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**TEXTCODE**

 

        base.Draw(gameTime);

    }

}

 

// Our program entry point

static class Program

{

    static void Main(string[] args)

    {

        using (GameText game = new GameText())

        {

            game.Run();

        }

    }

}

 

 

Yup, you have text on screen with a few clicks and a few lines of code...easy as pie!

 

 

 

 

 

 

 

 

 

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.