Sign in to follow this  
davidprogrammer

Fixed ID3DXFont! Crash on Start though. -_-

Recommended Posts

Hi I posted this in the c++ section a while back, I recently got all of the errors out of the program now. But it crashes. Anyone know what could cause this? Here is the code
BOOL DoInit()
{
  D3DPRESENT_PARAMETERS d3dpp;
  D3DDISPLAYMODE        d3ddm;
  LOGFONT               Font;

  // Do a windowed mode initialization of Direct3D
  if((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
    return FALSE;
  if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
    return FALSE;
  ZeroMemory(&d3dpp, sizeof(d3dpp));
  d3dpp.Windowed = TRUE;
  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat = d3ddm.Format;
  d3dpp.EnableAutoDepthStencil = FALSE;
  if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                  &d3dpp, &g_pD3DDevice)))
    return FALSE;

  // Create the font
  ZeroMemory(&Font, sizeof(Font));
  strcpy(Font.lfFaceName, "Arial");
  Font.lfHeight = -32;
  HRESULT WINAPI D3DXCreateFontIndirect(
		LPDIRECT3DDEVICE9 g_pD3DDevice,
    CONST D3DXFONT_DESC &Font,
    LPD3DXFONT &g_pFont
);


BOOL DoShutdown()
{
  // Release font
  if(g_pFont != NULL)
    g_pFont->Release();

  // Release device and 3D objects
  if(g_pD3DDevice != NULL)
    g_pD3DDevice->Release();

  if(g_pD3D != NULL)
    g_pD3D->Release();

  return TRUE;
}


BOOL DoFrame()
{
  RECT Rect = { 0,0,400,400 };

  // Clear device backbuffer
  g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,                                       D3DCOLOR_RGBA(0,64,128,255), 1.0f, 0);

  // Begin scene
  if(SUCCEEDED(g_pD3DDevice->BeginScene())) {

    // Draw some text
   
		g_pFont->DrawText(
		NULL,
    "Programming is Fun!",
    -1,
    &Rect,
    DT_CENTER | DT_VCENTER,
    0xFFFFFFFF);

    // End the scene
    g_pD3DDevice->EndScene();
  }

  // Display the scene
  g_pD3DDevice->Present(NULL, NULL, NULL, NULL);

  return TRUE;
}


Take notice that the LPD3DXSPRITE variable in g_pFont is NULL. When it isn't null I get an error saying DrawText doesn't take 5 parameters, (Because it is looking for that LPD3DXSPRITE variable. So what would I set it to? ) If I can get this done I will be finished and I can fix the gamecore file =) edit: Oh and the error is: The instruction "0x004014bd" referenced memory at "0x00000000". The memory could not be "read". Click OK to terminate the program. So I think it is the NULL thing. (Perhaps causing an empty pointer?)

Share this post


Link to post
Share on other sites
In your DoInit function, you aren't actually calling D3DXCreateFontIndirect. You've just declared a prototype.

Change:


HRESULT WINAPI D3DXCreateFontIndirect(
LPDIRECT3DDEVICE9 g_pD3DDevice,
CONST D3DXFONT_DESC &Font,
LPD3DXFONT &g_pFont
);


to:


if (FAILED(D3DXCreateFontIndirect(g_pD3DDevice,&Font,&g_pFont)))
{
// handle error
}

Share this post


Link to post
Share on other sites
Quote:
Original post by yuhaobo

Have u checked if your g_pFont is NULL? i.e. the createfont function returns successfully?


Yes it is declared NULL.

Quote:
Original post by Dave Hunt
if (FAILED(D3DXCreateFontIndirect(g_pD3DDevice,&Font,&g_pFont)))
{
// handle error
}

That threw me an error >.<

Reason: cannot convert from 'struct tagLOGFONTA *' to 'const struct _D3DXFONT_DESCA'
No constructor could take the source type, or constructor overload resolution was ambiguous
What does this mean?

Share this post


Link to post
Share on other sites
The second parameter (according to the DX9 docs) is supposed to be a pointer to a D3DXFONT_DESC structure, not a pointer to a LOGFONT structure. You need to change the declaration of your Font variable and initialize it appropriately.

Share this post


Link to post
Share on other sites
OMG ^^! Thanks all for your help. I fixed it. The author doesn't use the updated DirectX that comes with the book. If you download the original DirectX version which can be found here:
http://www.riaz.de/index.html
You will feel the feeling I have right now! lol. Excitement! Though I get the feeling I should probably use the updated versions, I am going to stick to the first one...Can someone tell me the drawbacks of doing this?

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