我在sf::Text
和sf::Font
时遇到问题。
文字不想画。 std::wstring
具有正确的文本值。 sf::Font
完全加载。
这是我的代码:
sf::Font mainGameFont;
void drawText( const std::wstring& str, const int size, const float xposition, const float yposition, sf::RenderWindow& window, const sf::Font& mainGameFont )
{
std::wcout << str << std::endl;
source.setFont( mainGameFont );
source.setColor( color );
source.setCharacterSize( size );
source.setString( str );
source.setPosition( xposition, yposition );
window.draw(source);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1024, 768), "test");
mainGameFont.loadFromFile("futura.ttf");
...
//Other code, calling drawText(...), etc
...
}
有人可以帮帮我吗? 抱歉我的英语不好。
更新: 这段代码不起作用!
#include <string>
#include <iostream>
#include "SFML/Graphics.hpp"
sf::Font mainGameFont;
sf::Text source;
void drawText( const std::wstring& str, const int size, const float xposition, const float yposition, sf::RenderWindow& window, sf::Font& mainGameFont )
{
mainGameFont.loadFromFile("futura.ttf");
std::wcout << str << std::endl;
source.setFont( mainGameFont );
source.setColor( sf::Color::White );
source.setCharacterSize( size );
std::cout << "stat1" << std::endl;
source.setString( str );
source.setPosition( xposition, yposition );
window.draw(source);
std::cout << "ok" << std::endl;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 400), "Test");
while (window.isOpen())
{
window.clear(sf::Color::Black);
drawText(L"Ok", 20, 90, 90, window, mainGameFont);
window.display();
}
return 0;
}
答案 0 :(得分:2)
好的,首先,你的整个代码都是糟糕的编程实践:
你声明:
sf::Font mainGameFont;
sf::Text source;
作为全局变量,但是通过在函数中使用完全相同的变量名来违反阴影规则:
void drawText( const std::wstring& str, const int size, const float xposition, const float yposition, sf::RenderWindow& window, sf::Font& mainGameFont )
主要通过重新宣布:
sf::Font& mainGameFont
然后你疯狂地传递函数已经可以看到的全局变量...作为函数的引用:
drawText(L"Ok", 20, 90, 90, window, mainGameFont);
您还错误地使用了保留关键字&#39; size&#39;:
source.setCharacterSize( size );
你不能使用名称。
#include <string>
#include <iostream>
#include <SFML/Graphics.hpp>
sf::Text source;
sf::Font mainGameFont;
void drawText( const sf::String &str, const int Size, const float xposition, const float yposition, sf::RenderWindow& window)
{
source.setString(str);
source.setCharacterSize(Size); //only the lower cased word size is reserved. A capital S fixes that.
source.setPosition(xposition,yposition);
window.draw(source);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 400), "Test");
//This should only be called ONCE, not at every pass.
mainGameFont.loadFromFile("futura.ttf");
source.setFont(mainGameFont);
while (window.isOpen())
{
window.clear(sf::Color::Black);
drawText("Ok", 20, 90, 90, window);
window.display();
}
return 0;
}
重新写作。