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

Author Topic: SF::Text not properly rendering  (Read 3867 times)

0 Members and 1 Guest are viewing this topic.

hadrian

  • Newbie
  • *
  • Posts: 2
    • View Profile
SF::Text not properly rendering
« on: September 01, 2021, 12:57:47 am »
Hi everyone,

I'm trying to write some code that can load various fonts and keep them cached in memory. However, for some reason, only the last font loaded properly displays. When I select any of the earlier fonts, all I see is a few dots (looks like 2x2 pixels).

It's probably something super obvious, but I can't seem to figure out what's going wrong here. The relevant code:

(click to show/hide)

(click to show/hide)

(click to show/hide)
« Last Edit: September 01, 2021, 01:03:19 am by hadrian »

kojack

  • Sr. Member
  • ****
  • Posts: 299
  • C++/C# game dev teacher.
    • View Profile
Re: SF::Text not properly rendering
« Reply #1 on: September 01, 2021, 05:50:10 am »
The Get_Font_By_Name function returns a reference to a Font. But it only contains a return for the font found path, if a font wasn't found there's no return, meaning it is returning an invalid reference.
But that's a separate issue (assuming you don't ask for a font with the wrong name).

The main problem appears to be ThisFont in _LoadFonts. The header comments for sfml's loadFromMemory say that the buffer must remain valid for the life of the font (so sounds like it doesn't copy the data, just uses it). But ThisFont is temporary, it is deleted when it goes out of scope after each font in the loop.
You'll need to store the data you get back from the database in something more persistent, copy it into a block of heap memory and store the pointer and size of that block in the Font_t struct as well.

hadrian

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: SF::Text not properly rendering
« Reply #2 on: September 01, 2021, 12:21:01 pm »
The Get_Font_By_Name function returns a reference to a Font. But it only contains a return for the font found path, if a font wasn't found there's no return, meaning it is returning an invalid reference.
But that's a separate issue (assuming you don't ask for a font with the wrong name).
Thank you for the advice. I am returning the first element in the array if the font isn't found, but I decided to keep it out of the code example to reduce the noise.

The main problem appears to be ThisFont in _LoadFonts. The header comments for sfml's loadFromMemory say that the buffer must remain valid for the life of the font (so sounds like it doesn't copy the data, just uses it). But ThisFont is temporary, it is deleted when it goes out of scope after each font in the loop.
You'll need to store the data you get back from the database in something more persistent, copy it into a block of heap memory and store the pointer and size of that block in the Font_t struct as well.
This was it, I had missed that note. I should not have assumed the loadFromMemory() function for fonts would work the same way as for textures. Thank you for the quick and helpful reply!