Tricks of the Windows Game Programming Gurus:Windows GDI, Controls, and Last-Minute Gift Ideas

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

### More on Text and Fonts

Working with fonts is an extremely complex subject and not really something that I want to get into. If you want an in-depth treatise on this subject, your best bet is to pick up Petzold’s Programming Windows 95. For products such as games under DirectX, you will in most cases render text yourself with your own font engine. The only time you might want to use GDI to draw text is in a GUI situation or a quick solution to drawing scores or other simple information during development of your game. However, in the end you must create your own font system to get any kind of speed.

To be somewhat complete I want to at least show you how to change fonts for the `DrawText()` and `TextOut()` functions. This is done by selecting a new font object into the current graphics device context just as you would a new pen or brush. Table 4.1 shows a number of font constants, such as `SYSTEM_FIXED_FONT`, which is a monospaced font. Monospaced means that each character is always the same width. Proportional fonts have different spacing. Anyway, to select a new font into the graphics context, you would do this:

    SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));

Whatever GDI text you rendered with `TextOut()` or `DrawText()` is drawn in the new font. If you want a little more power over the selection of fonts, you can use one of the built-in TrueType fonts listed in Table 4.4.

Table 4.4 TrueType Font Typeface Names

Font Typeface String | Example
--- | ---

Courier New | Hello World
Courier New Bold | Hello World
Courier New Italic | Hello World
Courier New Bold Italic | Hello World
Times New Roman | Hello World
Times New Roman Bold | Hello World
Times New Roman Italic | Hello World
Times New Roman Bold Italic | Hello World
Arial | Hello World
Arial Bold | Hello World
Arial Italic | Hello World
Arial Bold Italic | Hello World
Symbol | Hελλο Ωορλδ

To create one of these fonts, you can use the `CreateFont()` function:

    HFONT CreateFont( int nHeight, // logical height of font

          int nWidth,              // logical average character width

          int nEscapement,         // angle of escapement

          int nOrientation,        // base-line orientation angle

          int fnWeight,            // font weight

          DWORD fdwItalic,         // italic attribute flag

          DWORD fdwUnderline,      // underline attribute flag

          DWORD fdwStrikeOut,      // strikeout attribute flag

          DWORD fdwCharSet,        // character set identifier

          DWORD fdwOutputPrecision,// output precision

          DWORD fdwClipPrecision,  // clipping precision

          DWORD fdwQuality,        // output quality

          DWORD fdwPitchAndFamily, // pitch and family

          LPCTSTR lpszFace);       // pointer to typeface name string

                                   // as shown in table 4.4

The explanation of the function is far too long, so take a look at the Win32 SDK Help for details. Basically, you fill in all those ugly parameters and the results are a handle to a rasterized version of the font you requested. Then you can select the font into your device context and you’re ready to rock.

### Timing Is Everything

The next topic we’re going to cover is timing. Although it may seem unimportant, timing is crucial in a video game. Without timing and proper delays a game can run too fast or too slow and the illusion of animation is completely lost.

If you recall, back in Chapter 1, “Journey into the Abyss,” I mentioned that most games run about 30 fps (frames per second), but I never alluded to how to keep this timing constant. In this section you’ll learn some techniques to track time and even send time-based messages. Later in the book you’ll see how these ideas are used over and over to keep frame rate solid and you’ll see how to augment parametric animation and physics on slow systems that can’t sustain high frame rates. First, though, take a look at the `WM_TIMER` message.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.