以文本方式查看主题 - 计算机科学论坛 (http://bbs.xml.org.cn/index.asp) -- 『 C/C++编程思想 』 (http://bbs.xml.org.cn/list.asp?boardid=61) ---- Direct3D中的字体与文本显示 (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=69826) |
-- 作者:卷积内核 -- 发布时间:11/25/2008 3:27:00 PM -- Direct3D中的字体与文本显示 图形系统中为了获得当前运行程序的相关信息,往往需要在屏幕上显示文本,Direct3D的功能扩展接口ID3DXFont对此提供了方便的解决方法。
创建ID3DXFont对象 使用接口ID3DXFont绘制文本,首先需要通过函数D3DXCreateFont()创建ID3DXFont字体对象。ID3DXFont接口封装了Windows字体和Direct3D设备指针,D3DXCreateFont()函数通过Windows字体和Direct3D设备指针创建ID3DXFont对象,该函数的声明如下: Creates a font object for a device and font. HRESULT D3DXCreateFont( LPDIRECT3DDEVICE9 pDevice, INT Height, UINT Width, UINT Weight, UINT MipLevels, BOOL Italic, DWORD CharSet, DWORD OutputPrecision, DWORD Quality, DWORD PitchAndFamily, LPCTSTR pFacename, LPD3DXFONT * ppFont); Remarks The compiler setting also determines the function version. If Unicode is defined, the function call resolves to D3DXCreateFontW. Otherwise, the function call resolves to D3DXCreateFontA because ANSI strings are being used. If you want more information about font parameters, see The Logical Font. 示例代码如下: D3DXCreateFont(g_device, 50, 20, 20, 0, FALSE, DEFAULT_CHARSET, 0, 0, 0, "Arial", &g_font);
|
-- 作者:卷积内核 -- 发布时间:11/25/2008 3:27:00 PM -- 使用ID3DXFont对象绘制二维文本 创建了ID3DXFont对象后,就可以使用其接口函数ID3DXFont::DrawText()在指定位置绘制二维文本,该函数支持ANSI和双字节字符串,声明如下: Draws formatted text. This method supports ANSI and Unicode strings. INT DrawText( LPD3DXSPRITE pSprite, LPCTSTR pString, INT Count, LPRECT pRect, DWORD Format, D3DCOLOR Color); Remarks This method supports both ANSI and Unicode strings. This method must be called inside a IDirect3DDevice9::BeginScene ... IDirect3DDevice9::EndScene block. The only exception is when an application calls ID3DXFont::DrawText with DT_CALCRECT to calculate the size of a given block of text. Unless the DT_NOCLIP format is used, this method clips the text so that it does not appear outside the specified rectangle. All formatting is assumed to have multiple lines unless the DT_SINGLELINE format is specified. If the selected font is too large for the rectangle, this method does not attempt to substitute a smaller font. This method supports only fonts whose escapement and orientation are both zero. 示例代码如下: g_device->BeginScene(); DWORD format = DT_SINGLELINE | DT_NOCLIP | DT_CENTER | DT_VCENTER; g_device->EndScene();
|
-- 作者:卷积内核 -- 发布时间:11/25/2008 3:28:00 PM -- ID3DXFont其他相关接口函数 函数ID3DXFont::GetDevice()能够获得与ID3DXFont相关联的Direct3D设备指针,该函数声明如下: Retrieves the Direct3D device associated with the font object. HRESULT GetDevice( LPDIRECT3DDEVICE9 * ppDevice); Remarks
示例截图:
源程序: #pragma warning(disable : 4127) #define CLASS_NAME "GameApp" #define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0) IDirect3D9* g_d3d; RECT g_client_rect; bool init_d3d(HWND hwnd) if(g_d3d == NULL) D3DPRESENT_PARAMETERS d3dpp; d3dpp.Windowed = TRUE; if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, /* D3DXCreateFont(g_device, 50, 20, 20, 0, FALSE, DEFAULT_CHARSET, 0, 0, 0, "Arial", &g_font); return true; void cleanup() void render() g_device->BeginScene(); DWORD format = DT_SINGLELINE | DT_NOCLIP | DT_CENTER | DT_VCENTER; g_device->EndScene(); g_device->Present(NULL, NULL, NULL, NULL); LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) case WM_DESTROY: return DefWindowProc(hwnd, msg, wParam, lParam); int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR, INT) wc.cbSize = sizeof(WNDCLASSEX); if(! RegisterClassEx(&wc)) HWND hwnd = CreateWindow(CLASS_NAME, "Direct3D App", WS_OVERLAPPEDWINDOW, 200, 100, 600, 500, if(hwnd == NULL) if(init_d3d(hwnd)) MSG msg; while(msg.message != WM_QUIT) cleanup(); UnregisterClass(CLASS_NAME, wc.hInstance); return 0; |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
78.125ms |