SFML 2.4.2 - 绘制文本时出现分段错误



我在运行此代码时遇到段错误。这可能是一个错误的索引,但我很困惑,因为我尝试访问的元素存在,如"绘制"之前的"cout"所示。

我使用 GCC : g++ -std=c++11 ./ellipse.cpp -o ./ellipse -Wfatal-errors -lsfml-graphics -lsfml-window -lsfml-system

我会继续寻找,但任何帮助将不胜感激。

此致敬意

司仪

#include <iostream>
#include "SFML/Graphics.hpp"
using namespace std;
struct ellipse
{
sf::Text ellipseTitle;
};
vector<ellipse> initialize(vector<ellipse> ellipses,unsigned short numberOfEllipses)
{
sf::Font myFont;
string myfontFileName="./media/Arial.ttf";
if (!myFont.loadFromFile(myfontFileName))
{
cout << "Could not find the font " << myfontFileName << endl;
}
ellipse e;
e.ellipseTitle.setFont(myFont);
e.ellipseTitle.setCharacterSize(20);
e.ellipseTitle.setFillColor(sf::Color::White);
for(int i=0;i<numberOfEllipses;i++)
{
e.ellipseTitle.setString("[Some text "+to_string(i)+"]");
sf::FloatRect textRect = e.ellipseTitle.getLocalBounds();
e.ellipseTitle.setOrigin(textRect.left + textRect.width/2.0f,textRect.top  + textRect.height/2.0f);
e.ellipseTitle.setPosition(sf::Vector2f(100,100+(20*i)));
ellipses.push_back(e);
}
return ellipses;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(640,480), "Demo",sf::Style::Default);
unsigned short numberOfEllipses=11;
vector<ellipse> ellipses;
ellipses=initialize(ellipses,numberOfEllipses);
sf::Event myEvent;
while (window.isOpen())
{
while (window.pollEvent(myEvent))
{
if (myEvent.type == sf::Event::EventType::Closed)
{
window.close();
}
}
window.clear();
for(ellipse e:ellipses)
{
cout << "title " << e.ellipseTitle.getString().toAnsiString() << endl;
window.draw(e.ellipseTitle);
}
window.display();
}
return EXIT_SUCCESS;
} ```

真的谢谢。再搜索一下(知道如何命名问题(,我发现这是一个非常常见的错误。这是更正后的代码:

#include <stdio.h>
#include <iostream>
#include "SFML/Graphics.hpp"
using namespace std;
struct ellipse
{
sf::Text ellipseTitle;
};
vector<ellipse> initialize(vector<ellipse> ellipses,unsigned short numberOfEllipses,sf::Font& myFont)
{
ellipse e;
e.ellipseTitle.setFont(myFont);
e.ellipseTitle.setCharacterSize(20);
e.ellipseTitle.setFillColor(sf::Color::White);
for(int i=0;i<numberOfEllipses;i++)
{
e.ellipseTitle.setString("[Some text "+to_string(i)+"]");
sf::FloatRect textRect = e.ellipseTitle.getLocalBounds();
e.ellipseTitle.setOrigin(textRect.left + textRect.width/2.0f,textRect.top  + textRect.height/2.0f);
e.ellipseTitle.setPosition(sf::Vector2f(100,100+(20*i)));
ellipses.push_back(e);
}
return ellipses;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(640,480), "Demo",sf::Style::Default);
unsigned short numberOfEllipses=11;
sf::Font myFont;
string myfontFileName="./media/Arial.ttf";
if (!myFont.loadFromFile(myfontFileName))
{
cout << "Could not find the font " << myfontFileName << endl;
return EXIT_FAILURE;
}
vector<ellipse> ellipses;
ellipses=initialize(ellipses,numberOfEllipses,myFont);
sf::Event myEvent;
while (window.isOpen())
{
while (window.pollEvent(myEvent))
{
if (myEvent.type == sf::Event::EventType::Closed)
{
window.close();
}
}
window.clear();
for(ellipse e:ellipses)
{
cout << "title " << e.ellipseTitle.getString().toAnsiString() << endl;
window.draw(e.ellipseTitle);
}
window.display();
}
return EXIT_SUCCESS;
}```

相关内容

  • 没有找到相关文章

最新更新