c++ - 绘制 sf::Text 时出现问题。 C++

标签 c++ sfml


我在使用 sf::Textsf::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;
}

最佳答案

好的,首先,您的整个代码都散发着不良编程习惯的味道:

您声明:

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);

您还错误地使用了保留关键字“尺寸”:

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;
}

重新编写以工作。

关于c++ - 绘制 sf::Text 时出现问题。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38009368/

相关文章:

c++ - 应用程序启动时停靠 QDockWidget?

C++ SFML 在定义的矩形内绘制可滚动内容

c++ - sfml pollEvent,每个新图像都会删除前一个图像

c++ - '对象' : 'struct/class' type redefinition WITH header guards. 如何修复?

c++ - 禁止在继承类中定义复制构造函数

c++ - GObject 与 C++ : What benefits does GObj offer, 以及它在速度/大小方面的比较如何?

c++ - 我从指针得到一个未初始化的对象

c++ - 如何修复 "initial value of reference to non-const must be an lvalue?"

c++ - C++ 中的内存屏障示例有哪些?

c++ - 内联函数的局部静态/线程局部变量?