ID3DXFont problem

Started by
9 comments, last by CodeMaster Rapture 18 years, 8 months ago
I am trying to use ID3DXFont. The code gets compiled but when I run the build it crashes instantly. Here is the code I use


//1. Create a pointer at global scope:
 
ID3DXFont *g_font = NULL;

// 2. call  D3DXCreateFont:

        HRESULT rslt=0;

	rslt=D3DXCreateFont(g_pD3D,     //D3D Device
                     22,               //Font height
                     0,                //Font width
                     FW_NORMAL,        //Font Weight
                     1,                //MipLevels
                     false,            //Italic
                     DEFAULT_CHARSET,  //CharSet
                     OUT_DEFAULT_PRECIS, //OutputPrecision
                     ANTIALIASED_QUALITY, //Quality
                     DEFAULT_PITCH|FF_DONTCARE,//PitchAndFamily
                     "Arial",          //pFacename,
              &g_font);
    
// 3. Release the interface in GameShutDown function;

 if(g_font){
      g_font->Release();
      g_font=NULL;
   }

// 4. Call DrawText in my Render() function:


   RECT fontRect;
   SetRect (&fontRect, 50,50,70,70);
   
   char * text = "hello";
   
   g_font->DrawText(NULL,        //pSprite
                                text,  //pString
                                -1,          //Count
                                &fontRect,  //pRect
                                DT_LEFT|DT_NOCLIP,//Format,
                                0xFFFFFFFF); //Color
    


Can someone point me what`s wrong in this code? One more thing the debugger traks down the problem to this line: 0xFFFFFFFF); //Color. [Edited by - Calin on August 16, 2005 6:08:08 AM]

My project`s facebook page is “DreamLand Page”

Advertisement
Crashing on the Draw call most probably means that g_font is a NULL pointer, i.e. font creation didn't succeed. What do you when font creation fails?

Am I missing something or does:

[source lang = "cpp"]//here you delete "g_font"   if(g_font)   {      g_font->Release();      g_font=NULL;   }//here you try and use "g_font"  g_font->DrawText(NULL,        //pSprite                                text,  //pString                                -1,          //Count                                &fontRect,  //pRect                                DT_LEFT|DT_NOCLIP,//Format,                                0xFFFFFFFF); //Color    


look a trifle fishy?
Quote:
What do you when font creation fails?

Nothing up to now. I just began working at this (I am trying to make a log file system)

My project`s facebook page is “DreamLand Page”

Quote:Original post by Source
Am I missing something or does:

*** Source Snippet Removed ***

look a trifle fishy?


I didn`t post my entire code. I release the interface in the GameShutDown function. GameShutDown is called when WinMain message loop is done.


[Edited by - Calin on August 16, 2005 11:29:56 AM]

My project`s facebook page is “DreamLand Page”

Ok, I just added some error lockup code (HRESULT FAILED). D3DXCreateFont fails indeed. Do you know what might be the cause?

Edit: The debugging code:
'Primitives.exe': Loaded 'C:\Documents and Settings\Calin\My Documents\LrnRes\DirectX\Basic features\32bits\The Basics Part 3\2. Formats and Culling\primitives1vb\Debug\Primitives.exe', Symbols loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\d3d9.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\d3d8thk.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\user32.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\version.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\winmm.dll', Exports loaded.'Primitives.exe': Loaded 'C:\WINDOWS\system32\d3dx9_26.dll', Exports loaded.-------------------------------------------32Bits.co.uk Error Handling-The following error occurred on line 246 of c:\documents and settings\calin\my documents\lrnres\directx\basic features\32bits\the basics part 3\2. formats and culling\primitives1vb\sg0_03.cppD3DERR_INVALIDCALLExtra information: D3DXCreateFont() failed.The program '[1260] Primitives.exe: Native' has exited with code -1 (0xffffffff).[\source]


[Edited by - Calin on August 16, 2005 12:48:41 PM]

My project`s facebook page is “DreamLand Page”

Quote:Original post by Calin
Ok, I just added some error lockup code (HRESULT FAILED). D3DXCreateFont fails indeed. Do you know what might be the cause?


The most common cause is an invalid device pointer. Are you sure the device is valid at the time you are passing it to D3DXCreateFont? Based on the log you posted it doesn't appear to have been initialized.
Stay Casual,KenDrunken Hyena
I have a similar problem (crashing instantly) though my crash isn't due to a null pointer (or atleast the logs don't show it). It looks to me like your problem is:

if (g_font){     g_font->DrawText(NULL,        //pSprite                                     text,  //pString                                     -1,          //Count                                     &fontRect,  //pRect                                     DT_LEFT|DT_NOCLIP,//Format,                                     D3DCOLOR_ARGB(255,255,255,255); //Color}else     add_log("Warning: g_font->DrawText(), g_font NULL!");


Try and fix that and see how it goes.

Edit: changed the color code, should work properly now.

[Edited by - CodeMaster Rapture on August 16, 2005 4:26:52 PM]
CodeMaster Rapture: A Null pointer was the reason why my game crashed.

The following error occurred on line 246 of c:\documents and settings\calin\my documents\...\sg0_03.cppD3DERR_INVALIDCALLExtra information: D3DXCreateFont() failed.


The problem is situated somewhere in the D3DXCreateFont(). Anyways, thanks for trying to help.

My project`s facebook page is “DreamLand Page”

DrunkenHyena you are right. I`v placed my D3DXCreateFont function before my
D3D Initialization code. Thanks so much. Rating ++

My project`s facebook page is “DreamLand Page”

This topic is closed to new replies.

Advertisement