November 21, 2017, 03:55:07 PM

Author Topic: [bmx] Aspect correction for SetVirtualResolution() by Oddball [ 1+ years ago ]  (Read 75 times)

Offline BlitzBot

  • Newbie
  • *
  • Posts: 0
Title : Aspect correction for SetVirtualResolution()
Author : Oddball
Posted : 1+ years ago

Description : The new and changed functions are SetVirtualResolution, SetBorderColor and SwitchAspectMode. The other functions only mimic their original, but allow for the aspect changes.

The modes are:
ASPECT_KEEP - Keeps previously selected mode.
ASPECT_STRETCH - This is the classic style.
ASPECT_LETTERBOX_FILL - This mode scales the resolution to fit the screen. Any extra area is not clipped.
ASPECT_LETTERBOX_BORDER - This mode scales the resolution to fit the screen. Any extra area is cleared using the border color.
ASPECT_BESTFIT_FILL - This scales the resolution by integer values only. Any extra space is not clipped.
ASPECT_BESTFIT_BORDER - This scales the resolution by integer values only. Any extra space is cleared using the border color.

There is now a module version for those who prefer that sort of thing:
<a href="http://www.gooeyblob.com/temp/aspectmodule.zip" target="_blank">http://www.gooeyblob.com/temp/aspectmodule.zip[/url]

Enjoy!


