Hi,
I insert multiple sf::Text in vector in other function. When I try to loop my vector in the main and insert the sf::Text value into windows->draw my program crash and display segmentation fault (core dumped).
#include "../includes/sfml.h"
#include <iostream>
#include <vector>
std::vector<sf::Text> ft_load_text(sf::Font font)
{
std::vector<sf::Text> v_text;
sf::Text text;
sf::Text text2;
sf::Text text3;
text.setFont(font);
text.setString("test");
text.setCharacterSize(20);
text2.setFont(font);
text2.setString("test1");
text2.setCharacterSize(20);
text3.setFont(font);
text3.setString("test2");
text3.setCharacterSize(20);
v_text.push_back(text);
v_text.push_back(text2);
v_text.push_back(text3);
return (v_text);
}
int main()
{
SFMLClass* c_sfml = new SFMLClass();
sf::RenderWindow* window = c_sfml->getWindow();
std::vector<sf::Sprite> v_sprite;
sf::Sprite *sprite = ft_load_gb();
v_sprite.push_back(*sprite);
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
return EXIT_FAILURE;
std::vector<sf::Text> v_text = ft_load_text(font);
// it works
for (sf::Text tt: v_text)
{
std::string test_text(tt.getString());
std::cout << test_text << std::endl;
}
while (window->isOpen())
{
sf::Event event;
while (window->pollEvent(event))
if (event.type == sf::Event::Closed)
window->close();
// clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (sf::Sprite i : v_sprite)
window->draw(i);
// it doesn't work
for (sf::Text tmp: v_text)
window->draw(tmp);
window->display();
}
return EXIT_SUCCESS;
}
Can you help me fix that?