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

Author Topic: Font Aliasing  (Read 13310 times)

0 Members and 2 Guests are viewing this topic.

war_man333

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Font Aliasing
« on: January 03, 2015, 07:48:01 pm »
I'm aware of the fact that there are several threads on this, but all of them seem to end up with either changing source code or someone saying 'this has been fixed in the current revision (2.2)'

So, my problem is that I'm trying to use a bitmap (pixelated) font, but SFML insists on trying to anti-aliase it.

Here's an example:



How do you fix this? I just want oldshool pixelated text without having to create my own bitmap class.

 Thanks in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Font Aliasing
« Reply #1 on: January 03, 2015, 08:48:37 pm »
Where is the text in your screenshot? ???
Laurent Gomila - SFML developer

war_man333

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Font Aliasing
« Reply #2 on: January 03, 2015, 08:55:35 pm »
Sorry, it's a small 0 to the top left of the blue guy.
That's exactly the problem, the text is hard to see.

My game is enlarged 3 times, so one pixel becomes 9.
Sadly, so does the antialiased text's pixels, and so it is blurry.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Font Aliasing
« Reply #3 on: January 03, 2015, 09:03:04 pm »
A bit of a blind guess, but since I was browsing the sf::Text code recently and it might save someone some time: Could this be because sf::Font calls setSmooth(true) on the texture it uses to render glyphs? (https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L716)  I assume that's correct in most cases but maybe it should be configurable?

war_man333

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Font Aliasing
« Reply #4 on: January 03, 2015, 09:04:20 pm »
A bit of a blind guess, but since I was browsing the sf::Text code recently and it might save someone some time: Could this be because sf::Font calls setSmooth(true) on the texture it uses to render glyphs? (https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L716)  I assume that's correct in most cases but maybe it should be configurable?

How would you access this?

I think they talked about it here, but it didn't seem to work(?)

http://en.sfml-dev.org/forums/index.php?topic=5586.0
« Last Edit: January 03, 2015, 09:06:24 pm by war_man333 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Font Aliasing
« Reply #5 on: January 03, 2015, 09:13:55 pm »
Well, const_casting like that is probably undefined behavior so no surprises.  Even if it did work that'd just be a hack around SFML not providing proper access to that option.

https://github.com/SFML/SFML/issues/254 appears to have a lot of relevant discussion about the actual issue here.

war_man333

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Font Aliasing
« Reply #6 on: January 03, 2015, 09:21:33 pm »
Well, const_casting like that is probably undefined behavior so no surprises.  Even if it did work that'd just be a hack around SFML not providing proper access to that option.

https://github.com/SFML/SFML/issues/254 appears to have a lot of relevant discussion about the actual issue here.

I took at look at this, and if you see the last post, it says 'Since this seems resolved with the latest revision, I'll close the issue.' - but I'm not sure what he means by this. In general, the fix seems to be a hackaround as far as I can see.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Font Aliasing
« Reply #7 on: January 03, 2015, 11:31:17 pm »
Quote
My game is enlarged 3 times, so one pixel becomes 9.
That's what causes the problem. Font rendering is exact, and thus not blurry, as long as nothing is stretched. But if one texel = several pixels, texture filtering applies and you get the antialiased result.

The const_cast is an ugly hack, but you can use it to check if disabling texture smoothing would indeed give you the expected result. This would allow us to progress on this issue: if the result is ok then we can discuss the addition of this feature to the public API, and if it's not then it means there's a problem somewhere else that may be fixed / improved.
Laurent Gomila - SFML developer

war_man333

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Font Aliasing
« Reply #8 on: January 03, 2015, 11:57:06 pm »
Quote
My game is enlarged 3 times, so one pixel becomes 9.
That's what causes the problem. Font rendering is exact, and thus not blurry, as long as nothing is stretched. But if one texel = several pixels, texture filtering applies and you get the antialiased result.

The const_cast is an ugly hack, but you can use it to check if disabling texture smoothing would indeed give you the expected result. This would allow us to progress on this issue: if the result is ok then we can discuss the addition of this feature to the public API, and if it's not then it means there's a problem somewhere else that may be fixed / improved.

I tried this, no luck though. It looks exactly the same, blurry.

ScreenText::ScreenText(double posx, double posy, std::string font, sf::Color color, int size, std::string text) : Entity(posx,posy)
{
        x = posx;
        y = posy;
        font_.loadFromFile(font+".ttf"); //LCD_Solid.ttf
        const_cast<sf::Texture&>(font_.getTexture(size)).setSmooth(false);
        text_.setFont(font_);
        text_.setStyle(sf::Text::Regular);
        color_ = color;
        text_.setColor(color_);
        text_.setCharacterSize(size);
        text_.setString(text);
        textAlpha = 255;
        removed = false;
}


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Font Aliasing
« Reply #9 on: January 03, 2015, 11:59:09 pm »
Is the final position of the text rounded, or decimal?
Laurent Gomila - SFML developer

war_man333

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Font Aliasing
« Reply #10 on: January 04, 2015, 12:05:43 am »
Is the final position of the text rounded, or decimal?

It's decimal. I see where you're going at, I'll try rounding it. Makes sense.

No effect.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Font Aliasing
« Reply #11 on: January 04, 2015, 12:09:38 am »
Can you provide a complete and minimal example that reproduces the problem?
Laurent Gomila - SFML developer

war_man333

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Font Aliasing
« Reply #12 on: January 04, 2015, 12:20:59 am »
Can you provide a complete and minimal example that reproduces the problem?

Try this.
Includes a main and a font.
It's close but it's still blurry.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Font Aliasing
« Reply #13 on: January 04, 2015, 10:31:41 am »
Thanks.

To get the pixelated look, I had to disable texture smoothing and render glyphs in mono mode (2-color bitmaps). So this is indeed a case not covered by the latest modifications in sf::Font.

This brings back the idea of providing a "mode" when loading a font: antialiased or pixelated. This could also apply to fonts that are not natively pixelated.
Laurent Gomila - SFML developer

war_man333

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Font Aliasing
« Reply #14 on: January 04, 2015, 01:04:11 pm »
Thanks.

To get the pixelated look, I had to disable texture smoothing and render glyphs in mono mode (2-color bitmaps). So this is indeed a case not covered by the latest modifications in sf::Font.

This brings back the idea of providing a "mode" when loading a font: antialiased or pixelated. This could also apply to fonts that are not natively pixelated.

So you had to recompile the source? I guess I'll just work on some other things in my game than text, if you mean that it will be added to SFML in the near future.  :)