# D3DXFont giving me troubles

## Recommended Posts

utilae    188
It was working, drawing the frame rate, no problems. But now if I run the app from the debug directory it just quits out. Using debug->start in visual studio .net (c++, direct 3d9) I got this error: unhandled exception at 0x77f767cd Here's my code: setting up the font
//setting up the font
////FONT
D3DXCreateFont(g_pD3DDevice9,     //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_pFont);         //ppFont


Heres the game loop
//game loop
void Game_Loop()
{
////RECOVER FROM ALT TAB
HRESULT hr;
hr=g_pD3DDevice9->TestCooperativeLevel();

if(hr==D3DERR_DEVICELOST)
{ //Device is lost and cannot be reset yet
Sleep(500); //Wait a bit so we don't burn through cycles for no reason
FreeVolatileResources();
}
else if(hr==D3DERR_DEVICENOTRESET)
{ //Lost but we can reset it now
hr=g_pD3DDevice9->Reset(&g_d3dpp);
if(SUCCEEDED(hr))
InitVolatileResources();
}

////RENDER
//clear back buffer to white
g_pD3DDevice9->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(255,255,0),1.0f,0);

g_pD3DDevice9->BeginScene();

g_pD3DDevice9->SetFVF(g_dwFVF);

g_pD3DDevice9->SetStreamSource(0,g_pVertexBuffer,0,sizeof(VERTEX));

//Render from Vertex Buffer
g_pD3DDevice9->DrawPrimitive(D3DPT_TRIANGLELIST,0,g_primitive_count);

g_pD3DDevice9->EndScene();

//FRAMES PER SECOND
g_nFrames++;
DWORD dwFpsTime=GetTickCount();

//when 1 second has passed display the frames per second
if (dwFpsTime>(g_dwLastFpsLog+1000))
{
//store max fps and current fps
g_nCurrentFrames=g_nFrames;
if(g_nCurrentFrames>g_nMaxFrames)g_nMaxFrames=g_nCurrentFrames;

//reset frames and record time when we last showed fps
g_nFrames=0;
g_dwLastFpsLog=dwFpsTime;
}

//store max frames and fps in a string
g_StrStream<<"Max Frames: "<<g_nMaxFrames<<" Current Frames: "<<g_nCurrentFrames;
string strFrameRate=g_StrStream.str();
const char *c_chFrameRate=strFrameRate.c_str();
g_StrStream.str("");

//setup up rect for fps
RECT recFrameRate;
recFrameRate.left=0;
recFrameRate.top=0;
recFrameRate.right=100;
recFrameRate.bottom=100;

//DRAW FPS
g_pFont->DrawText(NULL,c_chFrameRate,
-1,&recFrameRate,DT_LEFT|DT_NOCLIP,D3DCOLOR_XRGB(0,0,0));//crashes at this point
//crash is an execution crash, so there were no compile errors

//flip back buffer
g_pD3DDevice9->Present(NULL,NULL,NULL,NULL);

}
void InitVolatileResources()
{
//Init internal resources
g_pFont->OnResetDevice();
}
void FreeVolatileResources()
{
//Free up some internal resources
g_pFont->OnLostDevice();
}


here's the global section relating to fonts
//globals.h
#ifndef __GLOBALS_H__
#define __GLOBALS_H__

#include <d3d9.h>
#include <d3dx9.h>

extern ID3DXFont *g_pFont;//font

#endif

//globals.cpp
#include "globals.h"

ID3DXFont *g_pFont=NULL;//font


Anyone able to help? [Edited by - utilae on February 15, 2005 8:26:23 PM]

##### Share on other sites
utilae    188
Anyone able to help?

The font was working fine once. Havn't changed the font at all. Now the font call Drawtext is failing for some reason.

##### Share on other sites
Evil Steve    2017
If you run the app from the debug directory, its default path is different than it would be if it was running from MSVC. Which means that your resources probably aren't being loaded.
The only problem I can see with that line is that g_pFont might be NULL or otherwise invalid. You should check the return value of D3DXCreateFont() also.

##### Share on other sites
utilae    188
Quote:
 The only problem I can see with that line is that g_pFont might be NULL or otherwise invalid.

If I make it so it's not NULL, then it still does it (quits out of app).

Quote:
 You should check the return value of D3DXCreateFont() also.

Before I can write the return value to a log, it quits out as soon as it hits the line:
g_pFont->DrawText(NULL,c_chFrameRate,
-1,&recFrameRate,DT_LEFT|DT_NOCLIP,D3DCOLOR_XRGB(0,0,0));//

##### Share on other sites
circlesoft    1178
Quote:
 Original post by utilaeBefore I can write the return value to a log, it quits out as soon as it hits the line:g_pFont->DrawText(NULL,c_chFrameRate,-1,&recFrameRate,DT_LEFT|DT_NOCLIP,D3DCOLOR_XRGB(0,0,0));//

You undoubtedly need to determine the return value of D3DXCreateFont() first. This shouldn't be *that* big of a deal, because:

(1) The D3DXCreateFont() call is in your initialization code
(2) You can easily step through the code with a debugger.

As general procedure, you should check the return value of all HRESULT functions. I have macros setup that will automatically log errors if a negative code occurs. This way, you don't have to write if statement upon if statment.

##### Share on other sites
Graham    166
I would be interested in seeing your macro for that. I am currently writing if(FAILED(func())) around almost all my intialization functions.

##### Share on other sites
utilae    188
I would like to know too.

Is there a way to print an HRESULT return value as the error message to a log. Eg, if the error message is 0, then it would print OK to the log (assuming 0=OK).

##### Share on other sites
Quote:
 Is there a way to print an HRESULT return value as the error message to a log. Eg, if the error message is 0, then it would print OK to the log (assuming 0=OK).

Lookup DXGetErrorString9() and DXGetErrorDescription9().

##### Share on other sites
utilae    188
Quote:
 Original post by circlesoftYou undoubtedly need to determine the return value of D3DXCreateFont() first. This shouldn't be *that* big of a deal, because:(1) The D3DXCreateFont() call is in your initialization code(2) You can easily step through the code with a debugger.As general procedure, you should check the return value of all HRESULT functions. I have macros setup that will automatically log errors if a negative code occurs. This way, you don't have to write if statement upon if statment.

Hi, my problems with font->drawtext continue. I checked the errors from createfont, but it seems to be ok, I get S_OK as the return error. The drawtext function still fails, and it quits out before I can get an error message from it.

//Setup FontHRESULT hrCreateFont=D3DXCreateFont(g_pD3DDevice9,     //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_pFont);         //ppFont//log errorsg_Log<<endl<<"CreateFont() "<<DXGetErrorString9(hrCreateFont)<<endl<<endl;

Also, the above method of getting errors, using the DXGetErrorString9 function works great.