About Monkey 2 › Forums › Monkey 2 Programming Help › DPI independent GUI system
This topic contains 12 replies, has 4 voices, and was last updated by
Danilo
2 years, 2 months ago.
-
AuthorPosts
-
January 21, 2017 at 8:03 am #6681
Do we have functions yet, to get the DPI and screen size + screen resolution
on all available platforms?
I need such functions for Font sizes and scaling GUI controls. I think it’s essential, because
it’s a big difference if you run an application on a 96 DPI monitor, or a 220 DPI monitor,
or a 264 DPI iPad Pro.
We can’t use fixed font & control sizes because of this, so some functions to get
the required system informations are essential, in my opinion.
Same thing applies to Printers, because it’s a big difference if you print with
300, 600, 1200, or 2400 DPI.Your app/game may need to look different on a small iPhone, bigger iPad Pro,
and 27″ or 32″ computer display.January 21, 2017 at 10:29 am #6692Well it is all based on sdl2 so the best place would be to look there.
My initial finding is:
Monkey1234int SDL_GetDisplayDPI(int displayIndex,float* ddpi,float* hdpi,float* vdpi)January 21, 2017 at 11:38 am #6697would it be adequate enough for your purposes to scale to a fixed ratio (ie 16:9) ?
you can render to a large “virtual resolution” and downscale it depending on the screen size
I posted some code that also scissor clips the letter box as needed.
January 21, 2017 at 11:42 am #6698http://monkey2.monkey-x.com/forums/topic/monkey-2-app-template/#post-6538
you’ll need to add
[/crayon]Monkey123[crayon-5cb9b52d17c59167808256 inline="true" ]canvas.Scissor=New Recti( Xoffset*scaleX,Yoffset*scaleY, (960+Xoffset)*scaleX, (540+Yoffset)*scaleY )just before drawing on the canvas
the only caveat might be if a small device has sufficient resources to cope with a large virtual resolution
January 21, 2017 at 10:16 pm #6703Thanks for the hint, AdamStrange!
@codifies:
Scaling is not an option for a GUI system. You may have seen this on Windows,
if you have a high DPI display and set the Windows scaling to 150%, for example.
If an application is not DPI-aware, the OS is scaling the controls, and the result
is blurry looking controls and fonts.
If an application is aware of DPI, it looks clear and crisp on all displays.January 21, 2017 at 11:42 pm #6704Scalimg down has always worked finne for me..?
January 22, 2017 at 12:18 am #6705I have a feeling the high dpi functionality is buggy at SDL2’s end (or within graphics drivers), at least on the MacOS platform. I run the following code on my 5k Retina iMac and receive incorrect results. It states that the DPIs are approx 108 and the desktop resolution is 2560×1440, all incorrect.
EDIT: Just had a thought. The 27″ 5k iMac has a stated resolution of 5120×2880 at 218 PPI. The values I get from my code are half of those values. Might not be a “bug” after all, just “unexpected behaviour”.
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637[crayon-5cb9b52d1f3ae709885473 inline="true" ]#Import "<sdl2>"#Import "<std>"#Import "<mojo>"Using std..Using mojo..Using sdl2..Class MyWindow Extends WindowMethod New()Super.New( "HIGH DPI", 1024, 768, WindowFlags.Resizable | WindowFlags.HighDPI)Local ddpi:FloatLocal hdpi:FloatLocal vdpi:FloatSDL_GetDisplayDPI(0, Varptr(ddpi), Varptr(hdpi), Varptr(vdpi))Print "ddpi: " + ddpiPrint "hdpi: " + hdpiPrint "vdpi: " + vdpiEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawText("Desktop resolution: " + App.DesktopSize.X + " x " + App.DesktopSize.Y, 10, 10)EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndOn the positive side, the undocumented WindowFlags.HighDPI creation flag works nicely, though I need to load my fonts at a larger size, obviously. Text is very sharp. (Ted2 makes use of that flag too).
My next project is very text heavy, so I also would like bug-free DPI-related functionality, if possible…
January 22, 2017 at 1:22 am #6710January 22, 2017 at 2:03 am #6711Using the flag WindowFlags.HighDPI, the window size gets doubled automatically
when moving a window from normal screen to a HiDPI screen.
But MX2 does not update the render screen size. With Ted2Go it looks like this:Attachments:
January 22, 2017 at 2:26 am #6715Do you need to double the font sizes?
I’d assume it depends on the amount of DPI. Higher amounts would require higher multiples font size. IIRC, Ted2 fonts are really small by default. I have to use the “zoom” functionality of the “View” menu item – but only on the first run.
Not sure about your problem above. Would have to see the relevant code… You might need an additional RequestRender call somewhere or to manually adjust View rects, etc…
January 22, 2017 at 2:39 am #6716The problem in the image above is not my code. It is the Ted2Go editor when opened on
a normal screen, and then moved to a Hi-DPI screen. It’s a bug, or missing feature,
in Ted2(go) or MX2.
You can test it if you switch to a Hi-DPI mode while Ted2(Go) is running.In my opinion the Ted2 fonts are too big, on a normal screen. Using ‘zoom out’ 2 or 3 times helps.
On macOS you could probably use:
Objective-C1[[NSScreen mainScreen] userSpaceScaleFactor];to get the scale factor for the fonts. But this example shows it only for
the main screen. It’s a bit more complicated when you use mixed screens and
move a window from 100 DPI screen to a 200 DPI screen at runtime.
NSWindow has also such a method, and it’s probably better to ask the window
for the scale factor, so if the window gets moved onto another screen, the scale factor
should change/update.January 22, 2017 at 4:22 am #6719@danilo. Very interesting, thanks for the info… Don’t you just love all the complexity and “edge cases” associated with software development?
January 22, 2017 at 4:29 am #6720Yes, I love those cases… and also hate them sometimes… with the goal of fixing
it in the development environment I currently use.
I am testing the DPI-awareness of MX2 to understand the system and make it better
and more useful. Maybe perfect – as perfect would be nice. Perfection is a nice goal. -
AuthorPosts
You must be logged in to reply to this topic.
