SFML community forums

Help => Graphics => Topic started by: war_man333 on January 03, 2015, 07:48:01 pm

Title: Font Aliasing
Post by: war_man333 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:

(http://i.imgur.com/eNhC0c0.png)

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

 Thanks in advance.
Title: Re: Font Aliasing
Post by: Laurent on January 03, 2015, 08:48:37 pm
Where is the text in your screenshot? ???
Title: Re: Font Aliasing
Post by: war_man333 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.
Title: Re: Font Aliasing
Post by: Ixrec 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?
Title: Re: Font Aliasing
Post by: war_man333 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
Title: Re: Font Aliasing
Post by: Ixrec 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.
Title: Re: Font Aliasing
Post by: war_man333 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.
Title: Re: Font Aliasing
Post by: Laurent 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.
Title: Re: Font Aliasing
Post by: war_man333 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;
}

Title: Re: Font Aliasing
Post by: Laurent on January 03, 2015, 11:59:09 pm
Is the final position of the text rounded, or decimal?
Title: Re: Font Aliasing
Post by: war_man333 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.
Title: Re: Font Aliasing
Post by: Laurent on January 04, 2015, 12:09:38 am
Can you provide a complete and minimal example that reproduces the problem?
Title: Re: Font Aliasing
Post by: war_man333 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.
Title: Re: Font Aliasing
Post by: Laurent 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.
Title: Re: Font Aliasing
Post by: war_man333 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.  :)
Title: Re: Font Aliasing
Post by: Laurent on January 04, 2015, 01:54:57 pm
Quote
So you had to recompile the source?
Yes. Using the mono mode for glyphs cannot be done externally, even with an ugly hack.

Quote
if you mean that it will be added to SFML in the near future.
Don't wait for it. It's not validated, and even if it is, nobody knows when it's going to be implemented.
Title: Re: Font Aliasing
Post by: war_man333 on January 04, 2015, 04:24:07 pm
Quote
So you had to recompile the source?
Yes. Using the mono mode for glyphs cannot be done externally, even with an ugly hack.

Quote
if you mean that it will be added to SFML in the near future.
Don't wait for it. It's not validated, and even if it is, nobody knows when it's going to be implemented.

Alright.. by any chance can you tell me what you changed specifically, and maybe drop a link to where to a recompile guide...?
Title: Re: Font Aliasing
Post by: Laurent on January 04, 2015, 05:39:33 pm
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L485
FT_LOAD_TARGET_NORMAL -> FT_LOAD_TARGET_MONO

https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L503
FT_RENDER_MODE_NORMAL -> FT_RENDER_MODE_MONO

https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L716
setSmooth(true) -> setSmooth(false)

http://www.sfml-dev.org/tutorials/2.2/compile-with-cmake.php
Title: Re: Font Aliasing
Post by: war_man333 on January 04, 2015, 05:51:28 pm
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L485
FT_LOAD_TARGET_NORMAL -> FT_LOAD_TARGET_MONO

https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L503
FT_RENDER_MODE_NORMAL -> FT_RENDER_MODE_MONO

https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L716
setSmooth(true) -> setSmooth(false)

http://www.sfml-dev.org/tutorials/2.2/compile-with-cmake.php

The compile part looks a bit complicated :(
I'll save it for when I get too tired of the current font-rendering.
Thanks :)
Title: Re: Font Aliasing
Post by: Ixrec on January 04, 2015, 06:05:21 pm
The compile part looks a bit complicated :(

In my personal experience as a newbie, it's actually slightly easier to build SFML myself than trying to use the pre-built libraries, especially on Mac/Linux where the dev environment is much harder to misconfigure.  Your mileage may vary.
Title: Re: Font Aliasing
Post by: Laurent on January 04, 2015, 07:20:32 pm
Quote
The compile part looks a bit complicated
Try it, and let us know if anything was actually complicated for you ;)
Title: Re: Font Aliasing
Post by: war_man333 on January 04, 2015, 07:47:53 pm
I pulled myself together and did it, text looks great now :D
Hopefully it'll be added as an option at some point, otherwise people can look at this thread! :)
Title: Re: Font Aliasing
Post by: Laurent on January 04, 2015, 08:30:57 pm
Feel free to open a discussion on the "Feature request" forum, with a link to this thread, so that the idea can be discussed with other members of the SFML team, and possibly added to the task list if it's validated.