The website of Lei Chen

Sunshine and rain of a developer

Archive for July, 2009

DirectX 10 DrawText Disable Depth Buffer. Here’s how to fix it.

Posted by hide1713 on July 6, 2009

DrawText() is a bitch. It changes device state silently.  You have to use a spirit to save the states before you draw it and reset depth buffer after you draw the text.


void cGraphicsLayer::DrawTextString(int x, int y,
 D3DXCOLOR color, const TCHAR* strOutput)
{
 m_pFontSprite->Begin(D3DX10_SPRITE_SAVE_STATE);
 RECT rect = {x, y, m_rcScreenRect.right, m_rcScreenRect.bottom};

 m_pFont->DrawText(m_pFontSprite, strOutput, -1, &rect, DT_LEFT|DT_NOCLIP , color);
 m_pFontSprite->End();
}

Here’s how to recover the depth buffer. You need to keep your DepthStencilState and set it with OMSetDepthStencilState


void cGraphicsLayer::ResetDepthBuffer()
{
 m_pDevice->OMSetDepthStencilState(m_pDepthStencilState, 1);
}

Good luck

Posted in Uncategorized | Leave a Comment »