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

Author Topic: LoadFromFile fails after too many Text objects  (Read 3254 times)

0 Members and 2 Guests are viewing this topic.

janos_

  • Newbie
  • *
  • Posts: 9
    • View Profile
LoadFromFile fails after too many Text objects
« on: March 28, 2021, 08:02:54 pm »
I am trying to create 1000 sf::Text objects. But loadFromFile returns 0 after 508 initialized objects. Why is that the case and is there a way to by pass this to create all 1000 objects? The code example demonstrates this.

Thank you for your time.

#include <iostream>
#include <SFML/Graphics.hpp>

class Text
{
private:
    sf::Text text;
    sf::Font font;

    sf::RenderTarget& renderTarget;
public:
    Text(const sf::String& fontPath, sf::RenderTarget& p_renderTarget) : renderTarget(p_renderTarget)
    {
        std::cout << "Font loaded: " << font.loadFromFile(fontPath) << std::endl;
        text.setFont(font);
        text.setString("Hallo Welt!");
        text.setCharacterSize(25);
    }

    void Update() { renderTarget.draw(text); }
};

int main()
{
    sf::RenderWindow window(sf::VideoMode(900, 600), "SFML works!");
    std::vector<Text*> texts;
    int textAmount = 1000;
    for (int i = 0; i < textAmount; i++)
    {
        std::cout << "Nr." << i << ": ";
        Text* t = new Text("arial.ttf", window);
        texts.push_back(t);
    }

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        for (int i = 0; i < textAmount; i++)
        {
            texts[i]->Update();
        }
        window.display();
    }

    for (int i = 0; i < textAmount; i++)
    {
        delete texts[i];
    }
}
« Last Edit: March 29, 2021, 06:21:59 pm by janos_ »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: LoadFromFile fails after too many sf::Text objects
« Reply #1 on: March 29, 2021, 09:32:14 am »
Why are you trying to load 1000 times the same font?

You should definitely not do this. A font consumes a lot of resources, and you most likely run out of GPU memory after 508.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: LoadFromFile fails after too many sf::Text objects
« Reply #2 on: March 29, 2021, 01:24:37 pm »
Said differently, you can reuse the font object for multiple text objects.

Also if you have a multi-line text, you can also insert \n to get the text on a newline.
That way you can potentially reduce the amount of text objects required.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

janos_

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: LoadFromFile fails after too many sf::Text objects
« Reply #3 on: March 29, 2021, 04:14:23 pm »
Quote
Why are you trying to load 1000 times the same font?
I am creating a list as a user interface where each list element consists of multiple text objects. That list gets quite large also because I have 4 lists in total (and some other locations were I load fonts). For example 80 list elements each loading 2 fonts creates 160 font objects.

Quote
Said differently, you can reuse the font object for multiple text objects.
That will fix the problem. I am just creating to many font objects. I'll try to implement that.

Thank you for your replies.