2017-09-03 68 views
-1

使用SFML進行遊戲時,我設置了一種字體,但它仍然不想顯示文本。任何幫助,將不勝感激。SFML - 不顯示文本

謝謝

歐文

// Choose a font 
Font font; 
font.loadFromFile("fonts/arial.tff"); 

// Set our message font 
scoreText.setFont(font); 

scoreText.setString("Score = 0"); 
scoreText.setCharacterSize(100); 

// Choose a color 
scoreText.setFillColor(Color::White); 

// Position the text 
scoreText.setPosition(20, 20); 

window.draw(scoreText); 
window.display(); 
+0

where is window.clear()?另外請確保你不要在白色背景上畫畫。 – pmaxim98

+0

put font.loadFromFile(「fonts/arial.tff」);在一種情況下,爲了檢查字體是否已加載 – pmaxim98

+0

window.clear在那裏,忘了包含它,背景是空的。我認爲字體沒有被加載,所以字體的路徑可能不正確。 –

回答

0

-Firstly:爲了能夠使用您的字體必須存在於字體在你的項目的主要文件夾文件夾,否則函數loadFromFile()應返回false,但請注意,這是寫在官方文檔中的:

的loadFromFile功能有時可能會失敗,並沒有明顯的理由

-Secondly:由@ pmaxim98提到的,你需要繪製任何東西之前調用clear()功能,且顏色參數應該是從文本填充顏色不同,所以您可以查看顯示的文字。

-Thirdly:儘量把字體文件在項目的主文件夾,並嘗試這個最小代碼:

#include <SFML/Graphics.hpp> 
#include <iostream> 

using namespace std; 
using namespace sf; 

int main() 
{ 

RenderWindow window(VideoMode(800,600),"TEXT"); 

/****************************************************/ 

//Declare a Font object 
Font font; 

//Load and check the availability of the font file 
if(!font.loadFromFile("arial.ttf")) 
{ 
    cout << "can't load font" << endl; 
} 

//Declare a Text object 
Text text("Score = 0",font); 

//Set character size 
text.setCharacterSize(100); 

//Set fill color 
text.setFillColor(Color::White); 

/****************************************************/ 


while(window.isOpen()) 
{ 
    Event event; 
    while(window.pollEvent(event)) 
    { 
     if(event.type == Event::Closed){window.close();} 
    } 

    //Clear the window 
    window.clear(); 
    //Draw the text 
    window.draw(text); 
    //Display the text to the window 
    window.display(); 
} 

return 0; 
} 

好運。

+0

我絕對不知道爲什麼,但我複製並粘貼從系統字體Arial字體。我現在已經下載了免版稅的字體和它的作品。謝謝大家的答案 –