D3DX Font object in C++

Started by
4 comments, last by Buckeye 13 years ago

I was wondering when creating a font object using D3DXCreateFont. Is it best to create the object when creating the device and then using this device over and over again. This allows me to create text but dose not allow me to change font, font size or font color. and then release the device when the program ends.

Or is it ok to Create the device and also release it in the create text call and release it in the same call wich allows me to change Font, color and size on the fly?

eg

void DrawText(float X, float Y, float Item)
{
D3DXCreateFont(d3ddev, 12, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET,OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,"Arial", &dxfont);

RECT Part;

//Creats Box for text and decides position
SetRect(&Part, X, Y, 1024, 768);

//Sets a container for the Variable output
static char strText[10];
_itoa_s(Item, strText, 10);

//Draw the text
dxfont->DrawTextA(NULL,(LPCSTR)&strText,strlen((LPCSTR) &strText),&Part,DT_LEFT,D3DCOLOR_ARGB(255, 255, 255, 255));

dxfont->Release();

return;
}

Advertisement
Create the font once at initialization. If you want to draw text with different fonts, simply create more fonts.

Create the font once at initialization. If you want to draw text with different fonts, simply create more fonts.


Thanks, it didn't really come to me to do it that way, i probably wont need very many different fonts either.
// Create a font array
LPD3DXFONT lpD3DXfont[MAX_FONTS] = {NULL};

// then you can create up to MAX_FONTS you like.
for (UINT i = 0; i < MAX_FONTS; i++)
{
D3DXCreateFont(m_pd3dDevice,
nHeight,
0,
nWeight,
1,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
strFace,
&lpD3DXfont ) );

}

Error handling omitted.


Jimmy Chen

// Create a font array
LPD3DXFONT lpD3DXfont[MAX_FONTS] = {NULL};

// then you can create up to MAX_FONTS you like.
for (UINT i = 0; i < MAX_FONTS; i++)
{
D3DXCreateFont(m_pd3dDevice,
nHeight,
0,
nWeight,
1,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
strFace,
&lpD3DXfont ) );

}

Error handling omitted.





Tyvm for this one, ill probably use it if it turns out i need more than a couple of different fonts =)
Don't forget to call font->OnLostDevice() and font->OnResetDevice() when appropriate.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement