- Joined
- Jan 2, 2020
- Messages
- 1,414
I've been looking a long time for a efficient and easy way to create a virtual resolution on all devices including mobiles and desktops.
There's autofit, it's for mojo1 only but I was thinking of converting it only to discover it might not be a good idea, also it's blurry (which could be a postive thing sometimes but it was too blurry for my taste).
Next up was letterbox for mojo2, I don't know why but it just wasn't reliable on my Mac and phone, strangely offset and getting stuck in all kinds of resolutions. It might be me. But I tried for a some weeks.
The last resort would be to go for a framework, there are a few. This is not something I ever do so what I would do would be to dive into the frameworks just to see how they did it. I didn't have the energy to do that.
So I made this. Maybe someone else likes it too so let me share it.
What it does :
You tell what resolution you have and what you would like to have instead and it will
find the biggest integer scale that you can fit and centre it nicely on the screen, and it does this with an optional background!
EDIT if you wonder weather it's pixel-perfect if you happen to use your exact native screen as your virtual screen,
the answer is actually yes. It does not make a single misstake, so it's a nice base to have.
There's autofit, it's for mojo1 only but I was thinking of converting it only to discover it might not be a good idea, also it's blurry (which could be a postive thing sometimes but it was too blurry for my taste).
Next up was letterbox for mojo2, I don't know why but it just wasn't reliable on my Mac and phone, strangely offset and getting stuck in all kinds of resolutions. It might be me. But I tried for a some weeks.
The last resort would be to go for a framework, there are a few. This is not something I ever do so what I would do would be to dive into the frameworks just to see how they did it. I didn't have the energy to do that.
So I made this. Maybe someone else likes it too so let me share it.
What it does :
You tell what resolution you have and what you would like to have instead and it will
find the biggest integer scale that you can fit and centre it nicely on the screen, and it does this with an optional background!
EDIT if you wonder weather it's pixel-perfect if you happen to use your exact native screen as your virtual screen,
the answer is actually yes. It does not make a single misstake, so it's a nice base to have.
Cerberus:
Import mojo2
Function Main ()
New Game
End
Class Game Extends App
Field screen2:Image
Field screen2canvas:Canvas
Field intwidth:Int, intheight:Int
Field startx:Int, starty:Int
Field canvas:Canvas
Field size:Int=8
Field cx:Int
Field wx:Int
Field wy:Int
Field tilemap:Int[512*512]
Field s:Int = 2
Field background:Image
' --------------------------------
' Virtual resolution
Field vwidth:Int = 320 ' 200'320
Field vheight:Int = 200 ' 320'200
' Native resolution or windowsize
Field awidth:Int = 1440 ' 720'1440
Field aheight:Int = 900 ' 1280'900
' ---------------------------------
Method OnCreate ()
For Local y:=0 To 511
For Local x:=0 To 511
tilemap[x + y * 512] = Int(Rnd(127))
Next
Next
background=Image.Load("80s.png",0,0,0)
Image.SetFlagsMask(Image.Managed)
SetDeviceWindow awidth,aheight,1 ' 4
canvas=New Canvas
screen2=New Image(vwidth,vheight,0,0,0)
screen2.SetFlagsMask(screen2.Managed)
screen2canvas=New Canvas(screen2)
SetSwapInterval 1
SetUpdateRate 0
HideMouse
Return 0
End
Method OnUpdate ()
If KeyHit(KEY_ESCAPE) Then EndApp
wx=wx-s*KeyDown(KEY_LEFT)+s*KeyDown(KEY_RIGHT)
wy=wy-s*KeyDown(KEY_UP)+s*KeyDown(KEY_DOWN)
wx=Max(0,Min(10000,wx))
wy=Max(0,Min(10000,wy))
End
Method OnRender ()
canvas.Clear 0,0,0
' Calculate how to put the biggest integer version of our wanted virtual resolution on the screen
intwidth = Floor(Float(awidth)/Float(vwidth))
intheight = Floor(Float(aheight)/Float(vheight))
If intwidth > intheight Then intwidth = intheight
If intheight > intwidth Then intheight = intwidth
startx = (awidth-(vwidth*intwidth)) / 2
starty = (aheight-(vheight*intheight)) / 2
' Done thinking, let's make it happen! Let's draw on our virtual screen, below follows an example
screen2canvas.Clear 0,0,0
screen2canvas.SetColor 1,1,1
Local w=320
Local h=200
wx=wx+1 Mod 4096
Local scrx:=wx Mod size
Local scry:=wy Mod size
Local mapx:=wx / size
Local mapy:=wy / size
Local cnty:= -scry
For Local y:=mapy To mapy+((h/size)+1)
Local cntx:=-scrx
For Local x:=mapx To mapx+((w/size)+1)
screen2canvas.DrawOval cntx,cnty,size,size
cntx=cntx+size
Next
cnty=cnty+size
Next
screen2canvas.Flush
' Now draw everything onto the native canvas
canvas.SetColor 1,1,1
canvas.DrawRect 0,0,awidth,aheight,background,0,0,890,1213 ' hardcoded width and height of example image
canvas.DrawImage screen2,startx,starty,0,intwidth,intheight
canvas.Flush
End
End
Attachments
Last edited: