# DirectX DrawText Stops Working

This topic is 2680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all,

I have a strange situation with DrawText and was hoping someone here knows what is going on. I am able to draw text onto the screen, but after about 50 more calls to DrawText it stops drawing text to the screen. Here is the code that stops working after a couple of seconds:

 class TestDraw { public: TestDraw(LPDIRECT3DDEVICE9 device); ~TestDraw(); virtual void Text(); private: TestDraw(); LPD3DXFONT m_font; D3DCOLOR fontColor; }; TestDraw::TestDraw(LPDIRECT3DDEVICE9 device) { D3DXCreateFont(device, 12, 0, FW_NORMAL, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &m_font); fontColor = D3DCOLOR_ARGB(255,0,0,0); } TestDraw::~TestDraw() { m_font->Release(); } TestDraw::Text() { RECT rct; rct.left=2; rct.right=780; rct.top=10; rct.bottom=rct.top+20; m_font->DrawText(NULL, "Hello World", -1, &rct, 0, fontColor); } 

I have noticed that the following does work, but it requires me to create and release the font every time which I would rather not do.

 class TestDraw { public: TestDraw(LPDIRECT3DDEVICE9 device); ~TestDraw(); virtual void Text(); private: TestDraw(); LPDIRECT3DDEVICE9 d3ddev; LPD3DXFONT m_font; D3DCOLOR fontColor; }; TestDraw::TestDraw(LPDIRECT3DDEVICE9 device) { d3ddev = device; } TestDraw::~TestDraw() { } TestDraw::Text() { D3DXCreateFont(d3ddev, 12, 0, FW_NORMAL, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &m_font); fontColor = D3DCOLOR_ARGB(255,0,0,0); RECT rct; rct.left=2; rct.right=780; rct.top=10; rct.bottom=rct.top+20; m_font->DrawText(NULL, "Hello World", -1, &rct, 0, fontColor); m_font->Release(); } 

Any idea what is going on here?

EDIT: I've found that d3ddev->Clear is clearing the text from the screen and that is why I can not see it. I'm still confused why this is because I am calling the following: Clear, BeginScene, DrawText, EndScene. Clear screen should not be covering my text after it is written.

Thanks,
Mark

##### Share on other sites
First, it doesn't appear you check for errors anywhere. Every DirectX function provides some indication of success or failure which you need to check and take appropriate action.

Second, have you enabled the Debug Runtime? Does it provide any information?

Do you check the status of the device (such as calling TestCooperativeLevel) each time through your rendering loop? If not, you need to do that and implement routines for OnLostDevice and OnResetDevice.

If the device needs to be reset, do you call font->OnLostDevice, and after the device is reset call font->OnResetDevice?

If the device is reset and font->OnResetDevice is not called, it may not render properly. Your second set of code will work because you create the font after the device has been reset, which is fine. But it will certainly be more efficient to create the font once and just call font->OnLostDevice and ->OnResetDevice appropriately.

Clear screen should not be covering my text after it is written.[/quote]
That's what clear does. It doesn't "cover" anything, it sets the entire color buffer to the color you specify, "erasing" whatever was previously drawn to the buffer (which includes your text). It will clear the screen from the previous pass through the rendering loop. Then you draw the text again and it gets presented. Clear/Draw/Present. That's the cycle. Do you have a device->Present after EndScene?

Also, how do you handle WM_PAINT and WM_ERASEBKGND messages in your window procedure? Are they correct?

• 11
• 18
• 12
• 24
• 9
• ### Forum Statistics

• Total Topics
631397
• Total Posts
2999825
×