我最近开始在 C++ 中使用 SFML,在绘制文本时遇到问题。我设法运行默认程序,在窗口上绘制一个绿色圆圈,因此我假设没有硬件问题。我正在使用 SFML 2.1 和 Visual studio 2013。
这是我的代码:
#ifdef SFML_STATIC
#pragma comment(lib, "glew.lib")
#pragma comment(lib, "freetype.lib")
#pragma comment(lib, "jpeg.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")
#endif // SFML_STATIC
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow mywindow(sf::VideoMode(800, 600), "SFML window");
sf::Text text;
text.setString(std::string("Hello World!"));
text.setCharacterSize(24);
text.setColor(sf::Color::Red);
text.setStyle(sf::Text::Bold);
mywindow.clear();
mywindow.draw(text);
mywindow.display();
std::cin.get(); //To prevent the window automatically closing.
}
感谢您提前提供的任何帮助!
最佳答案
在绘制文本之前,您需要通过创建 sf::Font 对象,使用其 loadFromFile() 函数,然后将其作为参数传递给 sf::Text 的 setFont() 函数来提供字体。
sf::Font font;
font.loadFromFile("file_path");
text.setFont(font);
关于C++ Sfml 文本输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23143643/