龙书笔记(9)

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 );


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值