Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Scaling  (Read 91 times)

0 Members and 1 Guest are viewing this topic.

catalinnic

  • Newbie
  • *
  • Posts: 19
    • View Profile
Scaling
« 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
« Last Edit: November 21, 2019, 06:47:12 pm by catalinnic »

Hapax

  • Hero Member
  • *****
  • Posts: 2789
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Scaling
« Reply #1 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).
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

catalinnic

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Scaling
« Reply #2 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));
}
« Last Edit: December 04, 2019, 06:16:44 pm by catalinnic »

Hapax

  • Hero Member
  • *****
  • Posts: 2789
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Scaling
« Reply #3 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?
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter