Sign in to follow this  

Using ID3DXSprite in ID3DXFont DrawText()

Recommended Posts

utilae    188
Hi, I am using C++ and Direct3D9. I have been using ID3DXFont DrawText() so far to draw text, but now I want to use ID3DXSprite with ID3DXFont by passing an ID3DXSprite object into the DrawText() function. Can someone give me some example code on how to create an ID3DXSprite object and then passing it into DrawText()?

Share this post

Link to post
Share on other sites
MTclip    242
here this should work for you

this is just a function to load a font

// Function for font loading
HRESULT LoadFont(LPD3DXFONT pFont, char* fontName, LPDIRECT3DDEVICE9 pDevice)
// clear if not empty <no memory leaks!!>

// font description struct
D3DXFONT_DESC m_fontDesc;

ZeroMemory(&pFont->m_fontDesc, sizeof(D3DXFONT_DESC));
m_fontDesc.Height = 12;
m_fontDesc.Width = 8;
m_fontDesc.Weight = 500;
m_fontDesc.MipLevels = D3DX_DEFAULT;
m_fontDesc.Italic = false;
m_fontDesc.CharSet = 0;
m_fontDesc.OutputPrecision = 0;
m_fontDesc.Quality = 0;
m_fontDesc.PitchAndFamily = 0;
strcpy(m_fontDesc.FaceName, fontName); // name will be something like "Arial"

return E_FAIL;

return S_OK;

// Other options

And sample of how to load a sprite and use it to draw text

////// DEMO //////////////////

LPDIRECT3DDEVICE9 m_pD3dDevice // <--- previously defined
// Loading <-- only call once
LoadFont( m_pFont, "Arial", m_pD3dDevice);
if(FAILED(D3DXCreateSprite(m_pD3dDevice, &m_pSprite))
return E_FAIL;

// Render Loop
m_pSprite->Begin(); // <--- must be called between LPD3DXSPRITE::Begin()
/// draw the text
pFont->DrawText(m_pSprite, // <-- the sprite
"ALL YOUR BASE ARE BELONG TO ME", // <-- the text
-1, // <-- num characters in string -1 means its null terminated
&rect, // <--- position <limits>
DT_TOP|DT_LEFT, // <--- alignment in the rect
D3DCOLOR_XRGB(0,0,255) ); // Color

m_pSprite->End(); <--- End sprite

Hope that solves your issues

Share this post

Link to post
Share on other sites
utilae    188
Thanks heaps.

Ok, my main goal is to draw text in order of z value. So if a texture had z value of 0.0f and another texture had a z value of 1.0f and the text had a z value of 0.5f, then the text would draw in between the two textures.

I know ID3DXSprite can draw sprites in order of z value, but how do you make ID3DXSprite draw text in order of z value?

Share this post

Link to post
Share on other sites
RenderTarget    398
Exactly the same way. When you call ID3DXSprite::Begin() prior to your font draw calls, pass D3DXSPRITE_SORT_DEPTH_BACKTOFRONT. Sprite will wait until the Flush() or End() call before rendering, and will sort any sprite or font draw calls you made in between.

Share this post

Link to post
Share on other sites
utilae    188
I have done my interface graphics such as buttons and textboxes using DrawPrimitive(). Do I have to draw them with ID3DXSprite instead in order to have sprites and fonts be sorted properly?

How do I specify where in the z order a piece of text appears when I use DrawText()? For example is there a way to go DrawText(0.5f) (simplified function for this example) to make text draw at 0.5f in z order?

Also, if I use ID3DXSprite to draw my buttons and textboxes, how would I do that (what's the draw function, etc)? And would using ID3DXSprite be slower than DrawPrimitive()?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this