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

Author Topic: Rendering clear text (SFML 2.0)  (Read 4760 times)

0 Members and 1 Guest are viewing this topic.

Blublop

  • Newbie
  • *
  • Posts: 13
    • View Profile
Rendering clear text (SFML 2.0)
« on: September 02, 2012, 10:50:55 pm »
I know there are many post about blurry text, but I feel the need to open a new Thread, since there are things I don't understand yet.

So, I tried to print a small text and discovered  that (with the font I used) SFML can only draw clear texts , when certain sizes are used.
In my case SFML only prints clear, if I use the size 8 or a multiple of it.
Here is image which shows 9 sf::Text object starting with the character size 8 and incrementing to 16.

As you can see, all texts are printed clear(but some still ugly). To achieve this I used the object with the size 8 and scaled every string with it and had to turn smooth of the Font off. Note that all multiples of 8 would draw fine without such a trick. (btw the font used is Pokemon GB.ttf from http://www.fontspace.com/jackster-productions/pokemon-gb)



On the right there are two screenshots from notepad written with the same font.
The top extract is drawn with the size 6 and the bottom one is written with the size 12, so that charactersize 8 is linked to font size 6 in Notepad and 16 equals 12.
So what exactly is meant with charactersize, since it doesnt provide the same result given by notepad, when using equal sizes. This would be my first question.

It is clear to me, that with this technique only multiples of 8 can be drawn correctly , since they only will result in an even scale factor.
Not using this technique will leave most sizes blurry.

The example above is given by the following code :
#include <SFML/Graphics.hpp>
#include <iostream>

//#define CLEAR_FONT // commented, SFML will only draw texts clear with size % 8 == 0
int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(320, 180), "SFML window 2.0");

    // Create a graphical text to display
    sf::Font font;
    if (!font.loadFromFile("GB.ttf"))
        return EXIT_FAILURE;
#ifdef CLEAR_FONT
    // 'old' Sfml 1.6 trick
    const sf::Texture* tex = &font.getTexture(8);
    ((sf::Texture*) tex)->setSmooth(false);


    sf::Text MyTexts[9] = { sf::Text("Example", font, 8),  
                            sf::Text("Example", font, 8),
                            sf::Text("Example", font, 8),
                            sf::Text("Example", font, 8),
                            sf::Text("Example", font, 8),
                            sf::Text("Example", font, 8),
                            sf::Text("Example", font, 8),
                            sf::Text("Example", font, 8),
                            sf::Text("Example", font, 8)};

    for (unsigned int i = 1;i < 9;i++)
        MyTexts[i].setScale( (i+8) / 8.0 , (i+8) / 8.0);   // every text will be clear, even when zooming in ( e.g. by resize)
#else

    sf::Text MyTexts[9] = { sf::Text("Example", font, 8),  // will be clear, but blurry when zooming in ( e.g. by resize)
                            sf::Text("Example", font, 9),
                            sf::Text("Example", font, 10),
                            sf::Text("Example", font, 11),
                            sf::Text("Example", font, 12),
                            sf::Text("Example", font, 13),
                            sf::Text("Example", font, 14),
                            sf::Text("Example", font, 15),
                            sf::Text("Example", font, 16)}; // will be clear, but blurry when zooming in ( e.g. by resize)

#endif

    float OffSet_Y = 1;
    for (unsigned int i = 0;i < 9;i++)
    {
        MyTexts[i].setPosition(1.0,OffSet_Y);

        OffSet_Y += MyTexts[i].getGlobalBounds().height + 2;
    }

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // Clear screen
        window.clear();

        // Draw the string
        for (unsigned int i = 0;i < 9;i++)
            window.draw(MyTexts[i]);

        // Update the window
        window.display();
    }

     return EXIT_SUCCESS;
 }
 

So using this font, I'm forced to only use the character size 8,16,24 and so on.
(If the picture is not available, it's in the attachment)

[attachment deleted by admin]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Rendering clear text (SFML 2.0)
« Reply #1 on: September 02, 2012, 10:57:19 pm »
Quote
So what exactly is meant with charactersize, since it doesnt provide the same result given by notepad, when using equal sizes. This would be my first question.
Text editors use points, whereas SFML uses pixels.

I'm sorry but your main problem is confusing me. What is wrong exactly? What is shown here, the version with CLEAR_FONT defined or the other one? Can you show a comparison of both?
Laurent Gomila - SFML developer

Blublop

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rendering clear text (SFML 2.0)
« Reply #2 on: September 02, 2012, 11:20:27 pm »
Text editors use points, whereas SFML uses pixels.

I'm sorry but your main problem is confusing me. What is wrong exactly? What is shown here, the version with CLEAR_FONT defined or the other one? Can you show a comparison of both?

Thanks for that info, didn't know that since 1.6

Here is a image with both versions :


obviously I failed to explain my question.
I'm wondering if it's possible to print a clear/non-smooth text between the size 8 and 16.
When printing with such a size I end up with characters with parts streched out ugly, like the 3rd and 5th text in the left example (the a's are readable , but ugly)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Rendering clear text (SFML 2.0)
« Reply #3 on: September 03, 2012, 01:07:11 am »
Have you tried to set the font's texture as not-smooth, but use character size instead of scale (a mix of your solutions)?

If this is not enough, you'll have to wait until it is properly implemented:
https://github.com/SFML/SFML/issues/254
Laurent Gomila - SFML developer