TGC Codebase Backup



Universal AGK Display Setup Code for Beginners (T1) by Marl

24th Nov 2011 11:42
Summary

Basic display setup function which handles resolution, orientation and zoom.



Description

Starting out with AGK, one of the trickiest parts for me was getting to grips with the screen sizes, orientations, zooms and whatnot;

- The device screen width and height returned by my phone do not change with orientation
- Windows returns orientation 1, even in landscape mode
- Object scaling seemed very arbitrary.

I needed some way to get on top of all this.

After a bit of trial and error I came up with a function which just takes care of it, allowing me to get on with the actual app. This has now been split into two functions, one to set everything up, the other to check orientation and zoom and update as necessary.

I provide this here for the community in the hope it will help others avoid some of the pitfalls I encountered.

Everything is stored in one custom type variable "myDisplay"

Simply provide the "short-edge" resolution and frame rate required and the function sets up the display to use the full screen irrespective of orientation. There should be no black bars and no wasted space.

Includes an optional function to display variable contents.

Tested on Samsung Galaxy SII
Testing on other devices welcomed and appreciated.

--- Update 1.1 ---

Added .vWidthVis & .vHeightVis to custom type to store Visible Display Area
Added test routine to demonstrate zoom effect - used Virtual Joystick / sprite



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    type displayType width height aspect# orient vMin vMax vWidth vHeight vWidthVis vHeightVis isLandscape textSize zoom# maxZoom# minZoom# fps endtype
global myDisplay as displayType

preferredFPS = 30
//	Typical Frame Rate for Android Phones
preferredMinRes = 320
//	Android baseline resolution ( T-Mobile G1 @ 320 x 480 )

initDisplay ( preferredMinRes , preferredFPS )

// *** For demonstration of Zoom, can be removed ***
// Test Routine - Initialisation
addVirtualJoystick ( 1 , myDisplay.vWidth - 64 , myDisplay.vHeight - 64 , 64 )
// Sprite to Show Change to Screen Size
mySprite = CreateSprite ( 0 )
setSpriteSize ( mySprite , 50 , 50 )
setSpritePosition ( mySprite , 50 , 50 )
setSpriteColor ( mySprite , 255 , 200 , 160 , 255 )
// End of Test Routine
// *************************************************

do

	// *** For demonstration of Zoom, can be removed ***
	// Test Routine - Loop Portion
	if getVirtualJoystickY ( 1 ) <> 0
		myDisplay.zoom# = myDisplay.zoom# - getVirtualJoystickY ( 1 ) / 8.0
		setViewZoom ( myDisplay.zoom# )
	endif
	// End of Test Routine
	// *************************************************

	if GetRawKeyState ( 27 ) = 1 then exit
	checkOrientation( 0 )

	// App Code Goes Here

	// Optional Function to Show Variable Contents
	showMyDisplayData ( )
	sync ( )
loop

function showMyDisplayData()
	print ( "myDisplay")
	print ( " .width...... : " + str ( myDisplay.width ) )
	print ( " .height..... : " + str ( myDisplay.height ) )
	print ( " .aspect#.... : " + str ( myDisplay.aspect# ) )
	print ( " .orient..... : " + str ( myDisplay.orient ) )
	print ( " .vMin....... : " + str ( myDisplay.vMin ) )
	print ( " .vMax....... : " + str ( myDisplay.vMax ) )
	print ( " .vWidth..... : " + str ( myDisplay.vWidth ) )
	print ( " .vHeight.... : " + str ( myDisplay.vHeight ) )
	print ( " .isLandscape : " + str ( myDisplay.isLandscape ) )
	print ( " .textSize... : " + str ( myDisplay.textSize ) )
	print ( " .zoom#...... : " + str ( myDisplay.zoom# ) )
	print ( " .maxZoom#... : " + str ( myDisplay.maxZoom# ) )
	print ( " .minZoom#... : " + str ( myDisplay.minZoom# ) )
	print ( " .vWidthVis.. : " + str ( myDisplay.vWidthVis ) )
	print ( " .vHeightVis. : " + str ( myDisplay.vHeightVis ) )
	print ( " .fps........ : " + str ( myDisplay.fps ) )
	print ( " " )
	print ( "Screen fps... : " + str ( screenfps ( ) ) )
endfunction

function initDisplay ( thisVMin , thisFPS )
	myDisplay.fps = thisFPS
	myDisplay.vMin = thisVMin
	myDisplay.maxZoom# = 3.0
	myDisplay.minZoom# = 0.3
	myDisplay.width = getDeviceWidth ( )
	myDisplay.height = getDeviceHeight ( )
	myDisplay.aspect# = getDisplayAspect ( )
	if myDisplay.width > myDisplay.height
		myDisplay.isLandscape = 1
		myDisplay.vMax = myDisplay.vMin * myDisplay.aspect#
	else
		myDisplay.isLandscape = 0
		myDisplay.vMax = myDisplay.vMin / myDisplay.aspect#
	endif
	myDisplay.textSize = myDisplay.vMin / 20
	setPrintColor ( 255 , 255 , 255 , 200 )
	SetSyncRate ( myDisplay.fps , 0 )
	SetOrientationAllowed ( 1 , 1 , 1 , 1 )
	checkOrientation( 1 )
endfunction

function checkOrientation( thisForce )
	myDisplay.orient = getOrientation ( )
	// Fiddle to get around windows always returning orientation as 1 - even in landscape
	if GetDeviceName( ) = "windows"
		thisLandscape = myDisplay.isLandscape
	else
		thisLandscape = ( myDisplay.orient -1 ) / 2
	endif
	if thisLandscape <> myDisplay.isLandscape or thisForce
		myDisplay.isLandscape = thisLandscape
		if myDisplay.isLandscape
			myDisplay.VHeight = myDisplay.vMin
			myDisplay.VWidth = myDisplay.vMax
		else
			myDisplay.VHeight = myDisplay.vMax
			myDisplay.VWidth = myDisplay.vMin
		endif
		setVirtualResolution ( myDisplay.VWidth , myDisplay.VHeight )
		setPrintSize ( myDisplay.textSize )
	endif
	myDisplay.zoom# = GetViewZoom ( )
	if myDisplay.zoom# < myDisplay.minZoom#
		myDisplay.zoom# = myDisplay.minZoom#
		setViewZoom( myDisplay.zoom# )
	endif
	if myDisplay.zoom# > myDisplay.maxZoom#
		myDisplay.zoom# = myDisplay.maxZoom#
		setViewZoom( myDisplay.zoom# )
	endif
	myDisplay.vWidthVis = myDisplay.vWidth / myDisplay.zoom#
	myDisplay.vHeightVis = myDisplay.vHeight / myDisplay.zoom#
endfunction

remstart
displayType
	width		width of device screen on startup
	height		height of device screen on startup
	aspect#		aspect of device screen on startup
	orient		current orientation - from getOrientation ( ) each frame
	vMin			Virtual Resolution short side - irrespective of orientation
	vMax			Virtual Resolution long side - irrespective of orientation
	vWidth		Virtual Resolution width - based on orientation
	vHeight		Virtual Resolution height - based on orientation
	isLandscape	true if device is currently in landscape
	textSize		text size - initially set to 1/20th of vMin
	zoom#		display zoom - from GetViewZoom ( ) each frame
	vWidthVis		Virtual resolution width visible allowing for Zoom
	vHeightVis	Virtual resolution height visible allowing for Zoom
	maxZoom#		maximum zoom allowed
	minZoom#		minimum zoom allowed
	fps			preferred fps
remend