Windows Phone 7 Game Programming: Printing Text
- Dec 15, 2011
This hour takes the next logical step in programming the WP7 with XNA 4.0. You have learned how to create a new Windows Phone project in Visual C# 2010. We will touch on that subject again in this hour while learning about fonts and text drawing code. There are actually quite a lot of interesting features available to us for working with text and fonts in an XNA program. There are several royalty-free fonts included with XNA, which can each be used to draw text in any font point size with special effects such as italic and bold. We will explore the runtime capabilities of the font system to render text in different colors (including alpha blending), color animation, and even scaling.
What You’ll Learn in This Hour:
- Creating the Font Demo project
- Adding a new font to the Content project
- Learning to use the SpriteFont class
- Printing text
Creating the Font Demo Project
At first glance, the text output capabilities of XNA seem to be pretty straightforward, but with a little know-how and experimentation, we find that XNA offers us some really great features for printing text on the little Windows Phone screen. The code here, as elsewhere in this book, will be the standard “XNA” fare, meaning the code would look the same on other platforms supported by XNA (namely, Windows and Xbox 360). Let’s first get started by creating the project.
A font in XNA is nothing more than a text file—at least, from the programmer’s point of view. When the project is compiled, XNA uses the text file to create a bitmap font on a memory texture and use that for printing text on the screen. This is a time-consuming process, which is why the font is created at program startup rather than while it’s running. Let’s create a new project and add a font to it.