“Old School” — a Little Monkey X Demonstration

Soon the year 2015 will be on the pages of history. I made today a little demonstration in Monkey X with Mojo2.

The demonstration is called “Old School” — for the good old Amiga days. See the video below:

Below is the source code:

Import mojo2
Import mojo2.glutil
Import brl.databuffer

Function Main()
	New MyApp

Class MyApp Extends App
	Const FONT_HEIGHT:Int = 149

	Global canvas:Canvas
	Global fontDat:DataBuffer
	Global gfxFont:Image
	Global gfxBG:Image
	Global angle:Float[10], lightAngle:Float, lightX:Float, lightY:Float
	Global x:Float, y:Float[10]
	Method OnCreate()

		Local angl:Float
		canvas = New Canvas

		gfxBG = Image.Load("bg.png",.0,.0)
		gfxFont = Image.Load("font.png")
		fontDat = New DataBuffer(95*4*2) ' 95 characters, for each character two 4 byte integers
		fontDat = DataBuffer.Load("monkey://data/font.dat")
		For Local i = 0 To 9
			angle[i] = angl
			angl = angl + 18

		canvas.SetAmbientLight .2,.3,.7
	Method OnUpdate()
		For Local i = 0 To 9
			y[i] = (480 - FONT_HEIGHT) / 2 + Sin(angle[i]) * 32
			angle[i] = angle[i] + 5
		lightX = Cos(lightAngle) * 150 + 640 / 2
		lightY = Sin(lightAngle) * 150 + 480 / 2
		lightAngle = lightAngle + 1
	Method OnRender()

		canvas.SetLightType 0,1
		canvas.SetLightColor 0,.7,.7,.9
		canvas.SetLightPosition 0,lightX,lightY,-100
		canvas.SetLightRange 0,200
		For Local sx:=0 Until DeviceWidth Step 128
			For Local sy:=0 Until DeviceHeight Step 128	
				canvas.DrawImage gfxBG,sx,sy

		drawString("Old School")


	Function drawString(text:String)
		Local len = text.Length()
		Local chrs:Int[]
		chrs = text.ToChars()
		x = (640 - stringWidth("Old School")) / 2
		For Local i = 0 To len - 1
			canvas.DrawRect(x, y[i], fontDat.PeekInt((chrs[i]-32)*4*2 + 4), FONT_HEIGHT, gfxFont, fontDat.PeekInt((chrs[i]-32)*4*2), 0, fontDat.PeekInt((chrs[i]-32)*4*2 + 4), FONT_HEIGHT)
			x = x + fontDat.PeekInt((chrs[i]-32)*4*2 + 4)


	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)
		Return length

End Class

Some words about the source…

The bitmap font is made with my Font 2 PNG. On the Font 2 PNG page is explained the file structure of the font.dat file.

The new thing to me was to try the lightning effect with Mojo2. In the code to variable gfxBG is loaded one image, but in order to get the lightning effect working, one needs to make 3 versions of the image. All those three images are used while rendering the effect. The naming of the image files goes like this: name.png, name_n.png and name_s.png.

The letter “n” stands for normal, “s” for specular. These are map files for the lightning. That’s all I know from the documentation of Monkey X Pro.

Feel free to use the code above as you wish.


New version of “Old School” video:


%d bloggers like this: