Monkey
Store
Community
Apps
Contact
Login or Signup

AutoFit (update)

Monkey General Forums/Monkey Code/AutoFit (update)

DruggedBunny(Posted 1+ years ago) #1
Here's an update to my AutoFit example from the Monkey distribution, which scales a fixed virtual display size to the current device size, adding black borders where needed, taking aspect ratio into account.

http://www.hi-toro.com/monkey/autofit/autofit.zip

The old version recalculated pretty much everything on every update; now it just does a few checks and recalculates only when the device or zoom level has changed, something I've meant to sort out for ages. This is the new typical codepath (after the first call):

Method UpdateVirtualDisplay (zoomborders:Bool, keepborders:Bool)

	If (DeviceWidth <> lastdevicewidth)...			' Early out here and nothing to do...
	If device_changed...					' Nothing to do here...
	If vzoom <> lastvzoom...				' Nothing to do here...
	If zoom_changed Or device_changed...			' Nothing to do here...

	SetScissor 0, 0, DeviceWidth, DeviceHeight		' Set outer border...
	Cls 0, 0, 0			'			' Clear to black...

	SetScissor sx, sy, sw, sh				' Set inner area...
	Scale multi * vzoom, multi * vzoom			' Set scale to fit...

	If vzoom Then Translate vxoff, vyoff			' Always called unless zoom is zero...
	
End


It's been tested on HTML5, Flash, GLFW, XNA (Windows) and Android (SDK emulator).

Finally, here's a realtime capture of the new code applied to an existing game (using AutoFit) with no changes required, in the slow Android emulator:

http://www.youtube.com/watch?v=I4XQhsvnLfw

(Device rotates at 00:05, change detected by device a second later, then display updates to adapt. Changes back at 00:13.)


c.k.(Posted 1+ years ago) #2
Well done, DB! :)


DruggedBunny(Posted 1+ years ago) #3
Here's a stripped-down demo that shows how you can treat a virtual resolution as if it's a fixed display size and simply hard-code your positions for that display size if you want to:




GfK(Posted 1+ years ago) #4
Cool. im using the older version for crime solitaire so ill update tomorrow.


DruggedBunny(Posted 1+ years ago) #5
Excellent -- it should just drop in with no side effects, hopefully.


Difference(Posted 1+ years ago) #6
Marvelous, - dropped it right in and it works great!


GfK(Posted 1+ years ago) #7
Just tried this in 1.45b - I'm assuming it'll work but there are shedloads of errors when compiled in Strict mode.


slenkar(Posted 1+ years ago) #8
it has virtual mouse coords too, thanks!


DruggedBunny(Posted 1+ years ago) #9
@Gfk, sorry, I'll sort that... I'm really surprised I didn't use SuperStrict, but you're right, I didn't!

@Slenkar: yeah, I'll probably add the Touch* stuff as well.


DruggedBunny(Posted 1+ years ago) #10
OK, I've updated AutoFit so that the module is now Strict, and added VTouchX/Y calls too, plus the simpler demo above:

http://www.hi-toro.com/monkey/autofit/autofit.zip

Don't know why I was rambling on about SuperStrict, since Monkey doesn't have it -- mustn't post while hung over. However, I've realised that I don't use Strict in Monkey because of the forced returns values/function types, etc -- non-Strict catches undeclared variables anyway, which is what I mainly want.


DarkyCrystal(Posted 1+ years ago) #11
Good job. So if I understand it's better to work with high resolution to avoid black borders ? On a phone graph could be more small.


DruggedBunny(Posted 1+ years ago) #12
No, the black borders only appear if your 'virtual' resolution's aspect ratio doesn't match that of the device's real display.

For example, a 320 x 240 display, a 640 x 480 display and an 800 x 600 display all share a 4:3 aspect ratio (width:height). If you set your virtual resolution to 640 x 480, and the game runs on a device with 320, 640 or 800 resolutions, there will be no borders.

If you run that game on a device with a 16:9 display (eg. 1920 x 1080), because the aspect ratios don't match, you use black borders to show the game at its 4:3 aspect ratio within the 16:9 display.

In short, you choose what aspect ratio your game is going to use via your choice of virtual resolution, and the game will automatically adapt to the aspect ratio of any display it finds itself running on, using the full width or height of the display available while maintaining your chosen aspect ratio.

