SFML community forums

Help => Graphics => Topic started by: miki151 on February 08, 2015, 12:00:30 am

Title: Possible reasons for a garbled font?
Post by: miki151 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:
(http://blkrs.com/~michal/font_error1.png)

The way it should look like:
(http://blkrs.com/~michal/font_error2.png)

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!
Title: Re: Possible reasons for a garbled font?
Post by: eXpl0it3r on February 08, 2015, 01:09:27 am
Do you reload the font somewhere? Might be related to this (http://en.sfml-dev.org/forums/index.php?topic=14019.0).
Can you narrow down the issue and provide a minimal and compilable example?
Title: Re: Possible reasons for a garbled font?
Post by: miki151 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;
  }
}

 
Title: Re: Possible reasons for a garbled font?
Post by: Laurent 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.
Title: Re: Possible reasons for a garbled font?
Post by: miki151 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?
Title: Re: Possible reasons for a garbled font?
Post by: Laurent 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.