puzzle when using D3DXCreateFontIndirect

Started by
0 comments, last by zkykjxz 15 years, 3 months ago
Hi,everyone. my question is when i create a 2d text,it always on the topest of all the scenes whatever the render state i use, and i create the 2d text as follows,

      ID3DXFont* Font = 0;
      //use the D3DXFONT_DESC struct
      D3DXFONT_DESC df;
      ZeroMemory(&df, sizeof(D3DXFONT_DESC));
      Font->GetDesc(&df);
      df.Height = 12;
      df.Width = 0;
      df.Weight = FW_BOLD;
      df.MipLevels = D3DX_DEFAULT;
      df.Italic = false;
      df.CharSet = DEFAULT_CHARSET;
      df.OutputPrecision = OUT_DEFAULT_PRECIS;
      df.Quality = ANTIALIASED_QUALITY;
      df.PitchAndFamily = DEFAULT_PITCH;
      strcpy(df.FaceName, "Arial");
      //create it
      D3DXCreateFontIndirect(Device, &df, &Font);
      //render it
      RECT rect = {0, 0, Width, Height};
      Font->DrawText(0, "FPSString", -1, &rect, DT_CENTER, 0xffff00ff); 
I also render many other things, but whatever the render sequence and the render state is, the text is always on the topest of all the scenes. but if i use LOGFONT struct instead of the D3DXFONT_DESC struce when i create the font, the text can be covered by other stuff. why this problem happens? Any suggestion maybe help.
Advertisement
Maybe i haven't tell the problem clearly, this is the code i do it.

