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

Author Topic: [OSX] setPosition inconsistency with getDesktopMode  (Read 3854 times)

0 Members and 2 Guests are viewing this topic.

mxwxz

  • Newbie
  • *
  • Posts: 4
    • View Profile
[OSX] setPosition inconsistency with getDesktopMode
« on: September 03, 2022, 09:34:34 am »
getDesktopMode returns physical pixel size in OSX retina, however in recent Mac the default scale is 2X.
Supposing the physical pixel is 3024*1964 (14 inch mac), scaled pixel (using screenshot tool or similar tools to get the window size) is 1512*982.
The following code creates a 800*600 window(in screenshot tool it shows 800*600 covering about 1/4 of the screen, meaning that 800*600 is scaled pixel and 1600*1200 in physical pixel).
But when I try to center the window using data of getDesktopMode and setPosition, it is inconsistency.
size.x / 2 and size.y / 2 produce 1512*982, but window.setPosition set the position in scaled pixel.
This means that
window.setPosition(sf::Vector2i(size.x / 2 - 400, size.y / 2 - 300));
will put the window in the right bottom corner and
window.setPosition(sf::Vector2i(size.x / 4 - 400, size.y / 4 - 300));
will center the window.

So, can we provide the API to get the scale ratio or make getDesktopMode returns the scaled pixel size? Since  all the size and position of other APIs are based on scaled pixel.

#include <SFML/Window.hpp>

int main() {
    sf::Window window(sf::VideoMode(sf::Vector2u(800, 600)), "My window");
    auto size = sf::VideoMode::getDesktopMode().size;
    // window.setPosition(sf::Vector2i(size.x / 2 - 400, size.y / 2 - 300));
    window.setPosition(sf::Vector2i(size.x / 4 - 400, size.y / 4 - 300));

    // run the program as long as the window is open
    while (window.isOpen()) {
        // check all the window's events that were triggered since the last
        // iteration of the loop
        sf::Event event;
        while (window.pollEvent(event)) {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
    }

    return 0;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [OSX] setPosition inconsistency with getDesktopMode
« Reply #1 on: September 15, 2022, 10:26:57 pm »
If this is the case, then this sounds like DPI scaling/awareness issue.
I've pinged some people who might be able to confirm this.

What version of SFML are you using?
Do you have a custom info.plist?
« Last Edit: September 15, 2022, 10:30:38 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Thrasher

  • SFML Team
  • Jr. Member
  • *****
  • Posts: 57
    • View Profile
Re: [OSX] setPosition inconsistency with getDesktopMode
« Reply #2 on: September 22, 2022, 08:41:03 am »
I can recreate this with SFML master (3a3935d) on my Retina Mac. Unsure how to fix it but I can look into what may be going on.

Thrasher

  • SFML Team
  • Jr. Member
  • *****
  • Posts: 57
    • View Profile
Re: [OSX] setPosition inconsistency with getDesktopMode
« Reply #3 on: September 22, 2022, 05:26:05 pm »
https://github.com/SFML/SFML/blob/master/src/SFML/Window/OSX/cg_sf_conversion.mm#L94

This call to `scaleOutWidthHeight` takes the scaled resolution of my display (1728 x 1117) and converts it to the actual pixel values of (3456 x 2234). Removing that function seems to result in the corrected (scaled) size being returned. That solution feels wrong but maybe it will help lead us to a better fix.

mxwxz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: [OSX] setPosition inconsistency with getDesktopMode
« Reply #4 on: September 23, 2022, 05:49:59 am »
If this is the case, then this sounds like DPI scaling/awareness issue.
I've pinged some people who might be able to confirm this.

What version of SFML are you using?
Do you have a custom info.plist?

I'm using the latest main version and no custom info.plist (I also tried custom info.plist, but NSHighResolutionCapable is irrelevant to this problem)

 

anything