Sign in to follow this  
Magmatwister

Drawing Text - DirectX9

Recommended Posts

Magmatwister    109
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
snake5    1587
-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
SystemFiles    100
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
Magmatwister    109
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
Buckeye    10747
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
Magmatwister    109
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
SystemFiles    100
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
Magmatwister    109
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
Evil Steve    2017
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
Magmatwister    109
Quote:
Original post by Evil Steve
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.


Thank you! You were right! Well, sort of, what was happening was I was calling the initialise font function BEFORE I had set up directx properly.

I was doing:

InitialiseFonts()
WIDTH = WIN_WIDTH;
HEIGHT = WIN_HEIGHT;
WIN_STATE = WINDOWED;

D3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS PARAMS;
ZeroMemory(&PARAMS, sizeof(PARAMS));
PARAMS.BackBufferHeight = WIN_HEIGHT;
PARAMS.BackBufferWidth = WIN_WIDTH;

// etc etc etc etc etc

Instead of:

D3DDEV->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
InitialiseFonts();

(The last thing I do in setting up direct x is setting the render states)

Thus when I tried to use the directx device (D3DDEV) when creating my font, It failed because it had not yet been filled. Thanks for your help, I will try to use the debugger in my future problems.

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