crxyz
crxyz

Reputation: 977

SFML Window Resizing is very ugly

When I resize my sfml window, when I cut resize to make it smaller and resize to make it larger, it gives you a really weird effect. A cut out green circle A "laser beam" coming out from the green circle

How do I make the resizing more prettier? The code is from the installation tutorial for code::blocks. Code (same as the code in the installation tutorial for code::blocks on the sfml website):

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

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

    return 0;
}

Upvotes: 1

Views: 3349

Answers (1)

Martin Sand
Martin Sand

Reputation: 518

You need to manage the resize of the window. Otherwise the coordinates are wrong. Here is an excerpt of your code with the solution. Credits go to the author of this forum post, this is where I once found it when I was looking for a solution: https://en.sfml-dev.org/forums/index.php?topic=17747.0

Additionally you can set the new coordinates based on the new size. The link gives you more information.

// create own view
sf::View view = window.getDefaultView();

while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();

        if (event.type == sf::Event::Resized) {
            // resize my view
            view.setSize({
                    static_cast<float>(event.size.width),
                    static_cast<float>(event.size.height)
            });
            window.setView(view);
            // and align shape
        }
    }

Upvotes: 5

Related Questions