Forms resize proportionally using a window in SFML 2.X

The code is outside the tutorial on the SFML site. When I compile and run it, the circle scales proportionally when the window is scaled by the user. I want the circle to remain the same.

When the screen is resized, both the correct screen dimensions and the correct circle radius are printed to the console, but the way the circle is drawn on the screen is definitely not what it claims. The circle is not visually distorted in any way, but it looks like it is being drawn with a different set of values โ€‹โ€‹relative to what is printed on the console.

antialiasingLevel

has nothing to do with the dawn of form, if that helps.

#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
    sf::ContextSettings settings;
    settings.antialiasingLevel = 8;

    sf::RenderWindow window(sf::VideoMode(200, 200), "Title", sf::Style::Default, settings);
    sf::CircleShape shape(100);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            else if (event.type == sf::Event::Resized)
            {
                std::cout << "resize: ("  << event.size.width << ',' << event.size.height << ") -> " << shape.getRadius() << std::endl;
            }
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

      

+3


source to share


1 answer


The next line added to the changed event will fix the problem.

window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));

      



The problem is that the view is not automatically scaled to fit the new resolution.

+5


source







All Articles