C++ Sfml 文本输出

标签 c++ visual-studio-2013 sfml

我最近开始在 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/

相关文章:

c++ - 如何在 GCC 的 VSCode 中设置 wxWidgets?

c++ - 递归函数的返回类型推导

c# - 将数据绑定(bind)到 Windows Phone 中的可观察集合

javascript - ComboBox 抛出错误 :Unable to get property 'createRange' of undefined or null reference

c++ - VS2015 Express & VS2017 : Document cannot be opened: It has renamed, 删除或移动

c++ - "Looking At"具有四元数的对象

c++ - SFML 未定义对 `sf::TcpSocket::TcpSocket()' 的引用

c++ - 将 BSTR 复制到 char[1024]?

visual-studio-2013 - 为什么Visual Studio 2013会不断更改为深色主题

c++ - 如何在 C++ 中使用可变参数泛型 lambda 计算总和?