Code :
Code: BlitzMax
  1. 'Code: David 'Oddball' Williamson
  2. '6th May 2010
  3. 'Enjoy!
  4.  
  5. SuperStrict
  6.  
  7. Const ASPECT_KEEP:Int=1
  8. Const ASPECT_STRETCH:Int=0
  9. Const ASPECT_LETTERBOX_FILL:Int=2
  10. Const ASPECT_LETTERBOX_BORDER:Int=3
  11. Const ASPECT_BESTFIT_FILL:Int=4
  12. Const ASPECT_BESTFIT_BORDER:Int=5       'Bestfit border may not work with all drivers(i.e. DirectX)
  13.  
  14. Private
  15.  
  16. Global _xoffset:Float
  17. Global _yoffset:Float
  18. Global _vwidth:Float
  19. Global _vheight:Float
  20. Global _mode:Int=ASPECT_STRETCH
  21. Global _bred:Int=0
  22. Global _bgreen:Int=0
  23. Global _bblue:Int=0
  24.  
  25. Public
  26.  
  27. Function SetVirtualResolution( width:Float, height:Float, mode:Int=ASPECT_KEEP )
  28.         If mode<>ASPECT_KEEP Then _mode=mode
  29.         If _mode=ASPECT_STRETCH
  30.                 _xoffset=0;_yoffset=0;_vwidth=width;_vheight=height;_mode=mode
  31.                 brl.max2d.SetVirtualResolution(_vwidth,_vheight)
  32.                 brl.max2d.SetViewport _xoffset,_yoffset,Ceil(_vwidth),Ceil(_vheight)
  33.                 brl.max2d.SetOrigin _xoffset,_yoffset
  34.                 Return
  35.         EndIf
  36.         Local gwidth:Int=GraphicsWidth()
  37.         Local gheight:Int=GraphicsHeight()
  38.         _vwidth=width
  39.         _vheight=height
  40.         Local wratio:Float=gwidth/_vwidth
  41.         Local hratio:Float=gheight/_vheight
  42.         If wratio<hratio Then hratio=wratio
  43.         If _mode&4
  44.                 hratio=Floor(hratio)
  45.                 hratio=Max(hratio,1)
  46.         EndIf
  47.         height=gheight/hratio
  48.         width=gwidth/hratio
  49.        
  50.         _xoffset=(width-_vwidth)*.5
  51.         _yoffset=(height-_vheight)*.5
  52.         If _mode&4
  53.                 _xoffset=Floor(_xoffset)
  54.                 _yoffset=Floor(_yoffset)
  55.         EndIf
  56.         brl.max2d.SetVirtualResolution(width,height)
  57.        
  58.         If _mode&1
  59.                 Local red:Int,green:Int,blue:Int
  60.                 GetClsColor red,green,blue
  61.                 SetClsColor _bred,_bgreen,_bblue
  62.                 Cls
  63.                 SetClsColor red,green,blue
  64.                 brl.max2d.SetViewport _xoffset,_yoffset,_vwidth,_vheight
  65.         Else
  66.                 brl.max2d.SetViewport 0,0,Ceil(brl.max2d.VirtualResolutionWidth()),Ceil(brl.max2d.VirtualResolutionHeight())
  67.         EndIf
  68.        
  69.         brl.max2d.SetOrigin _xoffset,_yoffset
  70.        
  71. End Function
  72.  
  73. Function SetBorderColor( red:Int, green:Int, blue:Int )
  74.         _bred=red
  75.         _bgreen=green
  76.         _bblue=blue
  77. End Function
  78.  
  79. Function SwitchAspectMode( mode:Int )
  80.         SetVirtualResolution _vwidth,_vheight,mode
  81. End Function
  82.  
  83. Function Flip( sync:Int=-1 )
  84.         brl.Graphics.Flip sync
  85.         If _mode&1
  86.                 brl.max2d.SetViewport 0,0,Ceil(brl.max2d.VirtualResolutionWidth())+1,Ceil(brl.max2d.VirtualResolutionHeight())+1
  87.                 Local red:Int,green:Int,blue:Int
  88.                 GetClsColor red,green,blue
  89.                 SetClsColor _bred,_bgreen,_bblue
  90.                 Cls
  91.                 SetClsColor red,green,blue
  92.                 brl.max2d.SetViewport _xoffset,_yoffset,_vwidth,_vheight
  93.         EndIf
  94. End Function
  95.  
  96. Function SetOrigin( x:Float, y:Float )
  97.         brl.max2d.SetOrigin x+_xoffset,y+_yoffset
  98. End Function
  99.  
  100. Function GetOrigin( x:Float Var, y:Float Var )
  101.         brl.max2d.GetOrigin x,y
  102.         x:-_xoffset
  103.         y:-_yoffset
  104. End Function
  105.  
  106. Function SetViewport( x:Int, y:Int, width:Int, height:Int )
  107.         Local x2:Float=Min(x+width,_xoffset+_vwidth)
  108.         Local y2:Float=Min(y+height,_yoffset+_vheight)
  109.         x=Max(x,_xoffset)
  110.         y=Max(y,_yoffset)
  111.         brl.max2d.SetViewport x,y,x2-x,y2-y
  112. End Function
  113.  
  114. Function GetViewport( x:Int Var,y:Int Var,width:Int Var,height:Int Var )
  115.         brl.max2d.GetViewport x,y,width,height
  116.         x:-_xoffset
  117.         y:-_yoffset
  118. End Function
  119.  
  120. Function VirtualMouseX:Float()
  121.         Return brl.max2d.VirtualMouseX()-_xoffset
  122. End Function
  123.  
  124. Function VirtualMouseY:Float()
  125.         Return brl.max2d.VirtualMouseY()-_yoffset
  126. End Function
  127.  
  128. Function MoveVirtualtMouse( x:Float, y:Float )
  129.         brl.max2d.MoveVirtualMouse x+_xoffset,y+_yoffset
  130. End Function
  131.  
  132. Function VirtualResolutionWidth:Float()
  133.         Return _vwidth
  134. End Function
  135.  
  136. Function VirtualResolutionHeight:Float()
  137.         Return _vheight
  138. End Function
  139.  
  140. '-----------------------
  141. 'Test code
  142. '-----------------------
  143. SeedRnd MilliSecs()
  144. Graphics Rand(320,DesktopWidth()),Rand(240,DesktopHeight())
  145.  
  146. SetBorderColor 32,32,32
  147. SetClsColor 223,223,223
  148.  
  149. SetVirtualResolution 320,240,ASPECT_LETTERBOX_BORDER
  150.  
  151. Repeat
  152.         If KeyHit(KEY_1) Then SwitchAspectMode ASPECT_STRETCH
  153.         If KeyHit(KEY_2) Then SwitchAspectMode ASPECT_LETTERBOX_FILL
  154.         If KeyHit(KEY_3) Then SwitchAspectMode ASPECT_LETTERBOX_BORDER
  155.         If KeyHit(KEY_4) Then SwitchAspectMode ASPECT_BESTFIT_FILL
  156.         If KeyHit(KEY_5) Then SwitchAspectMode ASPECT_BESTFIT_BORDER
  157.         If KeyHit(KEY_SPACE)
  158.                 Graphics Rand(320,DesktopWidth()),Rand(240,DesktopHeight())
  159.                 SetVirtualResolution 320,240
  160.                 SetClsColor 223,223,223
  161.                 SetColor 255,127,0
  162.         EndIf
  163.         Cls
  164.         SetColor 0,0,255
  165.         DrawText Int(VirtualMouseX())+","+Int(VirtualMouseY()),VirtualMouseX(),VirtualMouseY()
  166.         SetColor 255,0,0
  167.         DrawText "Aspect correction demo",5,5
  168.         SetColor 255,127,0
  169.         DrawText "[1] Stretch",5,35
  170.         DrawText "[2] Letterbox(no border)",5,50
  171.         DrawText "[3] Letterbox",5,65
  172.         DrawText "[4] Best fit(no border)",5,80
  173.         DrawText "[5] Best fit - May not work with DirectX Drivers",5,95
  174.         DrawText "[SPACE] Resize window(random size)",5,125
  175.         Flip
  176. Until KeyHit(KEY_ESCAPE)


Comments :


Oddball(Posted 1+ years ago)

 Made a couple of corrections. I could make this into a module if there's enough interest.


Oddball(Posted 1+ years ago)

 Added a keep mode and improved the demo code.


Oddball(Posted 1+ years ago)

 Added MoveVirtualMouse.


BlitzSupport(Posted 1+ years ago)

 Very nice. Still hopeful Mark will add aspect ratio correction at some point, but this will do very nicely indeed in the meantime! Thanks.


Oddball(Posted 1+ years ago)

 The module version is a little more complete, but a little less plug-and-play as the function names in the mod are changed to prevent clashes with Max2D.For those with sigs turned off you can get it at:<a href="http://www.gooeyblob.com/temp/aspectmodule.zip" target="_blank">http://www.gooeyblob.com/temp/aspectmodule.zip[/url]


Offline LamptonWorm

  • Jr. Member
  • **
  • Posts: 2
Just wanted to say thanks for this function, really useful and helped my project, cheers!

Offline Dabz

  • Jr. Member
  • **
  • Posts: 82
Off Topic... Mr Lampton Worm... Your not from County Durham by any chance are you?

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 8Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit