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

Author Topic: Possible reasons for a garbled font?  (Read 1289 times)

0 Members and 1 Guest are viewing this topic.

miki151

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Possible reasons for a garbled font?
« on: February 08, 2015, 12:00:30 am »
I've run into a very annoying problem with text rendering. Once every few runs of the program, it looks like this:


The way it should look like:


The second screenshot is actually from the same run, except the font was enlarged by one point (it scales with window size). This hints that only one specific size gets corrupted. Sometimes only a single letter or number is corrupted, and the rest of the font looks good. Sometimes the letter is only of a wrong size.

I checked the usual stuff, sf::Font is created inside of main(), and passed around by reference. I'm running the program with valgrind, but it doesn't detect any memory corruption. The problem occurs with different fonts, on both Linux and Windows, 32bit and 64bit, although in slightly different variations.

Any hint on how to tackle this would be greatly appreciated!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: Possible reasons for a garbled font?
« Reply #1 on: February 08, 2015, 01:09:27 am »
Do you reload the font somewhere? Might be related to this.
Can you narrow down the issue and provide a minimal and compilable example?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

miki151

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: Possible reasons for a garbled font?
« Reply #2 on: February 08, 2015, 09:22:41 am »
No, I'm quite sure I don't reload it anywhere. I don't think I'll be able to narrow it to a minimal example, as the bug got triggered by a seemingly innocent commit (moving some classes around), and the code base is pretty big.

Is it possible to set some breakpoints or something inside sfml to see what might be breaking?

Btw, here's how I use the font:


int Renderer::getTextLength(string s) {
  Text t(toUnicode(s), textFont, textSize);
  return t.getLocalBounds().width;
}

void Renderer::drawText(FontId id, int size, Color color, int x, int y, String s, bool center) {
  int ox = 0;
  int oy = 0;
  Text t(s, getFont(id), size);
  if (center) {
    sf::FloatRect bounds = t.getLocalBounds();
    ox -= bounds.left + bounds.width / 2;
  }
  t.setPosition(x + ox, y + oy);
  t.setColor(color);
  renderList.push_back(
      [this, t] { display->draw(t); });
}

String Renderer::toUnicode(const string& s) {
  std::basic_string<sf::Uint32> utf32;
  sf::Utf8::toUtf32(s.begin(), s.end(), std::back_inserter(utf32));
  return utf32;
}

Font& Renderer::getFont(Renderer::FontId id) {
  switch (id) {
    case Renderer::TEXT_FONT: return textFont;
    case Renderer::TILE_FONT: return tileFont;
    case Renderer::SYMBOL_FONT: return symbolFont;
  }
}

 
« Last Edit: February 08, 2015, 09:24:49 am by miki151 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Possible reasons for a garbled font?
« Reply #3 on: February 08, 2015, 09:43:55 am »
Are you using SFML 2.2? From your code it looks like you're still using an older version.
Laurent Gomila - SFML developer

miki151

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: Possible reasons for a garbled font?
« Reply #4 on: February 08, 2015, 09:45:32 am »
I made the switch to 2.2 recently.

I just realized that while I only call drawText from the rendering thread, I call getTextLength from two different threads. Could this be the reason?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Possible reasons for a garbled font?
« Reply #5 on: February 08, 2015, 02:46:07 pm »
Quote
I call getTextLength from two different threads. Could this be the reason?
The only shared variable is the font (textFont). If it is used from the other thread, you could run into troubles, yes.
Laurent Gomila - SFML developer

 

anything