效果图:
本文只设计到使用ID3DXFont接口来绘制文本,其他的方面参考DirectX 9.0 C++ 教程 第一个程序
ID3DXFont接口内部使用GDI绘制文本,相应地在性能上会有一点的损失。但是它支持ANSI and Unicode 字符串。
其实可以用D3DXCreateFont和D3DXCreateFontIndirect来创建,本文只设计到D3DXCreateFontIndirect,D3DXCreateFont可以参考SDK。
//D3DXCreateFontIndirect原型,发现需要D3DXFONT_DESC指针可以参考下面的原型 HRESULT WINAPI D3DXCreateFontIndirect( LPDIRECT3DDEVICE9 pDevice,//创建的Device CONST D3DXFONT_DESC *pDesc, LPD3DXFONT *ppFont //返回的指针 ); //D3DXFONT_DESC 结构体原型 typedef struct D3DXFONT_DESC { INT Height; //高 UINT Width; //宽 UINT Weight; //多粗 UINT MipLevels; BOOL Italic; //是否斜体 BYTE CharSet; BYTE OutputPrecision; BYTE Quality; BYTE PitchAndFamily; TCHAR FaceName[LF_FACESIZE]; //文本格式,如"Times New Roman" } D3DXFONT_DESC; //具体例子 // 填充D3DXFONT_DESC结构体 D3DXFONT_DESC df; ZeroMemory(&df,sizeof(D3DXFONT_DESC)); df.Height = 25; df.Width = 12; df.Weight = 100; df.MipLevels = D3DX_DEFAULT; df.Italic = false; df.CharSet = DEFAULT_CHARSET; df.Quality = 0; df.PitchAndFamily = 0; strcpy(df.FaceName,"Times New Roman"); //创建ID3DXFont 接口对象 D3DXCreateFontIndirect(g_pd3dDevice,&df,&g_font);
INT DrawText( LPD3DXSPRITE pSprite, //null LPCTSTR pString, //要绘制的文字 INT Count, //设为-1 null-terminated string LPRECT pRect, //矩形 DWORD Format, //位置 D3DCOLOR Color //颜色 ); //具体例子 //居中绘制文本 RECT g_FontPosition = {0, 0, 300, 300}; g_font->DrawText(NULL,"Hello World",-1,&g_FontPosition, DT_CENTER|DT_VCENTER,D3DCOLOR_XRGB(255,255,255));
//----------------------------------------------------------------------------- // File: CreateDevice.cpp // // Desc: This is the first tutorial for using Direct3D. In this tutorial, all // we are doing is creating a Direct3D device and using it to clear the // window. // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #include <Windows.h> #include <mmsystem.h> #include <d3dx9.h> #include <string> //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device LPD3DXFONT g_font = NULL; //----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; //Fill out the D3DPRESENT_PARAMETERS structure. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create the Direct3D device. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Device state would normally be set here return S_OK; } //----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if( g_pd3dDevice != NULL) g_pd3dDevice->Release(); if( g_pD3D != NULL) g_pD3D->Release(); if(g_font != NULL) g_font->Release(); } //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { if( NULL == g_pd3dDevice ) return; // Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // 填充D3DXFONT_DESC结构体 D3DXFONT_DESC df; ZeroMemory(&df,sizeof(D3DXFONT_DESC)); df.Height = 25; df.Width = 12; df.Weight = 100; df.MipLevels = D3DX_DEFAULT; df.Italic = false; df.CharSet = DEFAULT_CHARSET; df.Quality = 0; df.PitchAndFamily = 0; strcpy(df.FaceName,"Times New Roman"); //创建ID3DXFont 接口对象 D3DXCreateFontIndirect(g_pd3dDevice,&df,&g_font); //居中绘制文本 RECT g_FontPosition = {0, 0, 300, 300}; g_font->DrawText(NULL,"Hello World",-1,&g_FontPosition, DT_CENTER|DT_VCENTER,D3DCOLOR_XRGB(255,255,255)); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: The window's message handler //----------------------------------------------------------------------------- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; case WM_PAINT: Render(); ValidateRect( hWnd, NULL ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } //----------------------------------------------------------------------------- // Name: WinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "D3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } UnregisterClass( "D3D Tutorial", wc.hInstance ); return 0; }
项目下载:ID3DXFont (本站下载)
文章原地址http://www.waitingfy.com/?p=264
相关文章:
1.DirectX 9.0 C++ 教程 开发环境设定
2.DirectX 9.0 C++ 教程 第一个程序
3.DirectX 9.0 C++ 教程 绘制三角形
4.DirectX 9.0 C++ 教程 关于Perspective projection,Matrices,摄像头,旋转5.DirectX 9.0 C++ 教程 光照
6.DirectX 9.0 C++ 教程 Texture 纹理映射7.DirectX 9.0 C++ 教程 字体 迟到的helloworld
8.DirectX 9.0 C++ 教程 使用3ds max 创建的模型 xfile,mesh