About Monkey 2 › Forums › Monkey 2 Programming Help › How can I get the device Width and Height?
This topic contains 15 replies, has 7 voices, and was last updated by
arpie
1 year, 2 months ago.
-
AuthorPosts
-
February 2, 2018 at 12:38 am #13416
On Monkey1 I was able to get the Device Width and Height using DeviceWidht() or DeviceHeight()
I did use the code below to get the device width and height from Monke2, and tested on Android and iOS.
It gets the android screen sizes but not from iOS.
How can I get the device w x h before setting my app Window Widht and Height?
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowField _virtualY:FloatField DeviceWidth:Int=0Field DeviceHeight:Int=0Field size:Vec2iMethod New( title:String, width:Int, height:Int, flags:WindowFlags = Null )Super.New( title, width, height, flags )Layout= "letterbox"'"stretch"ClearColor=Color.Red_virtualY=288End MethodMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.Color=Color.Bluecanvas.DrawRect( 1,1,Width-2,Height-2 )canvas.Color=Color.Whitecanvas.DrawText( "DevWidth:"+DeviceWidth+" DevHeight:"+DeviceHeight,Width/2,Height/2,.5,.5 )canvas.DrawText( "Virtual Size="+size,Width/2,Height*0.6,.5,.5 )canvas.DrawText( "CanvasW:"+Width+" CanvasH:"+Height,Width/2,Height*0.3,.5,.5 )End MethodMethod OnMeasure:Vec2i() OverrideDeviceWidth=Frame.WidthDeviceHeight=Frame.HeightLocal scale:=Float(Frame.Height)/Float(_virtualY)' Local size:=New Vec2i( Frame.Width/scale,_virtualY )size=New Vec2i( Frame.Width/scale,_virtualY )'Print "Virtual Size="+sizeReturn sizeEndEnd ClassFunction Main()New AppInstanceNew MyWindow( "Hello",640,480,WindowFlags.Resizable )App.Run()EndFebruary 2, 2018 at 12:54 am #13417I haven’t done any iOS coding in Monkey2 so I’m not sure.
But I do know there’s App.DesktopSize so maybe give that a go?February 2, 2018 at 9:12 am #13420@apc : can you explain why you need it?
Code like the one below will give you a fullscreen window on IOS, and then you have access to Width and Height and stuff like
´Method OnLayout()
Print Self.RenderRect)
End Method´
Monkey123456Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )EndFebruary 2, 2018 at 3:51 pm #13422@Hezcore: Thank you for your suggestion I will give it a try.
@Diffrenzy: I want my app to look the same on devices with different resolution. I build my sprite sheets with high resolution images , than based on the screen resolution I scale down the images, so they look the same on all screens. That is how I did in Monkey 1.
I would like to hear from @Planiax, @Diddy and other people how do they handle iOS and Android screen resolution with Monkey 2?
Also some apps I have to detect the screen rotation, I am using the OnMeasure() to detect the orientation change, as it is posted above.
I have spent a lot o time searching this web site and Github to find answers and get my apps going. But I spent more time with trial and error than developing my app.
February 2, 2018 at 3:54 pm #13423I build my images based on resolution too, why can’t you do that in New() ?
[EDIT]: Testing this on IOS now, and I can’t seem to make my Ipad 3 go beyond 1024 x 768?
[EDIT2]: Is this the reason: https://stackoverflow.com/questions/11342081/how-to-setup-xcode-project-for-retina
[EDIT3] : On iPhone 6 i get 568 x 320 , hardware res is 750×1334
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowField _nw:IntField _nh:IntMethod New( title:String="Size test mojo app",width:Int=2048,height:Int=1536,flags:WindowFlags=Null )Super.New( title,width,height,flags )_nw = Width_nh =HeightEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawText( "NWidth: " + _nw + " NHeight: " + _nh,Width/2,Height/2,.5,.5 )canvas.DrawText( "Width: " + Width + " Height: " + Height,Width/2,Height/2+40,.5,.5 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndFebruary 2, 2018 at 6:02 pm #13424Could it be that your XCode project doesn’t have launch screen images for your device resolution?
https://developer.apple.com/ios/human-interface-guidelines/icons-and-images/launch-screen/
From what I understand, iOS uses them to determine if your app supports native resolution of the device, and if not, the OS will scale your app (and hence the app will get some scaled width and height info instead of the actual pixels).
February 2, 2018 at 9:17 pm #13425I’ve made progress but iPhone is still messed up.
Look at this screenshot, it should be 1364 x 750 and centered.

Adding the HighDPI flag (thanks Ethernaut ) gives higher res.
Adding the lauchimages (thanks skid and secondgear) (I used https://github.com/raphaelhanneken/Iconizer/releases ) makes the iPad 3 goto 2048 x 1536 with is correct.
Android phone goes to 1920 x 1080 correctly.
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowField _nw:IntField _nh:IntMethod New( title:String="Size test mojo app",width:Int=1364,height:Int=750,flags:WindowFlags=WindowFlags.HighDPI | WindowFlags.Fullscreen)Super.New( title,width,height,flags )_nw = Width_nh =HeightEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.Clear(Color.Red)canvas.Color = Color.DarkGreycanvas.DrawRect(RenderRect)canvas.Color = Color.Whitecanvas.DrawText( "NWidth: " + _nw + " NHeight: " + _nh,Width/2,Height/2,.5,.5 )canvas.DrawText( "Width: " + Width + " Height: " + Height,Width/2,Height/2+40,.5,.5 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndFebruary 3, 2018 at 4:23 pm #13445Thanks Deffrenzy,
I got the same results on iPhone and Android.
I never heard about these Windows settings ” HighDPI and Fullscreen” for mobile devices !
Monkey1Method New( title:String="Size test mojo app",width:Int=1364,height:Int=750,flags:WindowFlags=WindowFlags.HighDPI | WindowFlags.Fullscreen)Is there a place in this forum that is specific about iOS and Android?
There is a question that remains, How do I detect the device screen size before I can set the Windows size?
February 3, 2018 at 8:26 pm #13448@apc : My tests show two important things:
You can’t rely on app.DesktopSize on mobile, because at least on iPhone it returns a
wrong value.only non retina size.after Super.New( title,width,height,flags ) you can use Width and Height values, so that is a safe place to init graphics.
There is still 1 problem remaining: I have found out why I am getting the 1136×640 window: If the iphone has Settings, display & Brightness, “Display Zoom.” turned on, Monkey/SDL does something wrong.
[EDIT]: Posted an issue on github : https://github.com/blitz-research/monkey2/issues/331
This runs well for me (but does not solve the zoom issue)
PS: I only think you need the HighDPI and Fullscreen flags.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowField _nw:IntField _nh:IntMethod New( title:String="Size test mojo app",width:Int,height:Int,flags:WindowFlags=Null )Super.New( title,width,height,flags )_nw = Width_nh = HeightEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.Clear(Color.Red)canvas.Color = Color.DarkGreycanvas.DrawRect(RenderRect)canvas.Color = Color.Whitecanvas.DrawText( "NWidth: " + _nw + " NHeight: " + _nh,Width/2,Height/2,.5,.5 )canvas.DrawText( "Width: " + Width + " Height: " + Height,Width/2,Height/2+40,.5,.5 )EndEndFunction Main()Local app:=New AppInstance#If __DESKTOP_TARGET__Local win:= New MyWindow("test",640,480)#Elseif __MOBILE_TARGET__Local win:= New MyWindow("test",3000,3000, WindowFlags.HighDPI | WindowFlags.Fullscreen | WindowFlags.Borderless | WindowFlags.Maximized | WindowFlags.CenterX | WindowFlags.CenterY)#endifApp.Run()EndFebruary 3, 2018 at 8:27 pm #13449Its shouldn’t matter what you set window size to, as it will always be maximised. Just check Width and Height after creating window/
February 3, 2018 at 8:41 pm #13450February 4, 2018 at 8:39 pm #13469Should I ditch the HighDPI flag? Should it just always be ‘on’? It would save a lot of headaches as DesktopSize could returned scaled/physical/retina size, and DPI/retina handling could all be done internally etc.
February 5, 2018 at 7:05 am #13472Yes, to me the HighDPI is just confusing, so it can go away.
If setting it to always on, makes App.DesktopSize return the correct pixel dimensions, that is even better.
Android already does high resolutions without a concept of “scaled down/half res” so it would also make things more consistent.
February 5, 2018 at 8:16 am #13473I run into a lot of performance issues with HighDPI on when maximizing or going full screen. If it goes away, it would be nice to have some sort of “Half-resolution” mode, or a fill mode that lets you specify the vertical resolution (and adapts the horizontal automatically).
We can always render to texture with whatever resolution you want, but it’s kinda bureaucratic for small projects / quick tests.
February 5, 2018 at 9:56 am #13475In addition to Ethernaut’s thoughts I can say that after searching the net for solutions to the problem above, I realize that HighDPI is a desktop setting too, targeting Windows and Mac desktops, so it might be important for some uses.
-
AuthorPosts
You must be logged in to reply to this topic.