Now that we have loaded the .spritefont asset file, and XNA has created a bitmap font in memory after running the code in LoadContent(), the font is available for use. We can use the SpriteFont1 object to print text on the screen using SpriteBatch.DrawString(). Just be sure to always have a matching pair of SpriteBatch.Begin() and SpriteBatch.End() statements around any drawing code. That directive applies to both text and sprites, which we will learn about in Hour 5, “Drawing Bitmaps.”
Run the program by pressing F5. The WP7 emulator comes up, as shown in Figure 3.6.
Figure 3.6 Printing text in the Font Demo program.
The version of SpriteBatch.DrawString() used here is the simplest version of the method, but other overloaded versions of the method are available. An overloaded method is a method such as DrawString() that has two or more different sets of parameters to make it more useful to the programmer. There are actually six versions of DrawString(). Here is an example using the sixth and most complex version. When run, the changes to the text output are dramatic, as shown in Figure 3.7!
float rotation = MathHelper.ToRadians(15.0f); Vector2 origin = Vector2.Zero; Vector2 scale = new Vector2(1.3f, 5.0f); spriteBatch.DrawString(SpriteFont1, text, position, Color.White, rotation, origin, scale, SpriteEffects.None, 0.0f);
Figure 3.7 Experimenting with different DrawString() options.