Dx文字显示

DxUT 框架里面有个字体的使用,用到D3DXCreateFont函数。

1 D3DXCreateFont(pd3dDevice, 18, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_font);

 

作用说明

D3DXCreateFont创建了一个装置和字体的字体对象。
 

函数原型

 1 HRESULT D3DXCreateFont(
 2 
 3         LPDIRECT3DDEVICE9 pDevice,
 4 
 5         INT Height,
 6 
 7         UINT Width,
 8 
 9         UINT Weight,
10 
11         UINT MipLevels,
12 
13         BOOL Italic,
14 
15         DWORD CharSet,
16 
17         DWORD OutputPrecision,
18 
19         DWORD Quality,
20 
21         DWORD PitchAndFamily,
22 
23         LPCTSTR pFacename,
24 
25         LPD3DXFONT *ppFont);

 

参数说明

pDevice
一种IDirect3DDevice9接口指针,该装置可与字体对象关联。
Height
逻辑单位字符的高度。
Width
逻辑单位字符的宽度。
Weight
字体重量。
MipLevels
mipmap级别数。
Italic
为斜体字体真,否则为假。
CharSet
该字体的字符集。
OutputPrecision
指定了窗口应该尝试匹配所需的字体大小和字体的特点与实际。使用out_tt_only_precis例如,确保你总是TrueType字体。
Quality
指定Windows如何应所需的字体和一个真正的字体匹配。它适用于光栅字体,不应该影响TrueType字体。
PitchAndFamily
Pitch and family index。
pFacename
包含字体名称的字符串。如果编译器设置需要Unicode数据类型,解决lpcwstr LPCTSTR。否则,字符串数据类型解析字符串。
ppFont
返回一个指针指向一个id3dxfont接口,表示所创建的字体对象。

 

字体帮助类

 1 class CDXUTTextHelper
 2 {
 3 public:
 4     CDXUTTextHelper( ID3DXFont* pFont, ID3DXSprite* pSprite, int nLineHeight );
 5 
 6     //设置字体位置
 7     void SetInsertionPos( int x, int y )     { m_pt.x = x; m_pt.y = y; }
 8     //设置字体颜色
 9     void SetForegroundColor( D3DXCOLOR clr ) { m_clr = clr; }
10 
11     //开始绘制文字
12     void Begin();
13     //格式文字换行
14     HRESULT DrawFormattedTextLine( const WCHAR* strMsg, ... );
15     //换行
16     HRESULT DrawTextLine( const WCHAR* strMsg );
17     HRESULT DrawFormattedTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg, ... );
18     HRESULT DrawTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg );
19     void End();
20 
21 protected:
22     ID3DXFont*   m_pFont;
23     ID3DXSprite* m_pSprite;
24     D3DXCOLOR    m_clr;
25     POINT        m_pt;
26     int          m_nLineHeight;
27 };

 

 1 //--------------------------------------------------------------------------------------
 2 
 3 CDXUTTextHelper::CDXUTTextHelper( ID3DXFont* pFont, ID3DXSprite* pSprite, int nLineHeight )
 4 {
 5     m_pFont          = pFont;
 6     m_pSprite      = pSprite;
 7     m_clr          = D3DXCOLOR(1,1,1,1);
 8     m_pt.x          = 0; 
 9     m_pt.y          = 0; 
10     m_nLineHeight = nLineHeight;
11 }
12 
13 //--------------------------------------------------------------------------------------
14 
15 HRESULT CDXUTTextHelper::DrawFormattedTextLine( const WCHAR* strMsg, ... )
16 {
17     WCHAR strBuffer[512];
18     
19     va_list args;
20     va_start(args, strMsg);
21     StringCchVPrintf( strBuffer, 512, strMsg, args );
22     strBuffer[511] = L'\0';
23     va_end(args);
24 
25     return DrawTextLine( strBuffer );
26 }
27 
28 HRESULT CDXUTTextHelper::DrawTextLine( const WCHAR* strMsg )
29 {
30     if( NULL == m_pFont ) 
31         return DXUT_ERR_MSGBOX( L"DrawTextLine", E_INVALIDARG );    
32 
33     RECT rc;
34     SetRect( &rc, m_pt.x, m_pt.y, 0, 0 ); 
35 
36     HRESULT hr = m_pFont->DrawText( m_pSprite, strMsg, -1, &rc, DT_NOCLIP, m_clr );
37 
38     if( FAILED(hr) )
39         return DXTRACE_ERR_MSGBOX( L"DrawText", hr );
40 
41     m_pt.y += m_nLineHeight;
42 
43     return S_OK;
44 }
45 
46 
47 HRESULT CDXUTTextHelper::DrawFormattedTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg, ... )
48 {
49     WCHAR strBuffer[512];
50     
51     va_list args;
52     va_start(args, strMsg);
53     StringCchVPrintf( strBuffer, 512, strMsg, args );
54     strBuffer[511] = L'\0';
55     va_end(args);
56 
57     return DrawTextLine( rc, dwFlags, strBuffer );
58 }
59 
60 
61 HRESULT CDXUTTextHelper::DrawTextLine( RECT &rc, DWORD dwFlags, const WCHAR* strMsg )
62 {
63     if( NULL == m_pFont ) 
64         return DXUT_ERR_MSGBOX( L"DrawTextLine", E_INVALIDARG );
65     
66     HRESULT hr = m_pFont->DrawText( m_pSprite, strMsg, -1, &rc, dwFlags, m_clr );
67 
68     if( FAILED(hr) )
69         return DXTRACE_ERR_MSGBOX( L"DrawText", hr );
70 
71     m_pt.y += m_nLineHeight;
72 
73     return S_OK;
74 }

