SFML community forums

Help => Graphics => Topic started by: eggw on June 06, 2016, 08:53:02 pm

Title: Fixed viewport possible?
Post by: eggw on June 06, 2016, 08:53:02 pm
I have a view used to draw the black bar at the bottom. I want that black bar to have a fixed size, and to always be in the bottom-center of the window. By default, the window size is as such, and everything is fine:

(https://i.imgur.com/D7NN60A.png)

What I WANT to happen when I increase the size of the screen is have the black bar remain the same size:

(https://i.imgur.com/bQKZYVB.png)

But instead, the black bar scales to the new window size (due to SFML viewports being defined as a ratio of the screen) and this happens:

(https://i.imgur.com/nkijMoV.png)

Is there a solution for this, or is it impossible due to the way SFML's viewports work?
Title: Re: Fixed viewport possible?
Post by: Laurent on June 06, 2016, 09:14:07 pm
This is a non-problem. You can very easily switch from ratio to fixed size by multiplying / dividing by the window size whenever it changes.

const sf::Vector2f barSize(... fixed size...);

on resize event
{
    viewport.width = barSize.x / window.width;
    viewport.height = barSize.y / window.height;
    viewport.left = (1 - viewport.width) / 2;
    viewport.top = 1 - viewport.height;
}

(disclaimer: untested inaccurate pseudo-code)
Title: Re: Fixed viewport possible?
Post by: eggw on June 08, 2016, 03:17:01 am
Thanks Laurent, it worked like a charm.