1
Window / Re: Viewports
« on: August 02, 2014, 01:22:10 pm »So... the top-left of the viewport is at (50,50)?
If so, you could apply the same calculations to find out 50 pixels. e.g. viewportOffsetX = 50 / windowWidth.
Then, you could just add it to both of the x ratios.
Same for y.
Thanks! This works.
int viewportOffsetX = 50;
int viewportOffsetY = 50;
int viewportWidth = 200;
int viewportHeight = 200;
int windowWidth = window.getSize().x;
int windowHeight = window.getSize().y;
sf::View view(sf::FloatRect(static_cast<float>(viewportOffsetX), static_cast<float>(viewportOffsetY),
static_cast<float>(viewportWidth), static_cast<float>(viewportHeight)));
float viewportOffsetXRatio = static_cast<float>(viewportOffsetX) / windowWidth;
float viewportOffsetYRatio = static_cast<float>(viewportOffsetY) / windowHeight;
float viewportWidthRatio = static_cast<float>(viewportWidth) / windowWidth;
float viewportHeightRatio = static_cast<float>(viewportHeight) / windowHeight;
view.setViewport(sf::FloatRect(viewportOffsetXRatio, viewportOffsetYRatio,
viewportWidthRatio, viewportHeightRatio));
window.setView(view);
int viewportOffsetY = 50;
int viewportWidth = 200;
int viewportHeight = 200;
int windowWidth = window.getSize().x;
int windowHeight = window.getSize().y;
sf::View view(sf::FloatRect(static_cast<float>(viewportOffsetX), static_cast<float>(viewportOffsetY),
static_cast<float>(viewportWidth), static_cast<float>(viewportHeight)));
float viewportOffsetXRatio = static_cast<float>(viewportOffsetX) / windowWidth;
float viewportOffsetYRatio = static_cast<float>(viewportOffsetY) / windowHeight;
float viewportWidthRatio = static_cast<float>(viewportWidth) / windowWidth;
float viewportHeightRatio = static_cast<float>(viewportHeight) / windowHeight;
view.setViewport(sf::FloatRect(viewportOffsetXRatio, viewportOffsetYRatio,
viewportWidthRatio, viewportHeightRatio));
window.setView(view);
Let me know if this works. Not certain of my calculations
This is why i call it unintutive. There will be method for set viewport without this calculation (IMHO setViewport internally doing exact reverse calculations, so there are round errors).