|
|

楼主 |
发表于 2005-4-22 10:11:00
|
显示全部楼层
Re:一个关于3D场景绘制2D文字的问题
我把我的代码贴出来吧:
// File: CreateDevice.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <string>
#include <d3d9.h>
#include <strsafe.h>
#include "font.h"
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPD3DXSPRITE spr;//////////////////////////////////////////////////////////
font m_font;/////////////////////////////////////////////////////////////////
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;
}
m_font.fontInit(g_pd3dDevice);
D3DXCreateSprite(g_pd3dDevice,&spr);/////////////////////////////////////////
return S_OK;
}
VOID Cleanup()
{
。。。。。
}
VOID Render()
{
。。。。
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
RECT rect={0,0,500,500};
m_font.showText(spr,"hello",rect,0xffffffff);
g_pd3dDevice->EndScene();
}
g_pd3dDevice-> resent( NULL, NULL, NULL, NULL );
}
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
。。。
return DefWindowProc( hWnd, msg, wParam, lParam );
}
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
。。。
}
下面是我自己建的font类:
////////////////////////////////////////////////////////////////////////////
//////////////////////// font.h /////////////////////
///////////////////////////////////////////////////////////////////////////
#include <string>
#include <windows.h>
#include <d3dx9.h>
#include <string>
#include <d3dx9.h>
#include "d3dx9core.h"
using namespace std;
class font{
public:
font();
~font();
bool showText(LPD3DXSPRITE spr,string s,RECT rect,D3DCOLOR cor);
bool fontInit(LPDIRECT3DDEVICE9 g_pd3dDevice);
D3DXFONT_DESC d3dFont;
LPD3DXFONT sfont;
};
////////////////////////////////////////////////////////////////////////////
//////////////////////// font.cpp //////////////////
///////////////////////////////////////////////////////////////////////////
#include "font.h"
font::font(){
sfont = NULL;
}
font::~font(){
if( sfont )
{
sfont->Release();
}
}
bool font::showText(LPD3DXSPRITE spr,string s,RECT rect,D3DCOLOR cor){
char *str;
strcpy(str,s.c_str());
sfont->DrawText(spr,"Hello World",-1,&rect,DT_TOP|DT_LEFT,0xffffffff);
return 1;
}
bool font::fontInit(LPDIRECT3DDEVICE9 g_pd3dDevice){
memset(&d3dFont,0,sizeof(d3dFont));
d3dFont.Height=25;
d3dFont.Width=12;
d3dFont.Weight=500;
d3dFont.Italic=FALSE;
d3dFont.CharSet=DEFAULT_CHARSET;
LPD3DXFONT sfont;
D3DXCreateFontIndirect(g_pd3dDevice,&d3dFont,&sfont);
return 1;
}
|
|