chap 9 字体
生成和输出文本的3种方式:ID3DXFont, CD3DFont, D3DXCreateText
1.ID3DXFont接口
能处理一些复杂的字体和格式,但是速度略慢
//创建ID3DXFont对象
D3DXFONT_DESC df;
ZeroMemory(&df, sizeof(D3DXFONT_DESC));
df.Height = 25;
df.Width = 12;
df.Weight = 500;
df.MipLevels = D3DX_DEFAULT;
df.Italic = false;
df.CharSet = DEFAULT_CHARSET;
df.OutputPrecision = 0;
df.Quality = 0;
df.PitchAndFamily = 0;
strcpy(df.FaceName, "Times New Roman");
ID3DXFont * font = 0;
D3DXCreateFontIndirect(Device, &df, &font);
//用DrawText绘制
Font->DrawText(NULL, "Hello, world!", -1, &rect, DT_TOP | DT_LEFT, 0xff000000);
/*
注意:在这个例子的代码里面,有两个问题:
(1)D3DXCreateFontIndirect不能正常使用,具体原因不明,但是可以用他的”兄弟“版本:D3DXCreateFont,
这个版本直接把所有参数添加进来,而不像D3DXCreateFontIndirect要借用一个D3DXFONT_DESC结构.
eg:
ID3DXFont * Font = 0;
D3DXCreateFont(Device,25,12,FW_BOLD,1,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,TEXT("System"),&Font);
Font->DrawText(NULL, FPRSString, -1, &rect, DT_TOP | DT_LEFT, 0xffff0000);
(2)在display里面DrawText方法使用的时候少了一个NULL的参数,所以老是报错
在修改了这两个位置后,程序才能正常运行...
*/
2.计算每秒绘制的帧数
DWORD FrameCnt = 0;
float TimeElapsed = 0;
float FPS = 0;
void CalcFPS(float timeDelta)
{
FrameCnt++;
TimeElapsed += timeDelta;
if(TimeElapsed >= 1.0f)
{
FPS = (float)FrameCnt/TimeElapsed;
TimeElapsed = 0.0f;
FrameCnt = 0;
}
}
3.CD3DFont类
速度块,但是处理的字体较简单
//创建
Font = new CD3DFont("Times News Roman", 16, 0);
Font->InitDeviceObject(Device);
Font->RestoreDeviceObjects();
//绘制
Font->DrawText(20,20,0xff000000, "Hello World!");
//清理(这部分写在了Cleanup中)
Font->InvalidateDeviceObjects();
Font->DeleteDeviceObjects();
delete Font;
//要使用这个方法必须用一个已定义好的CD3DFont类
4.D3DXCreateText函数
创建文字网格
HDC hdc = CreateCompatibleDC(0);
HFONT hFont;
HFONT hFontOld;
LOGFONT lf;
ZeroMemory(&lf, sizeof(LOGFONT));
lf.lfHeight = 25;
lf.lfWidth = 12;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 500;
lf.lfItalic = false;
lf.lfUnderline = false;
lf.lfStrikeOut = false;
lf.lfCharSet = DEFAlUT_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);
// Restore the old font and free the acquired HDC.
SelectObject(hdc, hFontOld);
DeleteObject( hFont );
DeleteDC( hdc );