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

Author Topic: Passing the sf::font to a class  (Read 2564 times)

0 Members and 1 Guest are viewing this topic.

Riton_Lafouine

  • Newbie
  • *
  • Posts: 16
    • View Profile
Passing the sf::font to a class
« on: May 14, 2020, 04:27:46 pm »
hello,

I have a performance problem with my program, i'm using a class that i use multiple (up to 2048) times. Graphics of the class are drawn in the class.

The graphics consists in a vertex array and some dynamic string text that are processed in the class. To use text, i load the font for each instance of the class, wich is not optimal. As loading 2048 times the same font is not a good idea.

What i want to do is to make the class use a font that is out of the class in order to load it only once. But, i really don't even know where to start. I tried to make the font a pointer, but could not make it work.

What can i do to load an external sf::font in a class ?

thanks by advance

RLF

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Passing the sf::font to a class
« Reply #1 on: May 14, 2020, 09:26:57 pm »
You're right. It's much better to load the font only once and re-use that for each text.

All you really need to do is store the font somewhere and pass it around as a reference or using a pointer.

It might look something like this:

class Thing
{
public:
    setFont(sf::Font& font) // passing the font by reference
    {
        m_text.setFont(font);
    }

private:
    sf::Text m_text;
}

sf::Font font;
if (!font. loadFromFile("font.ttf"))
    return EXIT_FAILURE;

std::vector<Thing> things;
for (auto& thing : things)
    thing.setFont(font);

Obvious the Thing class would do more things. This is just a way to set the text to the same font.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Riton_Lafouine

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Passing the sf::font to a class
« Reply #2 on: May 19, 2020, 11:04:44 pm »
oooh yes that's a very late answer but i coulnd not finish without thanx,

We finally found the static method in the 1.6 SFML documentation. Old stuff are sometimes good stuff. This is not far of what you proposed. What is cool in code is that there a different way to do the same things and not only one good method.

Here is what we finally did :

in the main :

Channel::Init("kimberley_bl.ttf");
 

and in the class :

static sf::Font font;

bool Channel::Init(const std::string& fontFile)
{
    return font.loadFromFile(fontFile);
}
 
Creating multiple classes is now fast as hell ...

Regards,

RLF

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Passing the sf::font to a class
« Reply #3 on: May 20, 2020, 07:06:51 pm »
We finally found the static method in the 1.6 SFML documentation.
Are you using SFML 1?

Here is what we finally did :
Global SFML resources are never a good idea. Remember I said this :P

oooh yes that's a very late answer
Same day?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Riton_Lafouine

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Passing the sf::font to a class
« Reply #4 on: May 25, 2020, 04:57:13 pm »

Quote
Are you using SFML 1?
nope, but this part of the 1.6 tutorial seems to still be ok ... This part disapeared in later versions.

Quote
Global SFML resources are never a good idea. Remember I said this :P
well, i'm not sure why, but i'll do ... this sounds so mysterious to me ! do you think what i've done is a bad solution ?

Quote
Same day?

int HapaxPostDate = 15;
int MyAnswerDate = 19;
int TimeForAnswer = MyAnswerDate - HapaxPostDate;
cout << "Response Lag : " << TimeForAnswer << " days" << endl;

i should have used uint16_t ...  :P