📄 d3dfont.cpp
字号:
float x, float y, float z,
float xScale, float yScale, D3DCOLOR color,
const TCHAR* str, int flags )
{
if( !mDevice )
return E_FAIL;
// Set up renderstate
mBlockSaved->Capture();
mBlockDrawText->Apply();
mDevice->SetFVF( D3DFVF_FONT2DVERTEX );
mDevice->SetPixelShader( NULL );
mDevice->SetStreamSource( 0, mVB, 0, sizeof(SFont2DVertex) );
// Set filter states
if( flags & FILTERED ) {
mDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
mDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
}
D3DVIEWPORT9 vp;
mDevice->GetViewport( &vp );
float lineHeight = ( mTexCoords[0][3] - mTexCoords[0][1] ) * mTexHeight;
// Center the text block in the viewport
if( flags & CENTERED_X ) {
const TCHAR* strTmp = str;
float xFinal = 0.0f;
while( *strTmp ) {
TCHAR c = *strTmp++;
if( c == _T('\n') )
break; // Isn't supported.
if( (c-32) < 0 || (c-32) >= 128-32 )
continue;
float tx1 = mTexCoords[c-32][0];
float tx2 = mTexCoords[c-32][2];
float w = (tx2-tx1)*mTexWidth;
w *= (xScale*vp.Height)/lineHeight;
xFinal += w - (2 * mSpacing) * (xScale*vp.Height)/lineHeight;
}
x = -xFinal/vp.Width;
}
if( flags & CENTERED_Y ) {
y = -lineHeight/vp.Height;
}
float sx = (x+1.0f)*vp.Width/2;
float sy = (y+1.0f)*vp.Height/2;
float sz = z;
float rhw = 1.0f;
// Adjust for character spacing
sx -= mSpacing * (xScale*vp.Height)/lineHeight;
float startX = sx;
// Fill vertex buffer
SFont2DVertex* pVertices;
int numTris = 0;
mVB->Lock( 0, 0, (void**)&pVertices, D3DLOCK_DISCARD );
while( *str ) {
TCHAR c = *str++;
if( c == _T('\n') ) {
sx = startX;
sy += yScale*vp.Height;
}
if( (c-32) < 0 || (c-32) >= 128-32 )
continue;
float tx1 = mTexCoords[c-32][0];
float ty1 = mTexCoords[c-32][1];
float tx2 = mTexCoords[c-32][2];
float ty2 = mTexCoords[c-32][3];
float w = (tx2-tx1)*mTexWidth;
float h = (ty2-ty1)*mTexHeight;
w *= (xScale*vp.Height)/lineHeight;
h *= (yScale*vp.Height)/lineHeight;
if( c != _T(' ') ) {
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+h-0.5f,sz,rhw), color, tx1, ty2 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,sz,rhw), color, tx1, ty1 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,sz,rhw), color, tx2, ty2 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+0-0.5f,sz,rhw), color, tx2, ty1 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,sz,rhw), color, tx2, ty2 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,sz,rhw), color, tx1, ty1 );
numTris += 2;
if( numTris*3 > (MAX_NUM_VERTICES-6) ) {
// Unlock, render, and relock the vertex buffer
mVB->Unlock();
mDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, numTris );
mVB->Lock( 0, 0, (void**)&pVertices, D3DLOCK_DISCARD );
numTris = 0L;
}
}
sx += w - (2 * mSpacing) * (xScale*vp.Height)/lineHeight;
}
// Unlock and render the vertex buffer
mVB->Unlock();
if( numTris > 0 )
mDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, numTris );
// Restore the modified renderstates
mBlockSaved->Apply();
return S_OK;
}
*/
/** Draws 2D text. Note that sx and sy are in pixels. */
HRESULT CD3DFont::drawText( float sx, float sy, D3DCOLOR color, const TCHAR* str, int flags )
{
CD3DDevice& device = CD3DDevice::getInstance();
if( !device.isDevice() )
return E_FAIL;
// Setup renderstate
mBlockSaved->Capture();
mBlockDrawText->Apply();
device.setDeclarationFVF( D3DFVF_FONT2DVERTEX );
device.getDevice().SetPixelShader( NULL );
device.getDevice().SetStreamSource( 0, mVB, 0, sizeof(SFont2DVertex) );
// Set filter states
if( flags & FILTERED ) {
device.getDevice().SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
device.getDevice().SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
}
// Center the text block in the viewport
if( flags & CENTERED_X ) {
D3DVIEWPORT9 vp;
device.getDevice().GetViewport( &vp );
const TCHAR* strTmp = str;
float xFinal = 0.0f;
while( *strTmp ) {
TCHAR c = *strTmp++;
if( c == _T('\n') )
break; // Isn't supported.
if( (c-32) < 0 || (c-32) >= 128-32 )
continue;
float tx1 = mTexCoords[c-32][0];
float tx2 = mTexCoords[c-32][2];
float w = (tx2-tx1) * mTexWidth / mTextScale;
xFinal += w - (2 * mSpacing);
}
sx = (vp.Width-xFinal)/2.0f;
}
if( flags & CENTERED_Y ) {
D3DVIEWPORT9 vp;
device.getDevice().GetViewport( &vp );
float lineHeight = ((mTexCoords[0][3]-mTexCoords[0][1])*mTexHeight);
sy = (vp.Height-lineHeight)/2;
}
// Adjust for character spacing
sx -= mSpacing;
float startX = sx;
// Fill vertex buffer
SFont2DVertex* pVertices = NULL;
int numTris = 0;
mVB->Lock( 0, 0, (void**)&pVertices, D3DLOCK_DISCARD );
while( *str ) {
TCHAR c = *str++;
if( c == _T('\n') ) {
sx = startX;
sy += (mTexCoords[0][3]-mTexCoords[0][1])*mTexHeight;
}
if( (c-32) < 0 || (c-32) >= 128-32 )
continue;
float tx1 = mTexCoords[c-32][0];
float ty1 = mTexCoords[c-32][1];
float tx2 = mTexCoords[c-32][2];
float ty2 = mTexCoords[c-32][3];
float w = (tx2-tx1) * mTexWidth / mTextScale;
float h = (ty2-ty1) * mTexHeight / mTextScale;
if( c != _T(' ') ) {
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+h-0.5f,0.9f,1.0f), color, tx1, ty2 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,0.9f,1.0f), color, tx1, ty1 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,0.9f,1.0f), color, tx2, ty2 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+0-0.5f,0.9f,1.0f), color, tx2, ty1 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,0.9f,1.0f), color, tx2, ty2 );
*pVertices++ = gInitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,0.9f,1.0f), color, tx1, ty1 );
numTris += 2;
if( numTris*3 > (MAX_NUM_VERTICES-6) ) {
// Unlock, render, and relock the vertex buffer
mVB->Unlock();
device.getDevice().DrawPrimitive( D3DPT_TRIANGLELIST, 0, numTris );
pVertices = NULL;
mVB->Lock( 0, 0, (void**)&pVertices, D3DLOCK_DISCARD );
numTris = 0L;
}
}
sx += w - (2 * mSpacing);
}
// Unlock and render the vertex buffer
mVB->Unlock();
if( numTris > 0 )
device.getDevice().DrawPrimitive( D3DPT_TRIANGLELIST, 0, numTris );
// Restore the modified renderstates
mBlockSaved->Apply();
return S_OK;
}
/** Renders 3D text. */
/*
HRESULT CD3DFont::render3DText( const TCHAR* str, int flags )
{
if( mDevice == NULL )
return E_FAIL;
// Setup renderstate
mBlockSaved->Capture();
mBlockDrawText->Apply();
mDevice->SetFVF( D3DFVF_FONT3DVERTEX );
mDevice->SetPixelShader( NULL );
mDevice->SetStreamSource( 0, mVB, 0, sizeof(SFont3DVertex) );
// Set filter states
if( flags & FILTERED ) {
mDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
mDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
}
// Position for each text element
float x = 0.0f;
float y = 0.0f;
// Center the text block at the origin (not the viewport)
if( flags & CENTERED_X ) {
SIZE sz;
getTextExtent( str, sz );
x = -(((float)sz.cx)/10.0f)/2.0f;
}
if( flags & CENTERED_Y ) {
SIZE sz;
getTextExtent( str, sz );
y = -(((float)sz.cy)/10.0f)/2.0f;
}
// Turn off culling for two-sided text
if( flags & TWO_SIDED )
mDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Adjust for character spacing
x -= mSpacing / 10.0f;
float startX = x;
TCHAR c;
// Fill vertex buffer
SFont3DVertex* pVertices;
int numTris = 0;
mVB->Lock( 0, 0, (void**)&pVertices, D3DLOCK_DISCARD );
while( (c = *str++) != 0 ) {
if( c == '\n' ) {
x = startX;
y -= (mTexCoords[0][3]-mTexCoords[0][1])*mTexHeight/10.0f;
}
if( (c-32) < 0 || (c-32) >= 128-32 )
continue;
float tx1 = mTexCoords[c-32][0];
float ty1 = mTexCoords[c-32][1];
float tx2 = mTexCoords[c-32][2];
float ty2 = mTexCoords[c-32][3];
float w = (tx2-tx1) * mTexWidth / ( 10.0f * mTextScale );
float h = (ty2-ty1) * mTexHeight / ( 10.0f * mTextScale );
if( c != _T(' ') ) {
*pVertices++ = gInitFont3DVertex( D3DXVECTOR3(x+0,y+0,0), D3DXVECTOR3(0,0,-1), tx1, ty2 );
*pVertices++ = gInitFont3DVertex( D3DXVECTOR3(x+0,y+h,0), D3DXVECTOR3(0,0,-1), tx1, ty1 );
*pVertices++ = gInitFont3DVertex( D3DXVECTOR3(x+w,y+0,0), D3DXVECTOR3(0,0,-1), tx2, ty2 );
*pVertices++ = gInitFont3DVertex( D3DXVECTOR3(x+w,y+h,0), D3DXVECTOR3(0,0,-1), tx2, ty1 );
*pVertices++ = gInitFont3DVertex( D3DXVECTOR3(x+w,y+0,0), D3DXVECTOR3(0,0,-1), tx2, ty2 );
*pVertices++ = gInitFont3DVertex( D3DXVECTOR3(x+0,y+h,0), D3DXVECTOR3(0,0,-1), tx1, ty1 );
numTris += 2;
if( numTris*3 > (MAX_NUM_VERTICES-6) ) {
// Unlock, render, and relock the vertex buffer
mVB->Unlock();
mDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, numTris );
mVB->Lock( 0, 0, (void**)&pVertices, D3DLOCK_DISCARD );
numTris = 0;
}
}
x += w - (2 * mSpacing) / 10.0f;
}
// Unlock and render the vertex buffer
mVB->Unlock();
if( numTris > 0 )
mDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, numTris );
// Restore the modified renderstates
mBlockSaved->Apply();
return S_OK;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -