使用SFML在C ++中将字形渲染为位图图像

时间:2014-10-29 08:54:26

标签: c++ visual-studio-2008 bitmap sfml glyph

我想生成一个字形的位图图像,以便我可以将它与另一个图像中未知字母的像素值进行比较。

我正在使用Visual Studio 2008,我的项目使用的是C ++,而我正在使用SFML。我可以从ttf文件中加载一个字体,我尝试做类似的事情:

sf::Font myFont;
myFont.loadFromFile("Path\\arial.ttf");

sf::Texture myTexture = myFont.getTexture(48);
sf::Image textureImage = myTexture.copyToImage();

sf::Glyph myGlyph = myFont.getGlyph(65, 12, false); // get the 'A' glyph

sf::Image glyphImage;
glyphImage.create(myGlyph.bounds.width, myGlyph.bounds.height, sf::Color::White);
glyphImage.copy(textureImage, 0, 0, myGlyph.textureRect); 

我认为这不起作用,因为我只是根据字形所在的纹理部分创建一个图像,而不是字形本身的像素值。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这对我来说很好用:

unsigned int size = 20;
sf::Glyph glyph = font.getGlyph('A', size, false);
sf::Texture bitmap = font.getTexture(size);

sf::Image image;
image.create(glyph.bounds.width, glyph.bounds.height);
image.copy(bitmap.copyToImage(), 0, 0, glyph.textureRect);

请注意,在此示例中,调用getGlyphgetTexture时字符大小相同,而上述代码中并非如此。

此外,为了便于阅读,请使用65而不是像'A'这样的幻数。