DirectX text Rendering

Source: Internet
Author: User

Text rendering in direct can be done through the WIN32 program framework or by Dxut.

Starting with the first article, the WIN32 framework is very easy to implement, with just a few lines of code to add. The main point is to modify the place.

#pragma comment (lib, "Dxerr.lib")
#pragma comment (lib, "Dxguid.lib")
#pragma comment (lib, "D3dx9d.lib")
#pragma comment (lib, "D3d9.lib")
#pragma comment (lib, "Winmm.lib")
#pragma comment (lib, "Comct132.lib")
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9core.h>

//////////////////////////////////////////////////////////////////////////
D3D base Object;
Lpdirect3d9 g_pd3d = NULL;
D3D Equipment object;
Lpdirect3ddevice9 g_pd3ddevice = NULL;
Font object;
Lpd3dxfont g_pd3dfont=0;
wchar* StrText = L "DirectX9 font display";
Draw area objects;
RECT Clientrect;


//----------------------------//
Initial D3D object;
//----------------------------//
HRESULT Initialized3d (HWND hwndtemp) {
if (null== (G_pd3d=direct3dcreate9 (d3d_sdk_version)))
{
return false;
}
Set the D3dpreame parameter to create the D3D device object;
D3dpresent_parameters D3DPP;
ZeroMemory (&d3dpp,sizeof (D3DPP));
D3DPP. windowed = true;//window mode;
D3DPP. SwapEffect = D3dswapeffect_discard;
D3DPP. Backbufferformat = D3dfmt_unknown;

if (FAILED (G_pd3d->createdevice (d3dadapter_default,d3ddevtype_hal,hwndtemp,d3dcreate_software_ Vertexprocessing,&d3dpp,&g_pd3ddevice)))
{
return false;
}
//Create font object;
if (FAILED (D3dxcreatefont (g_pd3ddevice,0,0,0,0,0,0,0,0,0,l "italic", &g_pd3dfont)))
{
return false;
}
GetClientRect (Hwndtemp,&clientrect);
return true;
}


Rendering Image///////////////////////////////////////////////////////////////////////
VOID Render () {
G_pd3ddevice->clear (0,null,d3dclear_target,d3dcolor_xrgb (45,50,170), 1.0f,0);
if (SUCCEEDED (G_pd3ddevice->beginscene ()))
{//painting text;
G_pd3dfont->drawtext (null,strtext,-1,&clientrect,dt_singleline| dt_noclip| dt_center| DT_VCENTER,0XFFFFFFFF);

G_pd3ddevice->endscene ();
}
G_pd3ddevice->present (Null,null,null,null);
}

End Release Resource///////////////////////////////////////////////////////////////////////
VOID CleanUp () {
   //Release font object;
if (!g_pd3dfont) {
G_pd3dfont->release ();
}
if (!g_pd3ddevice) {
G_pd3ddevice->release ();
}
if (!G_PD3D) {
G_pd3d->release ();
}
}


Handling Message Loops///////////////////////////////////////////////////////////////////////
LRESULT WINAPI Msgproc (HWND hwnd,uint msgid, WPARAM WPARAM, LPARAM LPARAM) {
Switch (msgid) {
Case Wm_destroy:
CleanUp ();
PostQuitMessage (0);
return 0;

Case Wm_keyup:

     //The newly added key response;
if (Wparam==vk_escape)
{//esc key to exit;
CleanUp ();
PostQuitMessage (0);
}
if (wparam==vk_space)
{//esc key to exit;
StrText =l "strike enter";
Render ();
ValidateRect (Hwnd,null);
}
Break

Case WM_PAINT:
Render the scene;
Render ();
ValidateRect (Hwnd,null);
return 0;

}
return DefWindowProc (Hwnd,msgid,wparam,lparam);
}


Create Windows window///////////////////////////////////////////////////////////////////////
INT WINAPI WinMain (hinstance hint,hinstance,lpstr,int) {
Wndclassex wc={sizeof (wndclassex), Cs_classdc,msgproc,0l,0l,getmodulehandle (NULL), null,null,null,null,l " ClassName ", NULL};
RegisterClassEx (&AMP;WC);
HWND hwnd = CreateWindow (L "ClassName", L "D3D game Programming", Ws_overlappedwindow, 200,100,600,500,null,null,wc.hinstance, NULL) ;

if (SUCCEEDED (Initialized3d (HWND)))
{
ShowWindow (Hwnd,sw_showdefault);
UpdateWindow (HWND);

MSG msg;
ZeroMemory (&msg,sizeof (msg));
while (Msg.message!=wm_quit) {
if (PeekMessage (&msg,null,0u,0u,pm_remove))
{
TranslateMessage (&AMP;MSG);
DispatchMessage (&AMP;MSG);
}
Else
{
Render ();
}
}
}
Unregisterclass (L "ClassName", wc.hinstance);
return 0;
}

//////////////////////////////////////////////////////////

By comparing the above code with the first article Directx3d, just add a few lines of code. implementation is not complicated;

There are 2 functions for drawing fonts in DirectX "D3dxcreatefontindirect & D3dxcreatefont" is created using the same font library, which is drawn according to the specified parameters.

We used the D3dxcreatefont, whose function prototype was as follows:

D3dxcreatefont (
Lpdirect3ddevice9 pdevice,//Pointer to IDirect3DDevice9 he will associate with the created Font object;
INT height,//The logical height of the font, our incoming 0 means the default value;
UINT width,//logical widths of the font, 0 also indicates the default value of the font;
UINT Weight,//font weight, generally set to 0;
UINT Miplevels,//mipmap level, font general set 0;
BOOL Italic,//whether italic, 1 is 0 no;
DWORD CharSet,//the character set to which the font belongs, 0 auto-match;
DWORD outputprecision,//font output accuracy;
DWORD quality,//font output quality;
The index number of the DWORD pitchandfamily,//character;
LPCWSTR Pfacename,//String representing the name of the font;
lpd3dxfont* Ppfont); Pointer to an instance of the Id3dxfont object;

In the Initialized3d method above, we completed the setup and obtained the current render region message via GetClientRect. The next step is to draw in the Render method.

In the Msgproc method, we have added 2 New button responses. A ESC exit, a spacebar to change the font content.

DirectX text Rendering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.