17cupsofcoffee / tetra

🎮 A simple 2D game framework written in Rust
MIT License
909 stars 63 forks source link

Add methods to get device pixel ratio #249

Closed sumibi-yakitori closed 3 years ago

sumibi-yakitori commented 3 years ago

This method allows you to get the pixel ratio of the display on which the current window is displayed, which is more convenient than SDL_GetDisplayDPI

References:

17cupsofcoffee commented 3 years ago

This looks good, and is basically the same as how I've seen other engines implement this 👍

The only thing I'm not sure about is returning seperate values for the width and height ratio - is there any scenario where those values would not be the same? If not, then it might be worth just returning a single value (which is what Love2D and the web API you linked do).

sumibi-yakitori commented 3 years ago

Since I thought that there might be some very strange display devices in the world, and that there might be a rare possibility that they might appear in the future, and since I thought that a non-square HIDPI scale might be set by hacking, I decided to return two values in this function.

However, I think all of these cases are either very rare or impossible, so I have no objection to modifying this function to return a single value.

Which would be better?

windows - Are pixels ever not a square on a monitor? - Stack Overflow

sumibi-yakitori commented 3 years ago

It seems to be guaranteed that x and y will be on the same scale in Windows.

GetDpiForMonitor function (shellscalingapi.h) - Win32 apps | Microsoft Docs

The values of dpiX and dpiY are identical. You only need to record one of the values to determine the DPI and respond appropriately.

17cupsofcoffee commented 3 years ago

Done a little digging:

So while I think theoretically displays can have non-square pixels, it doesn't look like OSes really expose this in any meaningful way, so it's probably better to just return a single value. If anyone comes along that desperately needs non-uniform scale factors, I can change it later/add a new method 😄

sumibi-yakitori commented 3 years ago

Fixed. Thanks for the thorough investigation!

If you don't like the name of this function, feel free to change it to get_dpi_scale or whatever you like.

17cupsofcoffee commented 3 years ago

Thanks for the PR! I may switch the name to your suggestion, just for consistency with the existing high_dpi flag (and cause it's a little shorter 😛), but happy with the implementation

17cupsofcoffee commented 3 years ago

@sumibi-yakitori FYI, I've also added a get_physical_size function on the main branch as part of another change, which will give you the unscaled size of the window. Might be useful to you :)

sumibi-yakitori commented 3 years ago

Thank you for taking the time to tell me about this.

Lately, I've been spending a lot of time thinking about whether I should get out of Tokyo, where I live, and I haven't been able to concentrate on development work...