JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
  Previous Section Next Section

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 Charles Petzold's Programming Windows 95/98. 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 Hllo World

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.

      Previous Section Next Section
    



    JavaScript EditorAjax Editor     JavaScript Editor