Sign in to follow this  
markwilliams08

DirectX DrawText Stops Working

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:

[code]
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);
}
[/code]

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.

[code]
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();
}
[/code]

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 this post


Link to post
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 [url="http://archive.gamedev.net/community/forums/mod/journal/journal.asp?jn=300672&reply_id=3123284"]Debug Runtime[/url]? Does it provide any information?

Do you check the status of the device (such as calling TestCooperativeLevel) [i]each time[/i] 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 [i]not[/i] called, it may not render properly. Your second set of code will work because you create the font [i]after[/i] 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.

[quote]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?

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