Font Class for my Font 2 PNG’s fonts in Cerberus X

My studies keep me really busy and tired… Still, I’m working on Cerberus X version of Easy Math Challenge, because there seems to be problem with sound in Monkey2 with 64-bit compilations for Android.

Anyway, here’s direct implementation (earlier I did this for Monkey X) of font class to use with fonts made in my Font 2 PNG for Cerberus X.

The source (the code is directly from my Cerberus X editor, but highlighting isn’t quite right):

Import mojo2
Import brl.databuffer

Class MyFont
	Field maxHeight:Int
	Field fontDat:DataBuffer
	Field gfxFont:Image

Class FontTest Extends App

	Field bg:Image
	Field font1:MyFont
	Field canvas:Canvas
	Method OnCreate:Int()

		bg = Image.Load("cerberus://data/bg.jpg",0,0)
		' -------
		' Font
		' -------
		font1 = New MyFont
		font1.gfxFont = Image.Load("font.png")
		font1.fontDat = New DataBuffer(95*4*2) ' 95 characters, for each character two 4 byte integers
		font1.maxHeight = 51
		font1.fontDat = DataBuffer.Load("cerberus://data/font.dat")		
		canvas = New Canvas()
		Return 0

	Method OnRender:Int()

		' drawString assumes, that we have a canvas called canvas
		drawString(font1,"Sample Text", (640 - (stringWidth(font1, "Sample Text"))) / 2, 60)		
		Return 0
	Method drawString(font:MyFont, text:String, x:Float, y:Float)
		Local len = text.Length()
		Local chrs:Int[]
		chrs = text.ToChars()
		For Local i = 0 To len - 1
			'                          width of a character in font                                          pos. of character in font
			canvas.DrawRect(x, y, font.fontDat.PeekInt((chrs[i]-32)*4*2 + 4), font.maxHeight, font.gfxFont, font.fontDat.PeekInt((chrs[i]-32)*4*2), 0, font.fontDat.PeekInt((chrs[i]-32)*4*2 + 4), font.maxHeight)
			x = x + font.fontDat.PeekInt((chrs[i]-32)*4*2 + 4)
	Method stringWidth:Int(font:MyFont,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
			'                                width of character in font
			length = length + font.fontDat.PeekInt((chrs[i]-32)*4*2 + 4)
		Return length

Function Main:Int()
	New FontTest()
	Return 0

I’m a bit lost for words, but if you find my “poor man’s Font 2 PNG” useful, feel free to use this code.

%d bloggers like this: