The book tells two different ways of displaying text with DirectX. The first way is to use a surface that has all of the letters and digits in it. The program picks out the characters one at a time and draws them on the screen as bitmaps.

Question: What are the advantages and disadvantages of this approach?

The second approach is to use the CZenFont class. You can read about this approach beginning on page 679 under the heading Rendering Real Fonts.

The book does not explain how to use any fonts other than the stock fonts, but you can get other fonts using the GDI and then display them using DirectX and CZenFont.

Here are some lines of sample code from a program I wrote. Be sure that you delete the font objects before you quit from your program.

Variable declarations:
HFONT hFont, spriteHFont;
CZenFont font, spriteFont;
Creating and initializing:
hFont = CreateFont(14,0,0,0,0,0,0,0,0,0,0,0,0,"Courier New");
spriteHFont = CreateFont(16,0,0,0,0,0,0,0,0,0,0,0,0,"Impact");

font.Initialize(hFont, D3DCOLOR_XRGB(0,0,0));
spriteFont.Initialize(spriteHFont, D3DCOLOR_XRGB(255,255,255));
Shutting down:
Displaying text:

You can use the OutputText member function of the CZenFont object to display text. As written, it uses a color passed in to the Initialization function. I wanted to easily be able to write text in other colors, so I overloaded the OutputText function and made a version that takes a color as the third parameter.

Question: How can you set up OutputText so that the original font color isn't lost when you pass in a color as the third parameter?

Console and text commands

A console class is very useful, not only for the finished game, but also during game development. Once you have the console in place, you can use it for text versions of the normal game commands plus additional commands used only for testing and debugging. The console is also useful for simulating network communication, which will usually be text-based.

In my opinion the console class in the book is much more complicated than necessary. For one thing, it doesn't use the C++ string class and instead does everything with arrays of characters. For another thing, the author makes a big deal of animating the console so that it drops down and then goes back up.

These are the things you need to do to set up the console:

Question: How can you handle the backspace key? By the way, the character for the backspace key is '\b'.