1 //-----------------------------------------------------------------------------
2 // File: XBFont.h
3 //
4 // Desc: Texture-based font class. This class reads .abc font files that are
5 //       generated by the FontMaker tool. These .abc files are used to create
6 //       a texture with all the font's glyph, and also extract information on
7 //       the dimensions of each glyph.
8 //
9 //       Once created, this class is used to render text in a 3D scene with the
10 //       following function:
11 //          DrawText( fScreenY, fScreenSpaceY, dwTextColor, strText,
12 //                    dwJustificationFlags );
13 //
14 //       For performance, you can batch multiple DrawText calls together
15 //       between Begin() and End() calls, as in the following example:
16 //          pFont->Begin();
17 //          pFont->DrawText( ... );
18 //          pFont->DrawText( ... );
19 //          pFont->DrawText( ... );
20 //          pFont->End();
21 //
22 //       The size (extent) of the text can be computed without rendering with
23 //       the following function:
24 //          GetTextExtent( strText, pfReturnedWidth, pfReturnedHeight,
25 //                         bComputeExtentUsingFirstLineOnly );
26 //
27 //       Finally, the font class can create a texture to hold rendered text,
28 //       which is useful for static text that must be rendered for many
29 //       frames, or can even be used within a 3D scene. (For instance, for a
30 //       player's name on a jersey.) Use the following function for this:
31 //          CreateTexture( strText, d3dTextureFormat );
32 //
33 //       See the XDK docs for more information.
34 //
35 // Hist: 11.01.00 - New for November XDK release
36 //       12.15.00 - Changes for December XDK release
37 //       02.18.01 - Changes for March XDK release
38 //       04.15.01 - Using packed resources for May XDK
39 //
40 // Copyright (c) Microsoft Corporation. All rights reserved.
41 //-----------------------------------------------------------------------------
42 #ifndef XBFONT_H
43 #define XBFONT_H
44 #include "XBResource.h"
45 #include "XBUtil.h"
46 
47 
48 
49 
50 //-----------------------------------------------------------------------------
51 // Number of vertex buffers for rendering text. Having this number be greater
52 // than 1 can reduce potential stalling of the GPU.
53 //-----------------------------------------------------------------------------
54 #define NUM_FONT_BUFFERS 2
55 
56 
57 
58 //-----------------------------------------------------------------------------
59 // Flags for the CXBFont::DrawText() function
60 //-----------------------------------------------------------------------------
61 #define XBFONT_LEFT     0x00000000
62 #define XBFONT_RIGHT    0x00000001
63 #define XBFONT_CENTER_X 0x00000002
64 #define XBFONT_CENTER_Y 0x00000004
65 
66 
67 
68 
69 //-----------------------------------------------------------------------------
70 // Custom vertex type for rendering text
71 //-----------------------------------------------------------------------------
72 struct XBFONTVERTEX
73 {
74 	D3DXVECTOR4 p;
75 	DWORD       color;
76 	FLOAT       tu, tv;
77 };
78 
79 #define D3DFVF_XBFONTVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
80 
81 
82 
83 
84 //-----------------------------------------------------------------------------
85 // Name: struct GLYPH_ATTR
86 // Desc: Structure to hold information about one glyph (font character image)
87 //-----------------------------------------------------------------------------
88 struct GLYPH_ATTR
89 {
90 	FLOAT left, top, right, bottom; // Texture coordinates for the image
91 	SHORT wOffset;                  // Pixel offset for glyph start
92 	SHORT wWidth;                   // Pixel width of the glyph
93 	SHORT wAdvance;                 // Pixels to advance after the glyph
94 };
95 
96 
97 
98 
99 //-----------------------------------------------------------------------------
100 // Name: class CXBFont
101 // Desc: Class to implement texture-based font rendering. A .tga image file of
102 //       the pre-rendered font is used to create the texture. A .abc file
103 //       contains information for spacing the font characters (aka glyphs).
104 //-----------------------------------------------------------------------------
105 class CXBFont
106 {
107 public:
108 	// Font and texture dimensions
109 	DWORD         m_dwFontHeight;
110 	DWORD         m_dwTexWidth;
111 	DWORD         m_dwTexHeight;
112 
113 	// Unicode ranges
114 	WCHAR         m_cLowChar;
115 	WCHAR         m_cHighChar;
116 
117 	// Glyph data for the font
118 	DWORD         m_dwNumGlyphs;
119 	GLYPH_ATTR*   m_Glyphs;
120 
121 	// D3D rendering objects
122 	CXBPackedResource       m_xprResource;
123 	LPDIRECT3DDEVICE8       m_pd3dDevice;
124 	LPDIRECT3DTEXTURE8      m_pTexture;
125 	LPDIRECT3DVERTEXBUFFER8 m_pVBs[NUM_FONT_BUFFERS];
126 	LPDIRECT3DVERTEXBUFFER8 m_pVB;
127 	DWORD                   m_dwCurrentBuffer;
128 	XBFONTVERTEX*           m_pVertices;
129 	DWORD                   m_dwNumQuads;
130 	DWORD                   m_dwNestedBeginCount;
131 	BOOL                    m_bTextureFromFile;
132 
133 	// Internal call to trigger rendering of the vertex buffer
134 	HRESULT Render();
135 
136 	// Access functions for debugging purposes
GetTexture()137 	LPDIRECT3DTEXTURE8 GetTexture() const    { return m_pTexture; }
GetFontHeight()138 	DWORD              GetFontHeight() const { return m_dwFontHeight; }
139 
140 public:
141 	// Constructor/destructor
142 	CXBFont();
143 	~CXBFont();
144 
145 	// Functions to create and destroy the internal objects
146 	HRESULT Create( LPDIRECT3DDEVICE8 pd3dDevice,
147 					const CHAR* strFontResourceFileName );
148 	HRESULT Destroy();
149 
150 	// Replaces invalid (outside the valid glyph range) characters in a string
151 	VOID    ReplaceInvalidChars( WCHAR* strUpdate, WCHAR cReplacement ) const;
152 
153 	// Returns the dimensions of a text string
154 	HRESULT GetTextExtent( const WCHAR* strText, FLOAT* pWidth,
155 						   FLOAT* pHeight, BOOL bFirstLineOnly=FALSE ) const;
156 
157 	// Function to create a texture containing rendered text
158 	LPDIRECT3DTEXTURE8 CreateTexture( const WCHAR* strText,
159 									  D3DCOLOR dwBackgroundColor = 0x00000000,
160 									  D3DCOLOR dwTextColor = 0xffffffff,
161 									  D3DFORMAT d3dFormat = D3DFMT_LIN_A8R8G8B8 );
162 
163 	// Public calls to render text. Callers can simply call DrawText(), but for
164 	// performance, they should batch multiple calls together, bracketed by
165 	// calls to Begin() and End().
166 	HRESULT Begin();
167 	HRESULT DrawText( FLOAT sx, FLOAT sy, DWORD dwColor,
168 					  const WCHAR* strText, DWORD dwFlags=0L );
169 	HRESULT End();
170 };
171 
172 
173 
174 
175 #endif
176 
177 
178