Universal AGK Display Setup Code for Beginners (T1) by Marl24th 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; 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 |