Drawing Text - DirectX9

Started by
9 comments, last by Magmatwister 14 years, 1 month ago
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.
Currently trying to make a planet renderer. After many hours of work, somehow I know It'll never be complete.
Also, If I help you, please give me an ++
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.
How would I set up D3D debug mode?
Currently trying to make a planet renderer. After many hours of work, somehow I know It'll never be complete.
Also, If I help you, please give me an ++
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.

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

Currently trying to make a planet renderer. After many hours of work, somehow I know It'll never be complete.
Also, If I help you, please give me an ++
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?


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.
Currently trying to make a planet renderer. After many hours of work, somehow I know It'll never be complete.
Also, If I help you, please give me an ++
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.
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;
Currently trying to make a planet renderer. After many hours of work, somehow I know It'll never be complete.
Also, If I help you, please give me an ++
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.

This topic is closed to new replies.

Advertisement