2015-01-26 57 views
1

我在C++中創建SFML 2.1中的文本(它並不重要)。結合std :: wstring和函數

設置的文本字符串看起來像這樣:text.setString("something");

好了,但因爲我的遊戲語言爲波蘭語,我要進入像,Z,L,S,C等一些字符,這是不是'支持'在我的遊戲的ASCII編碼。

Iv'e想出了這個解決方案:text.setString(L"śomęthińg");

但是,當你嘗試的wstring和字符串從另一個功能相結合出現的問題。例如:

text.setString(L"Name: " + PlayerName()); 

我試圖將其轉換爲字符串或創建臨時變量,但要麼沒有工作,或者刪除這個「特殊字符」 ......

完整的例子:

std::string PlayerName() 
{ 
    std::string name = "John"; 
    return name; 
} 

int main() 
{ 
    sf::Text hello; 
    hello.setString(L"Hello " + PlayerName() + L", how are you?"); 
    //I need to use L" " 

    window.draw(hello); 
} 

任何想法?

+0

請提供一個http://stackoverflow.com/help/mcve – Yakk 2015-01-26 20:30:12

+1

PlayerName()返回什麼類型?你能提供一個代碼樣本,讓我們看看你在做什麼? – templatetypedef 2015-01-26 20:31:53

+0

您需要將'PlayerName()'轉換爲wstring。你可以使用這篇文章中的代碼來做到這一點:http://stackoverflow.com/a/18597384/4342498 – NathanOliver 2015-01-26 20:47:00

回答

1

好吧,我明白了!

正如@MooingDuck所說,SFML has it's own string type,這是非常強大的這類問題。

的方式我已經做到了:

std::string PlayerName() 
{ 
    std::string name = "John"; 
    return name; 
} 

int main() 
{ 
    sf::String helloText = L"Hello "; 
    helloText += PlayerName(); 
    helloText += L", how are you?"; 

    sf::Text hello; 
    hello.setString(helloText); 

    window.draw(hello); 
} 

非常感謝幫助!

相關問題