SFML community forums

Help => General => Topic started by: catalinnic on November 21, 2019, 06:42:49 pm

Title: Scaling
Post by: catalinnic on November 21, 2019, 06:42:49 pm
I'm developing a game, and I want to support different resolutions, so I need to make a scaling algorithm. This is what i came up with:
void Player::setViewSize(sf::Vector2f ViewSize)
{
    const sf::Vector2f defaultSize{960, 540}; // A size constant. I came up with it because it is a quarter of 1080p
    float scaleFactor = 1;
    while(ViewSize.x / scaleFactor > defaultSize.x || ViewSize.y / scaleFactor > defaultSize.y)
        scaleFactor++;
    PlayerView.setSize(ViewSize.x / scaleFactor, ViewSize.y / scaleFactor);
}
 

Are there better scaling algorithms? If so? Can you recommend one?
*edit: Please Remove the other 2 posts
Title: Re: Scaling
Post by: Hapax on December 01, 2019, 11:54:00 pm
Why do you need to scale the view if the view size can be any value under that 'default' size?

What relevance is that 'default' size in this case?

Following your logic, an actual "ViewSize" of 1080p would cause your scaling to divide by 3, not the expect 2 (because of your comparisons and your use of floats instead of integers).

Wouldn't it be easier to just use a set value for the view. It will automatically scale that to match the window anyway.

Something else to note is that none of this takes into account ratios of displays/sizes (or even pixel ratios).
Title: Re: Scaling
Post by: catalinnic on December 04, 2019, 06:05:06 pm
Good point. What about this:
void Player::setViewSize(sf::Vector2u NewSize)
{
    const sf::Vector2u defaultSize{960, 540};
    unsigned int scaleFactor = 1;
    while(NewSize.x / scaleFactor > defaultSize.x || NewSize.y / scaleFactor > defaultSize.y)
        scaleFactor++;
    PlayerView.setSize(static_cast<float>(NewSize.x / scaleFactor), static_cast<float>(NewSize.y / scaleFactor));
}
Title: Re: Scaling
Post by: Hapax on December 04, 2019, 07:44:24 pm
Not sure what you fixed, to be honest but I will just remind you that you can divide an SFML vector by a scalar, so for your final line, you can just do this:
PlayerView.setSize(sf::Vector2f(NewSize / scaleFactor));

Again, you could just use whichever size view you want to work with and it will automatically translate that size (stretch) to fit the window, which seems to be your intention anyway?