www.pudn.com > sxdl.zip > d3dfont.h


//----------------------------------------------------------------------------- 
// File: D3DFont.h 
// 
// Desc: Texture-based font class 
//----------------------------------------------------------------------------- 
#ifndef D3DFONT_H 
#define D3DFONT_H 
#include  
#include  
 
 
// Font creation flags 
#define D3DFONT_BOLD        0x0001 
#define D3DFONT_ITALIC      0x0002 
#define D3DFONT_ZENABLE     0x0004 
#define D3DFONT_FULLSET     0x0008 
 
// Font rendering flags 
#define D3DFONT_CENTERED_X  0x0001 
#define D3DFONT_CENTERED_Y  0x0002 
#define D3DFONT_TWOSIDED    0x0004 
#define D3DFONT_FILTERED    0x0008 
 
 
//----------------------------------------------------------------------------- 
// Name: class CD3DFont 
// Desc: Texture-based font class for doing text in a 3D scene. 
//----------------------------------------------------------------------------- 
class CD3DFont 
{ 
private :  
    TCHAR   m_strFontName[80];            // Font properties 
    DWORD   m_dwFontHeight; 
    DWORD   m_dwFontFlags; 
 
    DWORD   m_dwTexWidth;                 // Texture dimensions 
    DWORD   m_dwTexHeight; 
    FLOAT   m_fTextScale; 
    FLOAT   m_fTexCoords[256][4]; 
    DWORD   m_dwSpacing;                  // Character pixel spacing per side 
 
protected :  
    // Stateblocks for setting and restoring render states 
    static LPDIRECT3DSTATEBLOCK9 m_pStateBlockSaved; 
    static LPDIRECT3DSTATEBLOCK9 m_pStateBlockDrawText; 
    LPDIRECT3DDEVICE9       m_pd3dDevice; // A D3DDevice used for rendering 
    LPDIRECT3DTEXTURE9      m_pTexture;   // The d3d texture for this font 
    LPDIRECT3DVERTEXBUFFER9 m_pVB;        // VertexBuffer for rendering text 
 
	// Custom vertex types for rendering text 
	enum { MAX_NUM_VERTICES = 60*6 } ;  
 
	struct FONT2DVERTEX { D3DXVECTOR4 p;   DWORD color;     FLOAT tu, tv; }; 
	struct FONT3DVERTEX { D3DXVECTOR3 p;   D3DXVECTOR3 n;   FLOAT tu, tv; }; 
 
	enum { D3DFVF_FONT2DVERTEX = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1 } ;  
	enum { D3DFVF_FONT3DVERTEX = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 } ;  
 
private :  
	inline FONT2DVERTEX InitFont2DVertex( const D3DXVECTOR4& p, D3DCOLOR color, FLOAT tu, FLOAT tv ) 
	{ 
		FONT2DVERTEX v;   v.p = p;   v.color = color;   v.tu = tu;   v.tv = tv; 
		return v; 
	} ;  
 
	inline FONT3DVERTEX InitFont3DVertex( const D3DXVECTOR3& p, const D3DXVECTOR3& n, FLOAT tu, FLOAT tv ) 
	{ 
		FONT3DVERTEX v;   v.p = p;   v.n = n;   v.tu = tu;   v.tv = tv; 
		return v; 
	} ;  
 
	void Render3DText( const TCHAR* strText , DWORD dwFlags = 0L ); 
 
public: 
    // 2D and 3D text drawing functions 
    void DrawText( float x, float y, DWORD dwColor, const TCHAR* strText , DWORD dwFlags = 0L ); 
    void DrawTextScaled( float  x, float y, /* FLOAT z, */ 
                         float fXScale, float fYScale, DWORD dwColor,  
                         const TCHAR* strText , DWORD dwFlags = 0L );     
    // Function to get extent of text 
    HRESULT GetTextExtent( const TCHAR* strText, SIZE* pSize ); 
 
    // Initializing and destroying device-dependent objects 
    HRESULT InitDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice ); 
    HRESULT RestoreDeviceObjects(); 
    HRESULT InvalidateDeviceObjects(); 
    HRESULT DeleteDeviceObjects(); 
 
    // Constructor / destructor 
    CD3DFont( const TCHAR* strFontName, DWORD dwHeight, DWORD dwFlags=0L ); 
    virtual ~CD3DFont(); 
}; 
 
 
 
 
#endif