Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shapes proportionally resize with window in SFML 2.X

Tags:

c++

sfml

The code is off the tutorial on the SFML site. When I compile and run it the circle scales proportionally as the window is scaled by the user. I want the circle to stay a constant shape.

When the screen is re-sized, both the correct screen dimensions and the correct circle radius are printed to the console, but the way the circle is drawn to the screen is definitely not what it claims to be. The circle is not visually distorted in any way, but looks as though it is being drawn with a different set of values as to what is printed to the console.

The antialiasingLevel makes no difference to the shape dawn, 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;
}
like image 326
Morgoth Avatar asked Jan 05 '15 18:01

Morgoth


People also ask

What is a window in SFML?

Windows in SFML are defined by the sf::Window class. A window can be created and opened directly upon construction: #include <SFML/Window.hpp> int main() { sf::Window window(sf::VideoMode(800, 600), "My window"); ... return 0; }

How do you center a shape in SFML?

You need to set the position of shape with shape. setPosition(x, y) . You know the width and height of the window (600px each way), and you know the radius of the circle (100px), so you can calculate the x and y that the circle needs to be moved to be centered.

How do you draw a rectangle in SFML?

To draw rectangles, you can use the sf::RectangleShape class. It has a single attribute: The size of the rectangle.


1 Answers

The following line added in the resized event will correct the problem.

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

The issue seems to be that the view is not automatically scaled to fit the new resolution.

like image 92
Morgoth Avatar answered Nov 05 '22 21:11

Morgoth