Log in Page Discussion History Go to the site toolbox

XNA Tutorials/Display text

From BluWiki

Displaying Text in C#

Written by Ben Hale and Weston Castleberg

Part of a series of XNA Tutorials.

C# Spritefont Declaration and Usage

First, one needs a "spritefont" for most every font and font size one wants in one's XNA project.

Sample:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">
    <FontName>Arial</FontName>
      <Size>14</Size>
      <Spacing>0</Spacing>
      <UseKerning>true</UseKerning>
      <Style>Regular</Style>
      <CharacterRegions>
        <CharacterRegion>
          <Start> </Start>
          <End>~</End>
        </CharacterRegion>
      </CharacterRegions>
  </Asset>
</XnaContent>

Then one must declare a SpriteFont object:

//wherever your declarations are
SpriteFont courierNew;

// in LoadContent()
courierNew = Content.Load<SpriteFont>("Courier New");

The DrawString() function of our spriteBatch is called to display text on the screen. It is necessary to place this function between the spriteBatch.begin() and spriteBatch.end() functions as seen below.

DrawString(name of font, text string, position, color);

spriteBatch.begin();
   spriteBatch.DrawString(myfont, "Hello World!", new Vector2(50.0f, 10.0f), Color.Black);
spriteBatch.end();

How to Color Text in C#

Coloring Text Output to the Screen

Below is an override of the Draw function in a Visual Studio Windows Game(3.1) project. Please see the accompanying screenshot to see what this function does.

protected override void Draw(GameTime gameTime)
{
  //Assume that GraphicsDevice, SpriteBatch, and myfont are declared elsewhere
  GraphicsDevice.Clear(Color.BurlyWood);
  spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
     spriteBatch.DrawString(myfont, "Hello World!", new Vector2(50.0f, 10.0f), new Color((byte)0,(byte)0,(byte)0));
  spriteBatch.End();
  base.Draw(gameTime);
}

A screenshot of "Hello World!" displayed by the above code


In the above code the Color is set to black by setting the red, green and blue values all to 0. Please note that this constructor takes three bytes, while hard-coded numbers in C# are by default 32-bit signed integers. Therefore, in order to effectively call this constructor, one must be sure to cast the parameters accordingly. For more information on this constructor, please see the next section.

Initializing a Color Object

If one wishes to initialize a color object to something besides white, then that person has two options

Using a Static Instance of Color

The Color class in C# already supports a number of predefined (or static) instances. These static instances include all of the commonly known colors, such as Red, Orange, Yellow, and Purple. They also include some less commonly known hues such as SeaShell, Peru, and BurlyWood. To use one of these static instances, one must simply use the Color object's name. It is unnecessary and incorrect to instantiate a new Color object. For example:

protected override void Draw(GameTime gameTime)
{
  //Assume that GraphicsDevice, SpriteBatch, and myfont are declared elsewhere
  GraphicsDevice.Clear(Color.BurlyWood);
  spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
     spriteBatch.DrawString(myfont, "Hello World!", new Vector2(50.0f, 10.0f), Color.Black);
  spriteBatch.End();
  base.Draw(gameTime);
}

The above code will produce the output identical to the screenshot seen previously.

Creating a New Color

As many obscure colors as are represented by the static objects in the Color class, they don't come close to representing every possible color which is capable of being rendered by the Color class (some 16777216 different hues). So, it is possible to create a new Color object and initialize it with custom values for Red, Green, and Blue. For example:

protected override void Draw(GameTime gameTime)
{
  //Assume that GraphicsDevice, SpriteBatch, and myfont are declared elsewhere
  GraphicsDevice.Clear(Color.BurlyWood);
  spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
     //the following statement will output "Hello World!" in Aquamarine
     spriteBatch.DrawString(myfont, "Hello World!", new Vector2(50.0f, 10.0f), new Color(127.0f, 255.0f, 212.0f));
  spriteBatch.End();
  base.Draw(gameTime);
}

It is necessary to note that two different constructors may be called here. The first constructor takes three bytes, the second constructor takes three floats. Therefore, one must be sure to cast variables accordingly, or the ambiguous call will be ignored and the Color object will be assigned its default value. The above examples calls the constructor using floats. The f immediately following each number is a C# macro which denotes the number immediately succeeding it to be a floating point value.

Below is an example of outputting text in a color which is initialized using predefined values:

protected override void Draw(GameTime gameTime)
{
  int red = 127;
  int blue = 255;
  int green = 212;
  //Assume that GraphicsDevice, SpriteBatch, and myfont are declared elsewhere
  GraphicsDevice.Clear(Color.BurlyWood);
  spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
     spriteBatch.DrawString(myfont, "Hello World!", new Vector2(50.0f, 10.0f), new Color((byte)red,(byte)green,(byte)blue));
  spriteBatch.End();
  base.Draw(gameTime);
}

Below is a screenshot of "Hello World!" output in Aquamarine by either of the above methods.

A screenshot of "Hello World" output in Aquamarine by either of the above methods


Generating Random Colors

With the knowledge above, it should be relatively easy to introduce a random color generator. Random number generation itself is beyond the scope of this article; for more information please see Generating Random Numbers.

It is possible to generate a random color by calling the random number generator for each of the red, green, and blue values to be sent to the constructor. For example:

protected override void Draw(GameTime gameTime)
{
  //Assume that GraphicsDevice, SpriteBatch, and myfont are declared elsewhere
  GraphicsDevice.Clear(Color.BurlyWood);
  spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
     spriteBatch.DrawString(myfont, "Hello World!", new Vector2(50.0f, 10.0f), new Color((byte)gen(255),(byte)gen(255),(byte)gen(255)));
  spriteBatch.End();
  base.Draw(gameTime);
}

Below is a screenshot of the text output when the random generator returns 244, 50, and 6 for red, green, and blue respectively.

A screenshot of the text output when the random generator returns 244, 50, and 6 for red, green, and blue respectively

References

Microsoft, MSDN Library, January 2009

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 10 September 2009, at 18:40.
Disclaimers - About BluWiki