About Monkey 2 › Forums › Monkey 2 Programming Help › Is there a concept of Virtual Resolution?
This topic contains 4 replies, has 4 voices, and was last updated by
cocon 2 years ago.
-
AuthorPosts
-
March 11, 2017 at 2:39 am #7461
Hey guys, just installed Monkey 2 and wow, it’s looking good. What I want to know though, is there a way to handle virtual resolutions? I want to have a screen be some physical resolution (Don’t care what, ideally full res of the computer’s display) but then set a virtual resolution that I develop the game in and scaling is taken care of.
Obviously this would require letter or pillar boxing, scaling and cropping or whatever.
Does Monkey 2 support this easily?
Cheers
Aaron
March 11, 2017 at 3:34 am #7462If you look inside Monkey2/bananas/viewLayout there’s a complete demonstration on how to use virtual resolution and layouts. The basic idea is that the Window method “OnMeasure:Vec2i()” should return the resolution you want, and the “Layout” property controls things like stretching, letterboxing, etc.
Here’s a much simplified version that shows only what you want, scale the window to see the letter boxing in action:
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334[crayon-5cba9ba3ae768132324670 inline="true" ]#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowField virtualRes:=New Vec2i( 320,240 )Method New( title:String,width:Int,height:Int,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )Layout = "letterbox"Style.BackgroundColor = Color.DarkGreyClearColor = Color.BlackEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawText( "Hello World!",Width/2,Height/2,.5,.5 )EndMethod OnMeasure:Vec2i() OverrideReturn virtualResEndEndFunction Main()New AppInstanceNew MyWindow( "Simple Window Demo!",640,512 )App.Run()EndMarch 11, 2017 at 4:17 am #7463Thanks, that seems to do the resolution thing.
Does Monkey 2 support the ability to provide multiple resolution versions of images that are loaded based on the physical to virtual ratio?
Thanks
March 12, 2017 at 10:16 pm #7477Not yet, you still have to do this manually.
I want to support it though.
April 1, 2017 at 7:01 am #7672I had created a few weeks a test on this, however this is very unpolished and not tested. In the future once I come up with better ideas and a nice API for this I will post the results. For example autofit, change from portrait to landscape, dynamically, zooming, etc…
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class VirtualWindowPrivateField xCenter:Int, yCenter:IntField xOrigin:Int, yOrigin:IntField originalWidth:Int, originalHeight:IntField virtualWidth:Int, virtualHeight:IntPublicProperty Width:Int()Return virtualWidthEndProperty Height:Int()Return virtualHeightEndMethod New(originalWidth:Int, originalHeight:Int, virtualWidth:Int, virtualHeight:Int)Self.originalWidth = originalWidthSelf.originalHeight = originalHeightSelf.virtualWidth = virtualWidthSelf.virtualHeight = virtualHeightxCenter = originalWidth / 2yCenter = originalHeight / 2xOrigin = xCenter - (virtualWidth / 2)yOrigin = yCenter - (virtualHeight / 2)EndMethod DrawFunction(canvas:Canvas, func:Void(canvas:Canvas))' canvas.Translate(-xOrigin, -yOrigin)canvas.PushMatrix()canvas.Translate(xOrigin, yOrigin)canvas.Color = New Color(0.3, 0.3, 0.3)canvas.DrawRect(0, 0, virtualWidth, virtualHeight)func(canvas)canvas.PopMatrix()EndMethod CoordsToVirtual:Vec2i(realX:Int, realY:Int)Return New Vec2i(realX - xOrigin, realY - yOrigin)EndMethod CoordsToReal:Vec2i(virtX:Int, virtY:Int)Return New Vec2i(virtX + xOrigin, virtY + yOrigin)EndEndClass MyApp Extends WindowField viwin:VirtualWindowMethod New(title:String, width:Int, height:Int)Super.New(title, width, height)viwin = New VirtualWindow(width, height, 480, 720)EndMethod OnRender(canvas:Canvas) Override' Get Some CoordinatesLocal coords := viwin.CoordsToVirtual(Mouse.X, Mouse.Y)' := Mouse.X, ypos := Mouse.YIf coords.X < 0 Then coords.X = 0If coords.X > viwin.Width Then coords.X = viwin.WidthIf coords.Y < 0 Then coords.Y = 0If coords.Y > viwin.Height Then coords.Y = viwin.HeightApp.RequestRender()' Draw Things Inside The Virtual Spaceviwin.DrawFunction(canvas, Lambda(c:Canvas)canvas.Color = Color.Whitecanvas.DrawText("Real Coords: " + viwin.CoordsToReal(coords.X, coords.Y), 0, 0)canvas.DrawText("Virt Coords: " + coords, 0, 20 )canvas.Color = New Color(0.8, 0.1, 0.2)canvas.DrawCircle(coords.X, coords.Y, 10)End)canvas.Flush()EndEndFunction Main()New AppInstanceNew MyApp("Monkey2 Virtual Window", 1280, 720)App.Run()EndMy inspiration comes mostly from the 3D rendering and not from exactly from “virtual drawing”. In essence this might go for something like a “camera” approach. The essence is to consider that your game takes place in a “virtual space” and not on actual screen pixels (either real pixels or virtual pixels). For example lets say that you want to render the environment for basketball game, you might put instead of a pixel resolution the actual dimensions of the arena. This is a clever way to abstract your thinking from screen pixels.
-
AuthorPosts
You must be logged in to reply to this topic.