在 SFML 2.4 中使用字体精灵 sheet 作为字体
Use a font sprite sheet as font in SFML 2.4
是否可以在 SFML 中使用字体拍摄图像
我会拍摄这张图片并以某种方式将其表示为文本并在 sf::Text
或 sf::Font
中使用它。有什么想法吗?
SFML 字体处理仅与普通字体文件有关。不过,使用 sf::Sprite
可以轻松实现 Sprite sheet 字体。
虽然(还)没有直接实现,但您可以这样做——正如 Bartek 所提到的——为此使用 sf::Sprite
,但我建议使用 sf::VertexArray
。
首先,您需要一些函数来将任何字符转换为精灵中的坐标 sheet。有几种方法可以做到这一点。但现在我只想做一个简单的映射:
std::map<wchar_t, sf::IntRect> glyphs;
glyphs[L' '] = sf::IntRect(0, 0, 24, 24);
glyphs[L'!'] = sf::IntRect(24, 0, 24, 24);
// etc. define everything in your font
请注意,我没有测量您上面的 字符 sheet。我只是使用 24 像素进行演示。当然,您可以在加载 "font".
时动态构建这样的映射
当使用 sf::Sprite 绘图时,您可以这样做:
sf::Sprite character(myFontTexture);
int x = 0;
for(auto &c : text) {
auto &glyph = glyphs.find(c);
// Unknown character not in our mapping?
if(glyph == glyps.end())
continue; // skip it!
// Update the sprite
character.setTextureRect(glyph);
character.setPosition(x, 0);
// Draw it
window.draw(character);
// Update the position for the next glyph
x += glyph.width;
}
sf::VertexArray 的方法类似,但您只需一个字一个字地构造它,而不是一遍又一遍地重新绘制它们。
int x = 0;
for(auto &c : text) {
auto &glyph = glyphs.find(c);
// Unknown character not in our mapping?
if(glyph == glyps.end())
continue; // skip it!
// Specific vertex layout etc. depends on the actual sf::VertexArray
myVertexArray.append(sf::Vertex2f(sf::Vector2f(x, 0), sf::Vertex2f(glyph.left, glyph.top));
myVertexArray.append(sf::Vertex2f(sf::Vector2f(x + glyph.width, 0), sf::Vertex2f(glyph.left + glyph.width, glyph.top));
myVertexArray.append(sf::Vertex2f(sf::Vector2f(x + glyph.width, glyph.height), sf::Vertex2f(glyph.left + glyph.width, glyph.top + glyph.height));
myVertexArray.append(sf::Vertex2f(sf::Vector2f(x, glyph.height), sf::Vertex2f(glyph.left, glyph.top + glyph.height));
}
// Draw the text
window.draw(myVertexArray);
请注意,所有这些代码都未经测试并且来自内存,因此可能会出现错误,但它应该让您大致了解如何执行此操作。
是否可以在 SFML 中使用字体拍摄图像
我会拍摄这张图片并以某种方式将其表示为文本并在 sf::Text
或 sf::Font
中使用它。有什么想法吗?
SFML 字体处理仅与普通字体文件有关。不过,使用 sf::Sprite
可以轻松实现 Sprite sheet 字体。
虽然(还)没有直接实现,但您可以这样做——正如 Bartek 所提到的——为此使用 sf::Sprite
,但我建议使用 sf::VertexArray
。
首先,您需要一些函数来将任何字符转换为精灵中的坐标 sheet。有几种方法可以做到这一点。但现在我只想做一个简单的映射:
std::map<wchar_t, sf::IntRect> glyphs;
glyphs[L' '] = sf::IntRect(0, 0, 24, 24);
glyphs[L'!'] = sf::IntRect(24, 0, 24, 24);
// etc. define everything in your font
请注意,我没有测量您上面的 字符 sheet。我只是使用 24 像素进行演示。当然,您可以在加载 "font".
时动态构建这样的映射当使用 sf::Sprite 绘图时,您可以这样做:
sf::Sprite character(myFontTexture);
int x = 0;
for(auto &c : text) {
auto &glyph = glyphs.find(c);
// Unknown character not in our mapping?
if(glyph == glyps.end())
continue; // skip it!
// Update the sprite
character.setTextureRect(glyph);
character.setPosition(x, 0);
// Draw it
window.draw(character);
// Update the position for the next glyph
x += glyph.width;
}
sf::VertexArray 的方法类似,但您只需一个字一个字地构造它,而不是一遍又一遍地重新绘制它们。
int x = 0;
for(auto &c : text) {
auto &glyph = glyphs.find(c);
// Unknown character not in our mapping?
if(glyph == glyps.end())
continue; // skip it!
// Specific vertex layout etc. depends on the actual sf::VertexArray
myVertexArray.append(sf::Vertex2f(sf::Vector2f(x, 0), sf::Vertex2f(glyph.left, glyph.top));
myVertexArray.append(sf::Vertex2f(sf::Vector2f(x + glyph.width, 0), sf::Vertex2f(glyph.left + glyph.width, glyph.top));
myVertexArray.append(sf::Vertex2f(sf::Vector2f(x + glyph.width, glyph.height), sf::Vertex2f(glyph.left + glyph.width, glyph.top + glyph.height));
myVertexArray.append(sf::Vertex2f(sf::Vector2f(x, glyph.height), sf::Vertex2f(glyph.left, glyph.top + glyph.height));
}
// Draw the text
window.draw(myVertexArray);
请注意,所有这些代码都未经测试并且来自内存,因此可能会出现错误,但它应该让您大致了解如何执行此操作。