Scaling Bitmap Font Made with Font 2 PNG in Monkey X part 2

Just a quick update to old code, this one can be compiled to Android target.

Source code copyright: public domain.

Import mojo
Import brl.databuffer


Function Main()
	New MyApp
End

Class MyApp Extends App
	
	Const FONT_HEIGHT:Float = 49

	Field gfxBG:Image
	Global angle:Float	
	Global fontDat:DataBuffer
	Global gfxFont:Image

	Global textScale:Float
	
	Method OnCreate()

		gfxFont = LoadImage("font.png")
		
		fontDat = New DataBuffer(95*4*2) ' 95 characters, for each character two 4 byte integers
		fontDat = DataBuffer.Load("monkey://data/font.dat")
				
		gfxBG = LoadImage("bg.png")
		
		SetDeviceWindow(1024,768,0)
		
		angle = 180
		SetUpdateRate(60)
	End
	
	Method OnUpdate()
		textScale = (2 - Cos(angle))
		angle = angle + 4
	End
	
	Method OnRender()
		SetBlend AlphaBlend
		SetAlpha 1
		DrawImage gfxBG,0,0
		SetBlend AdditiveBlend
		'                             x-coordinate                                         y-coordinate                           scale
		drawString("Sample Text", (1024 - (stringWidth("Sample Text") * textScale)) / 2, (768 - (FONT_HEIGHT * textScale)) / 2, textScale)
		
		
	End
	
	Function drawString(text:String, x:Float, y:Float,scale:Float)
		Local len = text.Length()
		
		Local chrs:Int[]
		chrs = text.ToChars()
		
		For Local i = 0 To len - 1
			'                            pos. in font.png                      width in pixels in font.png
			DrawImageRect gfxFont, x, y, fontDat.PeekInt((chrs[i]-32)*4*2), 0, fontDat.PeekInt((chrs[i]-32)*4*2 + 4), FONT_HEIGHT,0,scale,scale,0
			x = x + fontDat.PeekInt((chrs[i]-32)*4*2 + 4) * scale
		Next

	End
	
	Function stringWidth:Int(text:String)
		Local len:Int = text.Length()
		Local chrs:Int[]
		Local length:Int = 0
		
		chrs = text.ToChars()
		
		For Local i:Int = 0 To len - 1
			length = length + fontDat.PeekInt((chrs[i]-32)*4*2 + 4)
		Next
		
		Return length
	End

End Class

Below is a YouTube video of this post:

 

%d bloggers like this: