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;
}
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; }
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.
To draw rectangles, you can use the sf::RectangleShape class. It has a single attribute: The size of the rectangle.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With