Hi, I'm working with window resize events in order to realign the user interface depending on the window's size. Therefore I discovered
RenderWindow::mapPixelToCoords() in order to align the position correctly. But unfortunately the dimension of the interface components (e.g. a
CircleShape) is stretched.
So I started handling my "own default view" by resizing it, so the shape isn't streched anymore. But I'm not sure whether this is the proper way to handle window resize or not. Any suggestions? Here's a minimal example code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window{{640, 480}, "Resize Test"};
sf::CircleShape shape{50.f};
shape.setOrigin({50.f, 50.f});
// 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();
} else 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
shape.setPosition(window.mapPixelToCoords(sf::Vector2i{window.getSize() / 2u}));
}
}
window.clear();
window.draw(shape);
window.display();
}
}
Kind regards
Glocke