DirectX 9.0 C++ 教程 字体 迟到的helloworld

网友投稿 292 2022-11-30

DirectX 9.0 C++ 教程 字体 迟到的helloworld

效果图:

本文只设计到使用ID3DXFont接口来绘制文本,其他的方面参考​​DirectX 9.0 C++ 教程 第一个程序​​

1.说明

ID3DXFont接口内部使用GDI绘制文本,相应地在性能上会有一点的损失。但是它支持ANSI and Unicode 字符串。

2.创建ID3DXFont接口对象

其实可以用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);

3.绘制文本

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

把2,3步的代码放到BeginScene和EndScene中即可。最后不要忘记清理内存。

4.完整代码

//-----------------------------------------------------------------------------// 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 #include #include #include //-----------------------------------------------------------------------------// Global variables//-----------------------------------------------------------------------------LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDeviceLPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering deviceLPD3DXFONT 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 ​​(本站下载)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:原来酷炫的大屏,用Excel就能做
下一篇:Java入门绊脚石之Override和Overload的区别详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~