Useful functions

Monkey Forums/Monkey Code/Useful functions

EdzUp(Posted 2013) [#1]
Here is some code I found useful :)

'
'	EdzUpFunctions.monkey - Copyright ŠEdzUp
'	Pragrammed by Ed 'EdzUp' Upton
'
Strict

Import mojo.graphics

'for scaling system
Global DesignScaleX:Float = 0.0
Global DesignScaleY:Float = 0.0

Global PixelScaleX:Float = 0.0
Global PixelScaleY:Float = 0.0

'for platform specific checks
Global Platform:Int =0
Const Platform_Mobile:Int = 1
Const Platform_Internet:Int = -1
Const Platform_Desktop:Int = 0

Function CheckPlatform:Int()
	Platform =Platform_Desktop					'desktop (default but will be changed if its a different target)
	#If TARGET="android" Or TARGET="ios" Or TARGET ="winphone8"
		Platform = Platform_Mobile				'mobile
	#Else
		#If TARGET="html5" Or TARGET="flash" Or TARGET="java"
			Platform = Platform_Internet			'internet
		#Endif
	#Endif
	
	Return( Platform )
End

Function SetDesignScale:Int( X:Float, Y:Float )
	DesignScaleX = X
	DesignScaleY = Y
	
	'this should work out the scales of the pixels on screen for system movement etc
	PixelScaleX = DeviceWidth() /X
	PixelScaleY = DeviceHeight() /Y	
	
	Return( 0 )
End

Function ScaleX:Float()
	If DesignScaleX = 0 Then Error "No scaling set, use SetDesignScale( float, float ) in OnCreate"
	
	Return( DesignScaleX /DeviceWidth() )
End

Function ScaleY:Float()
	If DesignScaleY = 0 Then Error "No scaling set, use SetDesignScale( float, float ) in OnCreate"
	
	Return( DesignScaleY /DeviceHeight() )
End


To use the scaling system just call SetDesignScale( width, height ) where width and height are the size of what your original graphics were scaled at so if you design a game thats 1024 by 768 it would be SetDesignScale( 1024, 768 ). After that you can use PixelScaleX and PixelScaleY in your systems like this:
DrawImage( MyImage, X *PixelScaleX, Y *PixelScaleY, 0, 1.0 *PixelScaleX, 1.0 *PixelScaleY, frame )

This would then scale the position and size of the graphics on screen so no matter the resolution your game would still fit the screen.

With the code above I have also done a font module:
'
'	EdzUpFontSystem.monkey - Copyright ŠEdzUp
'	Programmed by Ed 'EdzUp' Upton
'
Strict

Import mojo
Import EdzUpFunctions

Class EdzUpFontClass
	'for font system
	Field FontString:String = " !`#$% '()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_"	
	Field FontImage:Image					'this is the animimage that will contain the font
	Field FontWidth:Int =0
	Field FontHeight:Int

	Method New()
		FontString = " !`#$% '()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_"
		FontImage = Null
		FontWidth = 0
		FontHeight =0
	End

	Method SetFont:Int( characterlist:String )
		'this will change the font for that particular system
		FontString = characterlist
	
		Return( 0 )
	End

	Method LoadFont:Int( filename:String, width:Int, height:Int, count:Int )
		'this loads in the image and then uses it to render the font required
		FontImage = LoadImage( filename, width, height, count )
		FontWidth = width
		FontHeight = height
	
		Return( 0 )
	End

	Method ChangeFontSize:Int( newWidth:Int, newHeight:Int )
		FontWidth = newWidth
		FontHeight = newHeight
	
		Return( 0 )
	End 

	Method DrawText:Int( X:Int, Y:Int, Text:String, CenterX:Bool =False, CenterY:Bool =False, XScale:Float =1.0, YScale:Float =1.0 )
		Local CP:Int
		Local Char:Int
		Local Finder:String
		Local EDTX:Int = X
		Local EDTY:Int = Y
	
		If FontWidth = 0 Then Error "No font loaded please use EdzUpLoadFont first"
	
		If CenterX = True
			EDTX -= ( ( ( Text.Length() *( FontWidth *XScale ) ) /2 ) *PixelScaleX )
		Endif
	
		If CenterY = True Then EDTY -= ( ( FontHeight *YScale ) /2 )*PixelScaleY
	
		For CP=0 To Text.Length()
			Finder = String.FromChar( Text[ CP ] )
			Char = FontString.Find( Finder, 0 )
			If Char>-1
				DrawImage( FontImage, EDTX, EDTY, 0, XScale *PixelScaleX, YScale *PixelScaleY, Char )
			Endif
			
			EDTX += ( ( FontWidth *XScale ) *PixelScaleX )
		Next
	
		Return( 0 )
	End
End

To use this simply have a Global MyFont:EdzUpFontClass = New EdzUpFontClass in your variable declarations and then load the font in OnCreate like this:
GameFont.LoadFont( "font_16.png", 16, 16, 64 )
GameFont.SetFont( " !`#$% '()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_" )
GameFont.ChangeFontSize( 10, 16 )

LoadFont just simply loads the font into the class for use, SetFont will tell the class what characters are in the font and how they are arranged this can be in any order and finally ChangeFontSize will adjust the font size to accomodate the font. Changing the font size is useful when you have a font that when drawn looks like its spaced out a bit on some letters it just bunches them up more and makes it look better.

Another bonus of the font system is you can load more than one font at once into the monkey so you could have as many fonts as you want under different variable names and call them as you want. To render the font you can use the built in function like this:
GameFont.DrawText( X, Y, "MyText", CenterX, CenterY, XScale, YScale )


Hope it helps someone :)

UPDATE:Added scaling to the font system