渲染文本对象就要用到LPD3DXFONT对象的DrawText()函数。

1 INT DrawText(
2         LPD3DXSPRITE pSprite,   // 指向包含字符串的LPD3DXSPRITE对象的指针
3         LPCTSTR pString,        // 屏幕上要显示的文本
4         INT Count,              // 字符串字符的数目
5         LPRECT pRect,           // 一个RECT对象,定义了要显示在屏幕上的第一个字符的起始位置
6         DWORD Format,           // 指定字符串的显示方式
7         D3DCOLOR Color          // 屏幕上显示的文本颜色
8         );

  

■ pSprite:它是指向包含字符串的LPD3DXSPRITE对象的指针,如果需要Direct3D内部构建每个角色的小图形,该值可以设为NULL。

■  pString:屏幕上要显示的文本。

■ Count:字符串字符的数目。

■  pRect:它是一个RECT对象,定义了要显示在屏幕上的第一个字符的起始位置。

■  Format:指定字符串的显示方式。表5.1给出了对该值完整的介绍,这些值可以通过XOR一起使用。

■ Color:屏幕上显示的文本颜色。

表5.1 显示文本时可以使用的Format(格式)值

含义
DT_BOTTOM 将文本调整到定义文本的RECT区域的底部
DT_CALCRECT 计算RECT的高度和宽度。使用pRect参数指定的区域根据需要来调整矩形
DT_CENTER 将文本水平地调整到矩形的中间
DT_EXPANDTABS 拓展字符串中的tab字符。默认情况下tab是8个字符
DT_LEFT 将文本调整到矩形的左侧
DT_NOCLIP 无剪切地绘制文本,这样渲染速度更快
DT_RIGHT 将文本调整到矩形的右侧
DT_RTLREADING 按照从右向左的顺序渲染文本。通常在Hebrew或Arabic字体中使用该值
DT_SINGLELINE 只在一行上显示文本。忽律所有的换行字符
DT_TOP 将文本显示在矩形的顶部
DT_VCENTER 将文本垂直地调整到矩形的中间。只使用单独的一行
DT_WORDBREAK 单词通过定义好的矩形时,将被换行

 

 

 

 

 

 

 

使用例子

 1 void RenderText()
 2 {
 3     CDXUTTextHelper text_helper(g_font, g_text_sprite, 20);
 4     
 5     text_helper.Begin();
 6 
 7     // show frame and device states
 8     text_helper.SetInsertionPos(5, 5);
 9     text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 0.475f, 0.0f, 1.0f) );
10     text_helper.DrawTextLine( DXUTGetFrameStats(true) );
11     text_helper.DrawTextLine( DXUTGetDeviceStats() );
12 
13     // show other simple information
14     text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) );
15     text_helper.DrawTextLine(L"Put whatever misc status here");
16 
17     // show helper information
18     
19     const D3DSURFACE_DESC* surface_desc = DXUTGetBackBufferSurfaceDesc();
20 
21     if(g_show_help)
22     {
23         text_helper.SetInsertionPos(10, surface_desc->Height - 15 * 6);
24         text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 0.475f, 0.0f, 1.0f) );
25         text_helper.DrawTextLine(L"Controls (F1 to hide):");
26         
27         text_helper.SetInsertionPos(40, surface_desc->Height - 15 * 5);
28         text_helper.DrawTextLine(L"Quir: ESC");
29     }
30     else
31     {
32         text_helper.SetInsertionPos(10, surface_desc->Height - 15 * 4);
33         text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) );
34         text_helper.DrawTextLine(L"Press F1 for help");
35     }
36 
37     text_helper.End();
38 }

 

示例下载示例来源

posted @ 2013-07-08 07:25  小角  阅读(418)  评论(0)    收藏  举报