Jump to content
  • Advertisement
Sign in to follow this  
Magmatwister

Drawing Text - DirectX9

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

My program crashes now when I try and initialise/render text. Here is all my code: D3DDEV->BeginScene(); g_Font->DrawText(NULL, L"Fail", -1, &FontPosition, DT_LEFT, D3DCOLOR_XRGB(255, 255, 255)); D3DDEV->EndScene(); g_Font = NULL; D3DXCreateFont(D3DDEV, 24, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE, L"Arial", &g_Font); FontPosition.top = 0; FontPosition.bottom = HEIGHT; FontPosition.right = WIDTH; FontPosition.left = 0; Am I doing something wrong? thanks.

Share this post


Link to post
Share on other sites
Advertisement
-Enable the D3D debug mode
-Debug your application when it crashes:
--Look at the output window
--Look at the stack window
---Draw your conclusions about the position and type of the bug and fix it.

Share this post


Link to post
Share on other sites
You can also log in the file to check where it crash:

log( "1" );

peace of code

log( "2" );

peace of code

log( "3" );

peace of code

log( "4" );

if your logfile looks like this:

1
2
3

You know where its crashing. Here you got a function:

VOID CSettings::WriteLog ( PCHAR cString, ... )
{
CHAR cBuffer[512];

va_list valist;
va_start( valist, cString );
_vsnprintf( cBuffer, sizeof( cBuffer ) - strlen( cBuffer ), cString, valist );
va_end( valist );

FILE* oLog_File = fopen( "C:\\logfile.log", "a+" );

fprintf( oLog_File, "%s\n", cBuffer );
fclose( oLog_File );
}

After you logged it give me your crashing line please.

Share this post


Link to post
Share on other sites
Luckily for me my program still had console tied to it as a project, so it was as simple as:

g_Font = NULL;
cout << "Log: Set g_Font to NULL" << endl;
D3DXCreateFont(D3DDEV, 24, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE, TEXT("Arial"), &g_Font);
cout << "Log: Created Font." << endl;
FontPosition.top = 0;
FontPosition.bottom = HEIGHT; // Bottom Right of the screen is the ACTUAL resolution, whereas top left is 0,0.
FontPosition.right = WIDTH; // See above.
FontPosition.left = 0;
cout << "Filled the RECT structure." << endl;
//D3DXCreateFontIndirect(D3DDEV, &FontInfo, &g_Font);


The only thing that happens before the crash is:

Log: Set g_Font to NULL

So I would guess that:

D3DXCreateFont(D3DDEV, 24, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE, TEXT("Arial"), &g_Font);

Is where the problem lies. Help please

Share this post


Link to post
Share on other sites
First, you need to check the return code from your D3DX calls. Every DirectX function gives some indication of success or failure.

Second, your CreateFont and DrawText calls looks fine. Are you sure FontPosition is correct when you draw the text?


Share this post


Link to post
Share on other sites
g_Font = NULL;
cout << "Log: Set g_Font to NULL" << endl;
FontPosition.top = 0;
FontPosition.bottom = HEIGHT; // Bottom Right of the screen is the ACTUAL resolution, whereas top left is 0,0.
FontPosition.right = WIDTH; // See above.
FontPosition.left = 0;
cout << "Filled the RECT structure." << endl;
D3DXCreateFont(D3DDEV, 24, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE, TEXT("Arial"), &g_Font);
cout << "Log: Created Font." << endl;


From the following code, and by using debugging mode for my compiler, It HAS to be the createfont which is causing the crash. It succesfully outputs "Set g_font" and "Filled the RECT structure", before the crash. Could it be TEXT("Arial") which is causing the problem? My knowledge is hazy around LPCSTR AND LPCWSTR etc. As for the font position, it should be, the window is 1280 by 800 at ALL times. But besides that, my program crashes before the window even appears, so it doesn't even get to render.

Share this post


Link to post
Share on other sites
Eeuhm I am not sure but if you have this:

g_Font = NULL; or LPD3DXFONT g_Font = NULL;

Well it must be:

LPD3DXFONT g_Font = NULL;

And also try:

"Arial" and not TEXT( "Arial" )

Maybe it change something.

Share this post


Link to post
Share on other sites
If i change TEXT("Arial") to simply "Arial" then I get this error:

1>c:\users\****\documents\visual studio 2010\projects\l7a7\l7a7\d3dclass.cpp(81): error C2664: 'D3DXCreateFontW' : cannot convert parameter 11 from 'const char [6]' to 'LPCWSTR'

The initialisation and rendering takes place within my D3D class, and as such I have made these two into private variables.

LPD3DXFONT g_Font;
RECT FontPosition;

Share this post


Link to post
Share on other sites
The Debug Runtimes will tell you when a function fails and why it failed. However, for proper code, you should be checking the return value of every single function that creates an interface or returns information you rely on using the SUCCEEDED or FAILED macros.

The TEXT() macro is needed if you want to write code that will build as Unicode or non-Unicode.

The only reason that D3DXCreateFont would crash would be if your device pointer was invalid (Not NULL, but already Release()d or something). Your Debugger will tell you exactly what's wrong.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!