C++ 绘制sf::文本时出现问题。C++;

C++ 绘制sf::文本时出现问题。C++;,c++,sfml,C++,Sfml,我在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&a


我在
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
    ...
}
sf::Font mainGameFont;
void drawText(常量std::wstring&str、常量int size、常量float xposition、常量float yposition、sf::RenderWindow&window、常量sf::Font&mainGameFont)
{

std::wcout好的,首先,您的整个代码都散发着糟糕编程实践的味道:

您声明:

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 )
drawText(L"Ok", 20, 90, 90, window, mainGameFont);
主要通过重新说明:

sf::Font& mainGameFont
然后将函数已经可以看到的全局变量…作为函数的引用传递给函数:

void drawText( const std::wstring& str, const int size, const float xposition, const float yposition, sf::RenderWindow& window, sf::Font& mainGameFont )
drawText(L"Ok", 20, 90, 90, window, mainGameFont);
您还错误地使用了保留关键字“size”:

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;
}
#包括
#包括
#包括
文本源;
sf::字体mainGameFont;
void drawText(常量sf::String&str、常量int Size、常量float xposition、常量float yposition、sf::RenderWindow&window)
{
source.setString(str);
source.setCharacterSize(Size);//仅保留大小写较低的单词大小。大写字母S可修复此问题。
source.setPosition(xposition,yposition);
window.draw(源代码);
}
int main()
{
sf::RenderWindow窗口(sf::VideoMode(600400),“测试”);
//这应该只调用一次,而不是每次调用。
mainGameFont.loadFromFile(“futura.ttf”);
source.setFont(mainGameFont);
while(window.isOpen())
{
窗口。清除(sf::颜色::黑色);
drawText(“确定”、20、90、90、窗口);
window.display();
}
返回0;
}

重新写入工作。

您是否记得调用
Window::display
切换后台缓冲区?如果您不熟悉这个概念,可以解释一下。您对事件的查询太正确了?@conduct是的,我编写了Window.display()在主循环的末尾。@user975989我不太理解你。sfml事件都很好。@unciled_循环发布一篇文章,这样我们可以更好地帮助你。非常感谢你!现在是工作,画“Ok”,但最终崩溃。但这不是一个大问题,我会解决它。哈哈,我试图将此代码重写到我的游戏中,但它不是画图!然后我尝试更改source.setColor(color);改为sourve.setColor(sf::color::White)正确的是,建议代码应该1)不使用SFML类型作为全局变量,2)定期处理事件。这两个问题都使这段代码进入未定义的行为字段。“大小”真的是C++关键字还是你在想“sieof”?