[英]Issues to display Text from sfml
多虧了sfml,我想在Windows中顯示文本。 但是我沒有來做,短信只是沒有出現在窗戶上,這很奇怪,因為我可以毫無困難地顯示線條! 您能否檢查我的代碼並告訴我我需要更改什么? :)
我的main.cpp
int main()
{
bool sortir = false;
// Création de la pile de double
Pile<double> pile_double;
// Création de la pile de string
Pile<string> pile_string;
// Création du map
map<string, function<void()>> _map;
int largeur_fenetre = 300;
int hauteur_fenetre = 400;
// Création de la fenetre
sf::RenderWindow window(sf::VideoMode(largeur_fenetre, hauteur_fenetre), "Interpreteur NPI");
//copie largeur / longueur
push(pile_double, (double) largeur_fenetre);
push(pile_double, (double) hauteur_fenetre);
// on fait tourner le programme tant que la fenêtre n'a pas été fermée
while (window.isOpen())
{
// on traite tous les évènements de la fenêtre qui ont été générés depuis la dernière itération de la boucle
sf::Event event;
while (window.pollEvent(event))
{
cout << "passage 1er while" << endl;
// fermeture de la fenêtre lorsque l'utilisateur le souhaite
if (event.type == sf::Event::Closed)
window.close();
}
// effacement de la fenêtre en noir
window.clear(sf::Color::Black);
window.display();
cout << "coucou" << endl;
while (sortir == false)
{
cout << "Entrez la commande souhaité >";
string carac;
cin >> carac;
// Recherche du string dans le map
for (map<string, function<void()>>::iterator it = _map.begin(); it != _map.end(); ++it) // Debut recherche de la relation
{
// S'il est trouvé on demarre sa fonction associé
if (carac == it->first)
{
(it->second)();
pile_double.display();
pile_string.display();
window.display();
}
} // Fin de la recherche de la relation string -> fonction | Fin du For
if (carac == "exit")
{
sortir = true;
}
}
}
return 0;
}
fonction.cpp
void drawstr(Pile<double>& nomDeLaPile, sf::RenderWindow& window, Pile<string>& nomDeLaPile2)
{
sf::Text text;
sf::Font font;
text.setFont(font);
text.setString("Hello world");
text.setCharacterSize(24);
text.setColor(sf::Color::Red);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
window.draw(text);
}
對於英語不好,我深表歉意,希望您還是能理解!
你做:
sf::Font font;
text.setFont(font);
但是您不加載任何字體,而是“設置”一個空字體...
嘗試類似的事情之前:
if (!font.loadFromFile("arial.ttf"))
{
// error
}
但是您不應該在繪圖功能中這樣做。 啟動應用程序時,字體必須加載一次。
從SFML教程中:
請注意,SFML不會自動加載系統字體,即font.loadFromFile(“ Courier New”)將不起作用。 首先是因為SFML需要文件名,而不是字體名,其次是因為SFML沒有對系統字體文件夾的魔術訪問。 因此,如果要加載字體,則需要在應用程序中包含字體文件,就像其他資源(圖像,聲音等)一樣。
SFML沒有提供默認字體,也沒有為此提供字體。
使用以下方法創建Font對象時:
sf::Font font;
它是一個空字體。 您需要做的是從網站(例如http://www.dafont.com/ )下載字體,或者找到字體文件在計算機上的位置。
然后,您應該加載字體(來自同一SFML教程):
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
{
// error...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.