Hope that makes sense!


siread(Posted 1+ years ago) #13
Is it possible to lock the screen orientation? I only ever want my game to display in portrait view, and not switch to landscape (with borders) when the device is tilted (although I don't mind it flipping if the device is turned 180 degrees).


GfK(Posted 1+ years ago) #14
Is it possible to lock the screen orientation? I only ever want my game to display in portrait view, and not switch to landscape (with borders) when the device is tilted (although I don't mind it flipping if the device is turned 180 degrees).
That's target-specific. For iOS, you'll need to open up XCode, where you'll have the option to lock screen orientation, set icons, choose ARM6/7 architecture and so on. Oh, and don't forget to switch off Debug build, which is ON by default.

I know you can do the same stuff in Android but having never used it, I don't know how.


therevills(Posted 1+ years ago) #15
I know you can do the same stuff in Android but having never used it, I don't know how.


Just set the option in the AndroidManifest... with Monkey you can just alter the config text file :)

'Must be one of: user, portrait, langscape, behind, sensor, nosensor, unspecified - experiment!
SCREEN_ORIENTATION=landscape



siread(Posted 1+ years ago) #16
Cheers. :)


NoOdle(Posted 1+ years ago) #17
DruggedBunny you rock!

This is perfect, drops straight in to my framework. Works perfectly on IOS as well, you can rotate the device from the menu like on Android and it resizes to fit. Saved me a huge headache and quite a few hours of hell.

I owe you some drugs sir! ;)


slenkar(Posted 1+ years ago) #18
on android you have to set the screen orientation to 'user'
to make the game rotate when turn the android device?


Volker(Posted 1+ years ago) #19
Try 'sensor'.


siread(Posted 1+ years ago) #20
I added a method to the VirtualDisplay class that allows you to use scissors on your scaled display. Took me bloody ages to get my head around it but it seems to work. :)

	Method SetVScissor:Int (vsx:Float, vsy:Float, vsw:Float, vsh:Float)
		' Allows you to set up a scissor on a scaled display
		' Call UnsetVScissor when you want to continue drawing outside of the scissor area
		
		Local sclx:Float = scaledw / vwidth
		Local scly:Float = scaledh / vheight
		
		Local vscissor:Float[4]
		
		vscissor[0] = Max(sx, sx + (vsx * sclx))
		vscissor[1] = Max(sy, sy + (vsy * scly))
		vscissor[2] = vsw * sclx
		vscissor[3] = vsh * scly
		
		If vscissor[0] + vscissor[2] > scaledw Then vscissor[2] = (sx + scaledw) - vscissor[0]
		If vscissor[1] + vscissor[3] > scaledh Then vscissor[3] = (sy + scaledh) - vscissor[1]
		
		SetScissor(vscissor[0], vscissor[1], vscissor[2], vscissor[3])
		
		Return 0
	End
	
	Method UnsetVScissor:Int ()
		SetScissor sx, sy, sw, sh
		
		Return 0
	End



Foppy(Posted 1+ years ago) #21
Thanks for this wonderful module.

I had uploaded my Doggy Bounce game to Google Play, only to notice that on a friend's phone it went outside of the screen. I had completely forgotten about different resolutions, thinking that it would somehow be scaled automatically.

With Autofit I just had to add two lines of code to the program, and change one call to TouchX() to VTouchX(), and now it does work perfectly, at least on mine and that other phone! :D


DruggedBunny(Posted 1+ years ago) #22
Cool... I feel like Doc Brown hearing that others are making use of this in the real-world: "It works! I finally invented something that works!"


fsoft(Posted 1+ years ago) #23
DruggedBunny ... it works great, actually ;-)


Chroma(Posted 1 month ago) #24
Cool, thanks for this. Getting closer and closer to my first mobile game release!


bram(Posted 1 month ago) #25
Nice work, saves a lot of headaches. Thank you for sharing this.


Supertino(Posted 1 week ago) #26
Well I was coming here to enquirer about using scissors on a scaled display and it seems that siread has come up with a solution. yippee.

As always a big THANKYOU to James Boyd for autofit and a hearty handshake to sired, though I am sure sired is too busy mixing with his new high end dev friends and driving around in one of his many sports cars to care :p


DruggedBunny(Posted 1 week ago) #27
I was actually going to update it with Si's scissor stuff a while ago... will do soon-ish!