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

Author Topic: sf::Text in classes...font problem  (Read 3887 times)

0 Members and 1 Guest are viewing this topic.

mutonizer

  • Newbie
  • *
  • Posts: 8
    • View Profile
sf::Text in classes...font problem
« on: July 19, 2012, 03:15:37 pm »
Afternoon,

I'm working on some basic classes to have handy Labels, TextArea, InputBox, etc.

In all these, I use sf::text of course, and personal font as well and because of the default font crashing problem,  I've been using pointers to sf::Text in order to control how they initialize directly in the class constructor and a normal sf::font to hold the font, within each class. I figured, that way, the sf::font will stay alive as long as the object is..so no problem there...

That solved the default font crash but I then started getting insanely "wtf" issues on the sf::text draw function, each one regarding fonts in particular, as if they're no longer there at some point while they ARE still in the sf::font variables that was used to create the sf::text, for each objects. Made ZERO sense.

Then I figured: alright, let's make a sf::font right there at the start, and pass it around. But same issue again, when the draw calls arrive, it goes and starts doing font stuff...then returns that there's nothing there.

It's like...when you create a sf::text, it stores some address of something related to the font but can't find it later on!
As a note, everything fine if I just let the default font handle this, but of course, it crashes later on...and I can't use custom fonts...


So my question is...
If you want to put all sf::text calls into a class (let's say Clabel) how do you handle the font?

Another question would be: how the hell does sf::font work in relation to sf::text and especially draw calls?



Thanks in advance, that's driving me nuts :)

edit:
- oh, I use latest SFML 2.0 files, freshly made via CMAKE, and I'm on VS2010. I do have an ATI card (think it's related to the font issue if I got it right).
« Last Edit: July 19, 2012, 03:17:36 pm by mutonizer »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text in classes...font problem
« Reply #1 on: July 19, 2012, 03:43:02 pm »
When you pass the sf::Font instance around, are you sure that you pass and store it by reference and not by copy?
Laurent Gomila - SFML developer

mutonizer

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: sf::Text in classes...font problem
« Reply #2 on: July 19, 2012, 04:04:18 pm »
I did yes, at least I think so.

I had an epiphany however and I kinda solved my problem however....well it's working for now.

I went back to the basics, but instead of passing around the path/name of the font every time I created a new object, I use a config file, in which I store the fonts path & names (ie: font_menu, font_text, font_button, etc) and a classic sf::font in each class directly.
So when the constructor is called for the class, I read the config file, setup the proper sf::font depending on the variable I pass around for the font type I need from the file, then "= new" the sf::text pointer with that...and "roulez jeunesse" as we say in my country :)

Don't ask me why, works like a charm so far, no crash anywhere in sight and I finally got my DLL with labels, textarea, labellist, button and the like working without the default font, so not even a crash on exit now.

I still don't understand but it's obvious I was messing up somewhere, though it did not make any sense whatsoever.

I might try using some sort of #define pathname/fontname instead but I think I tried already and it was causing problems.

Anyhow, this font monkey business nearly drove me nuts, especially when everything else is working fine but you can't do that since the default font screws up :)

So consider this thread closed, though any details on how fonts work in relation to sf::text (they ARE linked, I'm sure of it, as soon as you link one with the other) would be appreciated.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text in classes...font problem
« Reply #3 on: July 19, 2012, 04:10:33 pm »
Quote
any details on how fonts work in relation to sf::text (they ARE linked, I'm sure of it, as soon as you link one with the other) would be appreciated.
sf::Text stores a pointer (not a copy!) to the sf::Font instance that you give to it. Then whenever you change something in your sf::Text (character size, style, ...) its internal geometry is reconstructed by querying the sf::Font instance it points to (font->getGlyph(...), etc.).
Laurent Gomila - SFML developer

mutonizer

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: sf::Text in classes...font problem
« Reply #4 on: July 19, 2012, 05:26:25 pm »
Hmm.

So if you wanted to declare the sf::font in your main() or whatever, and use it in various classes, you'd need to pass it by pointer, then the sf::text (in the class) would store a pointer, to a pointer, of the font, which, since it's in the main, will remain until the end?

I think I tried that but probably screwed up somewhere. I'll try again later probably, once the brain is rested a bit.

Anyway, thanks Laurent.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Text in classes...font problem
« Reply #5 on: July 19, 2012, 05:46:34 pm »
If you got multiple classes using the same font and eventually also the same textures, you could think about creating a resource manager. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything