添加文本比较简单,自己看源码吧,我就不多说了
//-------------------------------------------------------------------------------------- // File: chap13_text_dx9.cpp // // Copyright (c) Microsoft Corporation. All rights reserved. //-------------------------------------------------------------------------------------- #include "DXUT.h" #include "resource.h" #include "SDKmisc.h"//CDXUTTextHelper这个需要 //Global variables ID3DXFont* g_font; ID3DXSprite* g_text_sprite; bool g_show_help = true; #define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0) //-------------------------------------------------------------------------------------- // Rejects any D3D9 devices that aren't acceptable to the app by returning false //-------------------------------------------------------------------------------------- bool CALLBACK IsD3D9DeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext ) { // Typically want to skip back buffer formats that don't support alpha blending IDirect3D9* pD3D = DXUTGetD3D9Object(); if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) ) ) return false; return true; } //-------------------------------------------------------------------------------------- // Before a device is created, modify the device settings as needed //-------------------------------------------------------------------------------------- bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext ) { return true; } //-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { //初始化ID3DXSprite和ID3DXFont对象 D3DXCreateFont(pd3dDevice, 18, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_font); return S_OK; } //-------------------------------------------------------------------------------------- // Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT) // or that are tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { g_font->OnResetDevice(); D3DXCreateSprite(pd3dDevice, &g_text_sprite); // setup view matrix D3DXMATRIX mat_view; D3DXVECTOR3 eye(0.0f, 0.0f, -5.0f); D3DXVECTOR3 at(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&mat_view, &eye, &at, &up); pd3dDevice->SetTransform(D3DTS_VIEW, &mat_view); // set projection matrix D3DXMATRIX mat_proj; float aspect = (float)pBackBufferSurfaceDesc->Width / pBackBufferSurfaceDesc->Height; D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4, aspect, 1.0f, 100.0f); pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat_proj); return S_OK; } //-------------------------------------------------------------------------------------- // Handle updates to the scene. This is called regardless of which D3D API is used //-------------------------------------------------------------------------------------- void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext ) { } void RenderText() { CDXUTTextHelper text_helper(g_font, g_text_sprite, 20); text_helper.Begin(); //show frame and device states text_helper.SetInsertionPos(5, 5); text_helper.SetForegroundColor(D3DXCOLOR(1.0f, 0.475f, 0.0f, 1.0f)); text_helper.DrawTextLine(DXUTGetFrameStats(true)); text_helper.DrawTextLine(DXUTGetDeviceStats());//知识点1 /*知识点1:DXUT统计函数 --------------------------------------------- 来自<<DXUT框架剖析(11)>> --------------------------------------------- DXUTGetFPS 获取当前每秒提交的帧数 DXUTGetFrameStats 获取一个指向字符串的指针,该字符串包括每秒帧数、分辨率、后台缓冲区格式、深度缓冲区格式。 DXUTGetDeviceStats 获取一个指向字符串的指针,该字符串包括当前设备类型、顶点运算行为和设备名。 */ // show other simple information text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f)); text_helper.DrawTextLine(L"Put whatever misc status here"); //show helper information const D3DSURFACE_DESC* surface_desc = DXUTGetD3D9BackBufferSurfaceDesc(); if(g_show_help) { text_helper.SetInsertionPos(10, surface_desc->Height - 15 * 6); text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 0.475f, 0.0f, 1.0f)); text_helper.DrawTextLine(L"Controls(F1 to hide):"); text_helper.SetInsertionPos(40, surface_desc->Height - 15 * 5); text_helper.DrawTextLine(L"Quit: ESC"); } else { text_helper.SetInsertionPos(10, surface_desc->Height - 15 * 4); text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f)); text_helper.DrawTextLine(L"Press F1 for help"); } text_helper.End(); } //-------------------------------------------------------------------------------------- // Render the scene using the D3D9 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { HRESULT hr; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { RenderText(); V( pd3dDevice->EndScene() ); } } //-------------------------------------------------------------------------------------- // Handle messages to the application //-------------------------------------------------------------------------------------- LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext ) { return 0; } //-------------------------------------------------------------------------------------- // Release D3D9 resources created in the OnD3D9ResetDevice callback //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9LostDevice( void* pUserContext ) { g_font->OnLostDevice(); release_com(g_text_sprite); } //-------------------------------------------------------------------------------------- // Release D3D9 resources created in the OnD3D9CreateDevice callback //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9DestroyDevice( void* pUserContext ) { release_com(g_font); } void CALLBACK OnKeyboardProc(UINT character, bool is_key_down, bool is_alt_down, void* user_context) { if(is_key_down) { switch(character) { case VK_F1: g_show_help = !g_show_help; break; } } } //-------------------------------------------------------------------------------------- // Initialize everything and go into a render loop //-------------------------------------------------------------------------------------- INT WINAPI wWinMain( HINSTANCE, HINSTANCE, LPWSTR, int ) { // Enable run-time memory check for debug builds. #if defined(DEBUG) | defined(_DEBUG) _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); #endif // Set the callback functions DXUTSetCallbackD3D9DeviceAcceptable( IsD3D9DeviceAcceptable ); DXUTSetCallbackD3D9DeviceCreated( OnD3D9CreateDevice ); DXUTSetCallbackD3D9DeviceReset( OnD3D9ResetDevice ); DXUTSetCallbackD3D9FrameRender( OnD3D9FrameRender ); DXUTSetCallbackD3D9DeviceLost( OnD3D9LostDevice ); DXUTSetCallbackD3D9DeviceDestroyed( OnD3D9DestroyDevice ); DXUTSetCallbackDeviceChanging( ModifyDeviceSettings ); DXUTSetCallbackMsgProc( MsgProc ); DXUTSetCallbackFrameMove( OnFrameMove ); DXUTSetCallbackKeyboard(OnKeyboardProc); // TODO: Perform any application-level initialization here // Initialize DXUT and create the desired Win32 window and Direct3D device for the application DXUTInit( true, true ); // Parse the command line and show msgboxes DXUTSetHotkeyHandling( true, true, true ); // handle the default hotkeys DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen DXUTCreateWindow( L"chap13_text_dx9" ); DXUTCreateDevice( true, 640, 480 ); // Start the render loop DXUTMainLoop(); // TODO: Perform any application-level cleanup here return DXUTGetExitCode(); }
运行结果如下: