Windows Phone 7 development

I was wondering how to use some cool font in my XNA game. I came up with an idea to draw white and black text with an offset to give it “3d” look.

Here is the result:

Image

I made extension methods for SpriteBatch  that write this kind of text

public static class ExtensionMethods   
{       /// <summary>
        /// Draws white text with a black stroke
        /// </summary>
        public static void DrawStringBlackAndWhite(this SpriteBatch spriteBatch, SpriteFont smallFont, String text, Vector2 position)
        {
            spriteBatch.DrawStringWithStroke(smallFont, text, position, Color.White, Color.Black);
        }

        /// <summary>
        /// Draws white text with a black stroke
        /// </summary>
        public static void DrawStringBlackAndWhite(this SpriteBatch spriteBatch, SpriteFont smallFont, StringBuilder text, Vector2 position)
        {
            spriteBatch.DrawStringWithStroke(smallFont, text, position, Color.White, Color.Black);
        }

		/// <summary>
		/// Draws string with a stroke on up and right side of text
		/// </summary>
		public static void DrawStringWithStroke(this SpriteBatch spriteBatch, SpriteFont smallFont, String text, Vector2 position, Color color, Color stroke)
		{
			spriteBatch.DrawString(smallFont, text, position, stroke);
			spriteBatch.DrawString(smallFont, text, new Vector2(position.X - 1, position.Y + 1), color);
		}

		/// <summary>
		/// Draws string with a stroke on up and right side of text
		/// </summary>
		public static void DrawStringWithStroke(this SpriteBatch spriteBatch, SpriteFont smallFont, StringBuilder text, Vector2 position, Color color, Color stroke)
		{
			spriteBatch.DrawString(smallFont, text, position, stroke);
			spriteBatch.DrawString(smallFont, text, new Vector2(position.X - 1, position.Y + 1), color);
		}
	}

About these ads

Comments on: "Drawin cool simple text in XNA" (2)

  1. TJHeuvel said:

    This doubles the amount of draw calls, why not just make a font with a shadow in it?

    • Cause making bitmap font is extra work for the graphics designer and you could have issue with license (can use it, but cannot modify). This kind of text is shown in menus, so the performance is not a big issue.

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: