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;
}