\\ Home : Articoli : Stampa
Font
Di robydx (del 13/03/2007 @ 23:25:06, in English, linkato 2360 volte)

Let's start to do something of nice, and that will work. I know you would start now with 3D but before we have to start form 2D and we, now, will talk about fonts: how to write on the screen.

In DirectX10 (like in 9) the 2D graphics does not exists. Text and sprites (i'll talk about it nextly), are function written by Microsoft that moves some polygons with Z component = 0, in which it's interted images or text.

The using it's simple. Let's start with objects declaration:

ID3DX10Font* font;

HRESULT hr=D3DX10CreateFontA(device, 24, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial", & font);

First parameter it's device, the second the height (not the classic that you can see in Word, but in logic units), and width (if you set this parameter to 0, it will choose the right one).

There is weight and the text's depth,

FW_BLACK

FW_BOLD

FW_EXTRABOLD

FW_EXTRALIGHT

FW_HEAVY

There are other also, just take a look in WinAPI documentation.

There are the miplevels number (for now use 1, but i'll write entire articles about this concept) and a boolean value that specifies if use or not the cursive text.

There is charset, the precision type and quality (in this example, Antialiased option will draw the text more clear) and the impostation for pitch and family font (all this parameters are part of WinAPI library, nothing about DirectX.) At last, the address of interface that will contain the created font. If HRESULT will be = to S_OK then all it's gone ok.

Now we can use text to write:

font->DrawText(NULL,text,-1,&rectangle,DT_LEFT,D3DXCOLOR(1,1,1,1));

First parameter it's Sprite that i'll explain it in next tutorial. Using NULL D3D10 will use a normal rendering, but with a sprite we will be able to rotate and deform text, as a 3D object. The text parameter it's a string (const char* or LPCSTR that is a typedef). This will be the text drawed. The next parameter it's the width of text to print (-1 means all text). Rectangle it's a pointer to RECT structure and rapresent the zone in which will be inserted the text. The next parameter it's very important, and specifies how to draw text. Here are some (you can use more of this using the |operator)

DT_BOTTOM : used with DT_SINGLELINE, dress text in bottom

DT_CALCRECT : it will not write nothing but will compute the rect in which you will draw.

DT_CENTER : it will center text orizzontaly

DT_EXPANDTABS : expandes tabs

DT_LEFT : dress to left

DT_NOCLIP : Text won't be cutted. Performances will be increased.

DT_RIGHT : dress to right

DT_RTLREADING : show text in inverse side for nations (like Iraq :D) that use a inverse direction

DT_SINGLELINE : force text in a single line

DT_TOP : dress in up

DT_VCENTER : it will center text vertically (valid only in a single line)

DT_WORDBREAK :it will start a new line when text will go over rect.

A least the color. D3DXCOLOR will give you the color using red, green blu and alpha value. If you will use a lower value text will be semiInvisible. With 0 value it will be completely invisible.

Some other things on font: you can gain from font a DESC structure.

D3DX10_FONT_DESC desc;

font->GetDesc(&desc);

desc will contain all informations about text.

Optimizing text

When you render text, device generates images about it, but you can help DirectX by preload images, with this code.

font->PreloadCharacters(begin,end);

This istruction will mantain in memory the character and optimizing performance.

font->PreloadText("TEXT",-1);

This will preload a text. If you will write each time the same text, performance will be increased.

All istruction can be used with ASCII or UNICODE. For this, all function are in 2 versions: A and W.

Choose the right.

A special thanks goes to Vincent who translate this lesson.

Code