- Joined
- Jun 19, 2017
- Messages
- 320
Hi all,
I've changed the original letterbox example a bit in a way that it will always maintain a typical desktop size.
It will have a virtual resolution set, e.g. 1920x1080. If the real device resolution is wider you will have black bars left and right, if the width gets smaller if will provide an offset to the x coordinate.
Well bit hard to describe, it will just always keep the aspect ratio and scales if possible. That way you can have any aspect from 4:3 up to whatever your set with your virtual one and will always have an non-stretched view. Plus you get the virtual mouse coordinates.
I've changed the original letterbox example a bit in a way that it will always maintain a typical desktop size.
It will have a virtual resolution set, e.g. 1920x1080. If the real device resolution is wider you will have black bars left and right, if the width gets smaller if will provide an offset to the x coordinate.
Well bit hard to describe, it will just always keep the aspect ratio and scales if possible. That way you can have any aspect from 4:3 up to whatever your set with your virtual one and will always have an non-stretched view. Plus you get the virtual mouse coordinates.
Code:
Strict
Import mojo2
' set the parameters for a GLFW window
#GLFW_WINDOW_WIDTH = 800
#GLFW_WINDOW_HEIGHT = 400
#GLFW_WINDOW_RESIZABLE = True
#GLFW_WINDOW_RENDER_WHILE_RESIZING = True
' the size and aspect ratio of our virtual window
Const VWIDTH:Int = 1024
Const VHEIGHT:Int = 576
Global gScreenXOffset:Int ' will have the offset of the x start on screen when aspect is less than VWIDTH/VHEIGHT
Global gVirtualMouseX:Int ' calculated virtual mouse coordinate, always 0 ... VWIDTH
Global gVirtualMouseY:Int ' calculated virtual mouse coordinate, always 0 ... VHEIGHT
Class MyApp Extends App
Field myCanvas:Canvas
Method OnCreate:Int()
myCanvas = New Canvas()
Return 0
End
Method OnUpdate:Int()
Return 0
End
Method calcLetterboxAndVirtualMouse:Void(vprect:Int[])
Local devWidth:Float = Float(DeviceWidth())
Local devHeight:Float = Float(DeviceHeight())
Local virtualScale:Float = devHeight / VHEIGHT
vprect[0] = 0.5 * (devWidth - virtualScale * VWIDTH)
vprect[1] = 0.5 * (devHeight - virtualScale * VHEIGHT)
vprect[2] = VWIDTH * virtualScale
vprect[3] = VHEIGHT * virtualScale
gScreenXOffset = -Int( (devWidth - VWIDTH * virtualScale) * 0.5 / virtualScale)
if (gScreenXOffset < 0 ) Then gScreenXOffset = 0
gVirtualMouseX = (MouseX() - (devWidth - VWIDTH * virtualScale) * 0.5) / virtualScale
gVirtualMouseY = (MouseY() - (devHeight - VHEIGHT * virtualScale) * 0.5) / virtualScale
If (gVirtualMouseX < 0) Then gVirtualMouseX = 0
If (gVirtualMouseX > VWIDTH) Then gVirtualMouseX = VWIDTH
If (gVirtualMouseY < 0) Then gVirtualMouseY = 0
If (gVirtualMouseY > VHEIGHT) Then gVirtualMouseY = VHEIGHT
End
Method RenderScene:Void()
Local vprect:Int[4]
calcLetterboxAndVirtualMouse(vprect)
myCanvas.SetViewport(vprect[0], vprect[1], vprect[2], vprect[3])
myCanvas.SetProjection2d(0, VWIDTH, 0, VHEIGHT)
myCanvas.Clear(0, 0, 1)
myCanvas.DrawText("Virtual Mouse: " + gVirtualMouseX + ", " + gVirtualMouseY, 10 + gScreenXOffset, 10, 0, 0)
End
Method OnRender:Int()
' set viewport to fill the window
myCanvas.SetViewport(0, 0, DeviceWidth(), DeviceHeight())
myCanvas.Clear(0, 0, 0)
RenderScene()
myCanvas.Flush()
Return 0
End
End
Function Main:Int()
New MyApp
Return 0
End