代码改变世界

Direct3D 9学习笔记(9)文本绘制

2012-08-10 13:26  Clingingboy  阅读(643)  评论(0编辑  收藏  举报

 

一.使用ID3DXFont接口

image

创建一个ID3DXFont对象

D3DXFONT_DESC df;
ZeroMemory(&df, sizeof(D3DXFONT_DESC));

df.Height         = 25;    // in logical units
df.Width          = 12;    // in logical units   
df.Weight         = 500;   // boldness, range 0(light) - 1000(bold)
df.Italic         = false;     
df.CharSet        = DEFAULT_CHARSET;
df.OutputPrecision   = 0;                     
df.Quality        = 0;           
df.PitchAndFamily = 0;           
strcpy(df.FaceName, "Times New Roman"); // font style

//
// Create an ID3DXFont based on 'lf'.
//

if(FAILED(D3DXCreateFontIndirect(Device, &df, &Font)))
{
}

二.使用ID3DXFont绘制文本

image

        RECT rect = {0, 0, Width, Height};
        Font->DrawText(NULL,
            FPSString, 
            -1, // size of string or -1 indicates null terminating string
            &rect,            // rectangle text is to be formatted to in windows coords
            DT_TOP | DT_LEFT, // draw in the top left corner of the viewport
            0xffff0000);      // black text

三.D3DXCreateText

Creates a mesh containing the specified text, using the font associated with the device context.

//
// Describe the font we want.
//

LOGFONT lf;
ZeroMemory(&lf, sizeof(LOGFONT));

lf.lfHeight         = 25;    // in logical units
lf.lfWidth          = 12;    // in logical units
lf.lfEscapement     = 0;        
lf.lfOrientation    = 0;     
lf.lfWeight         = 500;   // boldness, range 0(light) - 1000(bold)
lf.lfItalic         = false;   
lf.lfUnderline      = false;    
lf.lfStrikeOut      = false;    
lf.lfCharSet        = DEFAULT_CHARSET;
lf.lfOutPrecision   = 0;              
lf.lfClipPrecision  = 0;          
lf.lfQuality        = 0;           
lf.lfPitchAndFamily = 0;    
strcpy(lf.lfFaceName, "Times New Roman"); // font style

//
// Create the font and select it with the device context.
//
hFont = CreateFontIndirect(&lf);
hFontOld = (HFONT)SelectObject(hdc, hFont); 

//
// Create the text mesh based on the selected font in the HDC.
//
D3DXCreateText(Device, hdc, "Direct3D", 
    0.001f, 0.4f, &Text, 0, 0);

其会返回一个ID3DXMesh,然后调用DrawSubset方法

Device->SetMaterial(&d3d::BLUE_MTRL);
Text->DrawSubset(0);

image