Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - matfire

Pages: [1]
1
Graphics / Re: Problem with sf::Text in a custom class
« on: March 20, 2019, 12:37:49 pm »
it is caused by the push_back call on the vector.

2
Graphics / Re: Problem with sf::Text in a custom class
« on: March 20, 2019, 12:20:14 pm »
So,
I changed my contructor to this:

TextItem(std::string text, int fontSize, sf::Font *font)

I can tell call it like this:
TextItem name(a, 20, font); where font = sf::Text *
where font is sf::Font _titleFont;
Still getting a bad_alloc exception.

I'm sorry to be such bother, I'm still learning how c++ really works

3
Graphics / Re: Problem with sf::Text in a custom class
« on: March 20, 2019, 10:31:13 am »
I also tried initialising a font inside the class

TextItem::TextItem(std::string fontPath, std::string text, int fontSize)
{
   _font.loadFromFile(fontPath);
   _text.setFont(_font);
   _text.setString(text);
}

But I still get an std::bad_alloc. I'm thinking of just having a reference to the font in the Core (where windows, events etc are initialized) and the referencing it when initializing text, but it's not very elegant. What do you think?

4
Graphics / Problem with sf::Text in a custom class
« on: March 19, 2019, 10:23:23 am »
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


Pages: [1]