Sunday 9 December 2007

How to print text in directx

First, you need to create a RECT object to set the area on screen, ID3DXFont object, and a D3DCOLOR object:

RECT lineRect;
ID3DXFont * pDefaultFont;
D3DCOLOR defaultFontColor;


This is how to initialise them:

A rectangle which has a height of 50px, on top of screen:
lineRect.left = 0;
lineRect.top = 0;
lineRect.right = 1024;
lineRect.bottom = 50;


How to create a font
D3DXCreateFont(Device, 30/*fontHeight*/,
0, FW_BOLD, 0, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
TEXT("Calibri"), &pDefaultFont );


I'm not going to deal with the details of D3DXCreateFont method, but you can find the reference here:
http://msdn2.microsoft.com/en-us/library/bb172773.aspx

How to set the color:
defaultFontColor = D3DCOLOR_ARGB(255, 255, 255, 255);

Now you can write your text:
pDefaultFont->DrawTextA(NULL, "My Text Here", -1, &lineRect, DT_RIGHT, defaultFontColor);

Sometimes I strongly miss the VB6 times:
Print "my text here"
But of course, they're "incomparable" in terms of speed :)

2 comments:

Kaya Oğuz said...

wow! so can i write "anything" on screen with these commands? :)

if the rectangle is not wide enough, it doesn't wrap the text. i had this little rectangle which did not show my whole sentence which was a char array and i kept on blaming the char array for not being copied properly. it was the narrow rectangle...

Görkem PAÇACI said...

:)

I use the D3DDISPLAYMODE struct to get information about the screen. And use them to create my rectangles :))

D3DDISPLAYMODE displayMode;
// set the global displaymode struct
Device->GetDisplayMode(0, &displayMode);
// initialize the line and font heights
lineHeight = (float)displayMode.Height/30;
fontHeight = (int)(lineHeight);