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

Author Topic: Some Text is Blurry  (Read 3544 times)

0 Members and 1 Guest are viewing this topic.

Zantax

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Some Text is Blurry
« on: November 04, 2017, 07:29:23 pm »
I'm trying to make a simple starter menu for an application with two buttons, new and load. The way I figured I'd do it is have two rectangles with text on top. After running I noticed an odd issue that I can't figure out. One piece of text seems to be blurry while the other is crisp and clean. They are both using the same font, size, etc so I'm not sure why one acts differently then the other. Here's an image of it https://i.imgur.com/CGLuoyO.png and here's the code being used (sorry for the mess I'm just messing around with it atm)
        sf::RenderWindow InputWindow(sf::VideoMode(INPUT_W, INPUT_H), "Main Menu");
        InputWindow.clear(sf::Color::Black);

        sf::Font font;
        if (!font.loadFromFile("times.ttf"))
        {
                std::cout << "Font not loaded!";
        }

        int button_width = 100, button_height = 50;
        sf::RectangleShape NewButton(sf::Vector2f(button_width,button_height));
        NewButton.setPosition(sf::Vector2f((INPUT_W / 2)-button_width/2, (INPUT_H / 3 * 1) - button_height/2));
        NewButton.setFillColor(sf::Color::White);

        sf::RectangleShape LoadButton(sf::Vector2f(100, 50));
        LoadButton.setPosition(sf::Vector2f((INPUT_W / 2) - button_width/2, (INPUT_H / 3 * 2) - button_height/2));
        LoadButton.setFillColor(sf::Color::White);

        sf::Text newText ("New Map",font,20);
        sf::Text loadText("Load Map", font, 20);

        newText.setPosition(sf::Vector2f((INPUT_W / 2) - newText.getLocalBounds().width /2, (INPUT_H / 3 * 1)- newText.getLocalBounds().height /2));
        newText.setFillColor(sf::Color::Black);
        loadText.setPosition(sf::Vector2f((INPUT_W / 2) - loadText.getLocalBounds().width /2, (INPUT_H / 3 * 2) - loadText.getLocalBounds().height /2));
        loadText.setFillColor(sf::Color::Black);

        InputWindow.draw(NewButton);
        InputWindow.draw(newText);
        InputWindow.draw(LoadButton);
        InputWindow.draw(loadText);

        InputWindow.display();

On a side note anyone have a better method for centering text?  :P

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Some Text is Blurry
« Reply #1 on: November 04, 2017, 07:34:09 pm »
I hade same issue. Did just this:

sf::Text.setPosition((int)x, (int)y);

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10913
    • View Profile
    • development blog
    • Email
Re: Some Text is Blurry
« Reply #2 on: November 04, 2017, 09:05:42 pm »
A search might bring up similar topics. It differs because one is drawn at pixel perfect location and the other isn't. Make sure to round to integer positions (similar to what Achpile wrote).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Zantax

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Some Text is Blurry
« Reply #3 on: November 04, 2017, 09:20:56 pm »
I calculated the positions and put them into separate integers and that fixed the problem, thanks!

For anyone else who has trouble here's the new code.
        sf::Text newText ("New Map",font,20);
        sf::Text loadText("Load Map", font, 20);
        int x1 =  (INPUT_W / 2) - newText.getLocalBounds().width / 2;
        int y1 =  (INPUT_H / 3 * 1) - newText.getLocalBounds().height / 2;
        int x2 =  (INPUT_W / 2) - loadText.getLocalBounds().width / 2;
        int y2 =  (INPUT_H / 3 * 2) - loadText.getLocalBounds().height / 2;
        newText.setPosition(x1,y1);
        newText.setFillColor(sf::Color::Black);
        loadText.setPosition(x2,y2);
        loadText.setFillColor(sf::Color::Black);

I'm assuming there's some sort of issue with data loss with converting inside vs outside the setPosition command. Just my guess I've never looked too far into that stuff.