附录代码:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
// 创建一个窗口
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML 李明");
// 加载字体
sf::Font font;
std::string fontPath = "C:\\Users\\BOBY7914\\source\\repos\\Project3\\fonts\\tuffy.ttf";
std::cout << "Attempting to load font from: " << fontPath << std::endl;
if (!font.loadFromFile(fontPath))
{
std::cerr << "Error loading font" << std::endl;
return -1;
}
std::cout << "Font loaded successfully!" << std::endl;
// 创建一个文本对象
sf::Text text("李明", font, 50);
text.setFillColor(sf::Color::Red);
text.setPosition(100, 100);
// 游戏循环
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// 清空窗口
window.clear(sf::Color::White);
// 绘制文本
window.draw(text);
// 显示窗口内容
window.display();
}
return 0;
}