//=============================================================================//Direct3D prog //=============================================================================#pragma comment(lib, "d3d9.lib")#pragma comment(lib, "d3dx9.lib")#include <d3d9.h>#include <d3dx9.h>//-----------------------------------------------------------------------------// globle //-----------------------------------------------------------------------------LPDIRECT3D9             g_pD3D       = NULL;LPDIRECT3DDEVICE9       g_pd3dDevice = NULL;const int Width  = 640;const int Height = 480;IDirect3DVertexBuffer9* Line = 0;IDirect3DVertexBuffer9* Line2 = 0;ID3DXFont* Font   = 0;//vertex structstruct ColorVertex{	ColorVertex(){}		ColorVertex(float x, float y, float z, D3DCOLOR c)	{		_x = x;	 _y = y;  _z = z;  _color = c;	}		float _x, _y, _z;	D3DCOLOR _color;		static const DWORD FVF;};const DWORD ColorVertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;//init two line vertex buffervoid InitLineVertex(){	// Fill the buffer of line1.	g_pd3dDevice->CreateVertexBuffer(		2 * sizeof(ColorVertex), 		D3DUSAGE_WRITEONLY,		ColorVertex::FVF,		D3DPOOL_MANAGED,		&Line,		0);	ColorVertex* lv;	Line->Lock(0, 0, (void**)&lv, 0);	lv[0] = ColorVertex( -Width/2.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(  0,   0, 0));	lv[1] = ColorVertex( Width/2.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(  0,   0, 0));		Line->Unlock();	// Fill the buffer of line2.		g_pd3dDevice->CreateVertexBuffer(		2 * sizeof(ColorVertex), 		D3DUSAGE_WRITEONLY,		ColorVertex::FVF,		D3DPOOL_MANAGED,		&Line2,		0);			ColorVertex* lv2;	Line2->Lock(0, 0, (void**)&lv2, 0);		lv2[0] = ColorVertex( 0.0f, Height/2.0f, 2.0f, D3DCOLOR_XRGB(  255, 0, 0));	lv2[1] = ColorVertex( 0.0f, -Height/2.0f, 2.0f, D3DCOLOR_XRGB(  255,   255, 255));		Line2->Unlock();		}//init the font bool InitFont(){	// Initialize a D3DXFONT_DESC structure that describes the font/*	D3DXFONT_DESC df;	ZeroMemory(&df, sizeof(D3DXFONT_DESC));	df.Height = 12;	df.Width = 8;	df.Weight = FW_BOLD;	df.MipLevels = D3DX_DEFAULT;	df.Italic = false;	df.CharSet = DEFAULT_CHARSET;	df.OutputPrecision = OUT_DEFAULT_PRECIS;	df.Quality = ANTIALIASED_QUALITY;	df.PitchAndFamily = DEFAULT_PITCH;	strcpy(df.FaceName, "Arial");		// Create an ID3DXFont based on 'df'.		if(FAILED(D3DXCreateFontIndirect(g_pd3dDevice, &df, &Font)))	{		::MessageBox(0, "D3DXCreateFontIndirect() - FAILED", 0, 0);		::PostQuitMessage(0);	}*/  //use the old SDk Version, I can use the LOGFONT struct to create 	LOGFONT lf;	ZeroMemory(&lf, sizeof(LOGFONT));		lf.lfHeight         = 25;    // in logical units	lf.lfWidth          = 12;    // in logical units	lf.lfEscapement     = 0;        	lf.lfOrientation    = 0;         lf.lfWeight         = 500;   // boldness, range 0(light) - 1000(bold)	lf.lfItalic         = false;   	lf.lfUnderline      = false;    	lf.lfStrikeOut      = false;    	lf.lfCharSet        = DEFAULT_CHARSET;	lf.lfOutPrecision   = 0;              	lf.lfClipPrecision  = 0;          	lf.lfQuality        = 0;           	lf.lfPitchAndFamily = 0;           	strcpy(lf.lfFaceName, "Times New Roman");    if(FAILED(D3DXCreateFontIndirect(g_pd3dDevice, &lf, &Font)))    {		::MessageBox(0, "D3DXCreateFontIndirect() - FAILED", 0, 0);		::PostQuitMessage(0);	}		return true;}//-----------------------------------------------------------------------------// matrixs//-----------------------------------------------------------------------------VOID SetupMatrices(){    //world matr    D3DXMATRIXA16 matWorld;    D3DXMatrixIdentity( &matWorld );    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );		//view matr    D3DXVECTOR3 vEyePt( 0.0f, 0.0f, -15.0f );    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );    D3DXMATRIXA16 matView;    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );		//prog matr    D3DXMATRIXA16 matProj;	D3DXMatrixOrthoLH(&matProj,Width,Height,1.0f, 20.0f);	    //D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 20.0f );    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );}//-----------------------------------------------------------------------------//Init Direct3D//-----------------------------------------------------------------------------HRESULT InitD3D( HWND hWnd ){    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )        return E_FAIL;    D3DPRESENT_PARAMETERS d3dpp;     ZeroMemory( &d3dpp, sizeof(d3dpp) );    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,                                      &d3dpp, &g_pd3dDevice ) ) )    {        return E_FAIL;    }	SetupMatrices();	InitLineVertex();	InitFont();   return S_OK;}//-----------------------------------------------------------------------------// Render//-----------------------------------------------------------------------------VOID Render(){    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0 );        if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )    {		//draw line 2		g_pd3dDevice->SetFVF(ColorVertex::FVF);		g_pd3dDevice->SetStreamSource(0, Line2, 0, sizeof(ColorVertex));		g_pd3dDevice->DrawPrimitive(D3DPT_LINELIST, 0, 1);		//draw line 1		g_pd3dDevice->SetFVF(ColorVertex::FVF);		g_pd3dDevice->SetStreamSource(0, Line, 0, sizeof(ColorVertex));		g_pd3dDevice->DrawPrimitive(D3DPT_LINELIST, 0, 1); 		        //Draw text		RECT rect = {0, 0, Width, Height};		//Font->DrawText(0, "FPSString", -1, &rect, DT_CENTER|DT_VCENTER, 0xffff00ff);		//if use LOGFONT struct, may use this DrawText		Font->DrawText("FPSString", -1, &rect, DT_CENTER | DT_VCENTER, 0xffff00ff);		g_pd3dDevice->EndScene();    }    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );}//-----------------------------------------------------------------------------// Release the source//-----------------------------------------------------------------------------VOID Cleanup(){	if( Line )		Line->Release();	if( Line2 )		Line2->Release();	if( Font )		Font->Release();    if( g_pd3dDevice != NULL)         g_pd3dDevice->Release();    if( g_pD3D != NULL)        g_pD3D->Release();}LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {        case WM_DESTROY:            Cleanup();            PostQuitMessage( 0 );            return 0;        case WM_PAINT:            Render();            ValidateRect( hWnd, NULL );            return 0;    }    return DefWindowProc( hWnd, msg, wParam, lParam );}INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, MsgProc, 0L, 0L,                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,                      "ClassName", NULL };    RegisterClassEx( &wc );    HWND hWnd = CreateWindow( "ClassName", "Direct3D",                               WS_OVERLAPPEDWINDOW, 0, 0, Width, Height,                              NULL, NULL, wc.hInstance, NULL );    if( SUCCEEDED( InitD3D( hWnd ) ) )    { 		ShowWindow( hWnd, SW_SHOWDEFAULT );		UpdateWindow( hWnd );		MSG msg;		ZeroMemory( &msg, sizeof(msg) );		while( msg.message!=WM_QUIT )		{			if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )			{				TranslateMessage( &msg );				DispatchMessage( &msg );			}			else			{				Render(); 			}		}    }    UnregisterClass( "ClassName", wc.hInstance );    return 0;}


When use LOGFONT struct,the text maybe cover by the lines, but use the D3DXFONT_DESC struct, the text always on the topest, how can the text be covered by the lines when i use D3DXFONT_DESC struct? because when use the LOGFONT struct two draw 2d text is slower.

thanks.

This topic is closed to new replies.

Advertisement