【发布时间】:2019-05-20 11:55:40
【问题描述】:
我正在尝试使用 freetype 将字形加载到位图中,然后将其加载到每个字形的 d3d9 纹理中,但由于某种原因,纹理设置不正确。
我想将每个字形加载到它自己的纹理中,这样我就可以分别绘制每个字符。
我如何为字体创建字形纹理:
new_font_t::new_font_t( const char* szFontFilename, int iHeight, std::size_t _sRangeBegin, std::size_t _sRangeEnd ): sRangeBegin( _sRangeBegin ), sRangeEnd( _sRangeEnd )
{
assert( !FT_New_Face( _RenderContext.ftLibrary, ( std::string( R"(C:\Windows\Fonts\)" ) + szFontFilename ).c_str( ), 0, &fFont ) );
FT_Set_Char_Size( fFont, 0, iHeight * 64, 96, 96 );
pGlyphs = new IDirect3DTexture9*[ sRangeEnd - sRangeBegin ];
for( auto u = sRangeBegin; u <= sRangeEnd; u++ )
{
const auto iIndex = FT_Get_Char_Index( fFont, u );
D3DLOCKED_RECT recGlyph;
FT_Load_Glyph( fFont, iIndex, FT_LOAD_DEFAULT );
FT_Render_Glyph( fFont->glyph, FT_RENDER_MODE_NORMAL );
const auto uWidth = fFont->glyph->bitmap.width;
D3DXCreateTexture( _RenderContext.pDevice, uWidth, iHeight, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pGlyphs[ u ] );
pGlyphs[ u ]->LockRect( 0, &recGlyph, nullptr, 0 );
for ( auto i = 0; i < int( uWidth ); i++ )
{
for ( auto j = 0; j < iHeight; j++ )
if( fFont->glyph->bitmap.buffer[ i + j * uWidth ] > 0 )
reinterpret_cast< DWORD* >( recGlyph.pBits )[ i + j * uWidth ] = 0xFFFFFFFF;
}
pGlyphs[ u ]->UnlockRect( 0 );
}
}
这就是我渲染文本的方式:
void new_font_t::RenderText( const char* szText )
{
if( _RenderContext.pSprite->Begin( D3DXSPRITE_ALPHABLEND ) == D3D_OK )
{
auto loc = D3DXVECTOR3( 50, 100, 0 );
for ( auto u = 0u; u < strlen( szText ); u++ )
_RenderContext.pSprite->Draw( pGlyphs[ szText[ u ] ], nullptr, nullptr, &loc, 0xFFFFFFFF );
_RenderContext.pSprite->End( );
}
}
我这样创建一个字体结构:
test_font = new new_font_t( "arial.ttf", 32, 0, 0xFF );
然后我调用渲染函数:
test_font->RenderText( "!" );
如您所见,它应该呈现一个感叹号,但实际上呈现的是:https://i.imgur.com/Chqnltt.png
【问题讨论】: