Hello everyone,
For the needs of a school project, I need to create a menu using only the standard SFML that loads all the games in a directory and lets the user choose which one he wants to play. So I created a custom class that I called TextItem that holds a reference to the font used for the title (sf::Font) and the sf::Text that is going to be rendered on screen. I then add all the texts to a vector (std::vector<TextItem>) with a push_back. The problem is that when I try to run my game loop (code below), I get an invalid free() error.
This is the main loop code:
//TextureItem menuBg("./assets/arcade_img.bmp");
std::vector<std::string> gameNames = getGamesNames();
std::vector<TextItem> gameTexts = getGamesTexts(gameNames);
int baseText = 100;
std::cout << "loaded " << gameTexts.size() << " titles" << std::endl;
while (_window.isOpen()) {
int event = this->HandleEvents();
if (event == 1)
break;
_window.clear();
//_window.draw(menuBg.getSprite());
// for (auto a: gameTexts) {
// _window.draw(a.getText());
// }
_window.display();
}
return "";
This is the constructor for the TextItem class:
TextItem::TextItem(std::string fontPath, std::string text, int fontSize, sf::Font font) : _font(font), _text(text, _font, fontSize)
{
}
This is the function getGamesTexts
std::vector<TextItem> getGamesTexts(std::vector<std::string> gameNames)
{
std::vector<TextItem> res;
sf::Font font;
font.loadFromFile("./assets/gamesFont.ttf");
int baseText = 100;
for (auto a: gameNames) {
std::cout << "a" << std::endl;
TextItem name("./assets/gamesFont.ttf", a, 20, font);
//name.setCoordinates(40, baseText);
//res.push_back(name);
std::cout << "added " << a << " to list" << std::endl;
baseText += 30;
}
return res;
}
If I comment out the res.push_back part I get an invalid free error, if I leave it there I get a std::bad_alloc error.
I'm sorry if I'm too vague in my description, if any other detail is needed please let me know.
Thanks