我已经创建了一个Button
类,它应该将一些顶点和一个字符串绘制到RenderWindow
. 这是剪掉无关部分的代码:(这是完整的代码)
namespace game {
class Button
{
public:
Button(int _x, int _y, int _width, int _height, std::string text)
{
...
sf::Font font;
font.loadFromFile("res/SF Intermosaic B.ttf");
label.setFont(font);
label.setString(text);
label.setCharacterSize(16);
label.setColor(sf::Color(20, 20, 20));
...
}
...
void draw(sf::RenderWindow& window) const
{
sf::RenderStates states;
states.texture = &texture;
window.draw(vertices[state], states);
window.draw(label); // If this line is commented out, there's no error.
}
private:
...
sf::Text label;
...
};
}
但是当我绘制文本时,程序编译得很好,但是当我运行它时,它立即崩溃。
这是来自 gdb 的回溯:
#0 0x00007ffff7bad604 in sf::Font::getTexture(unsigned int) const () from /usr/local/lib/libsfml-graphics.so.2
#1 0x00007ffff7bcd626 in sf::Text::draw(sf::RenderTarget&, sf::RenderStates) const () from /usr/local/lib/libsfml-graphics.so.2
#2 0x00007ffff7bc5bf4 in sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&) () from /usr/local/lib/libsfml-graphics.so.2
#3 0x00000000004033ad in game::Button::draw(sf::RenderWindow&) const ()
#4 0x0000000000403b64 in game::Menu::draw(sf::RenderWindow&) const ()
#5 0x00000000004042c5 in game::State::draw() ()
#6 0x0000000000402b4d in main ()
我将如何解决这个问题?