“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
End

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
		Next

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

		canvas.Clear
		
		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
			Next
		Next

		canvas.SetBlendMode(BlendMode.Additive)
		
		drawString("Old School")

		canvas.Flush()
		canvas.PopMatrix()	

	End
		
	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)
		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

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.

Update:

New version of “Old School” video:

 

3D Stars with Mojo2

As an example of using Mojo2 module in Monkey X, I wrote the 3D stars using Mojo2 module in Monkey X.

In earlier post the stars were made with the old Mojo. Mojo2 has lots of nice features to experiment with.

Below is the source code in full:

Import mojo2

Function Main:Int()
	New MyApp
	Return 0
End
 
Class MyApp Extends App
 
	Const BG_WIDTH:Int = 640
	Const BG_HEIGHT:Int = 480
	Const STARS:Int = 180
	
	Field scaleX:Float, scaleY:Float
	
	Field x:Float[STARS],y:Float[STARS],z:Float[STARS]
 
	Field canvas:Canvas
	
	Method OnCreate()
 
		scaleX = DeviceWidth() / BG_WIDTH
		scaleY = DeviceHeight() / BG_HEIGHT
 
		For Local i:Int = 0 To STARS - 1
			x[i] = Rnd(-50,50)
			y[i] = Rnd(-50,50)
			z[i] = Rnd(50,200)
		Next
		
		canvas = New Canvas()
		
		SetUpdateRate(60)
	End
 
	Method OnUpdate()
			
	End
	
 
	Method OnRender()
		
		Local shade:Float
		
		' Scale the graphics
		canvas.PushMatrix()
		canvas.Scale (scaleX,scaleY)
  
		canvas.Clear()
		
		For Local i:Int = 0 To STARS - 1
			z[i] = z[i] - 1
			If z[i] < 10 Then z[i] = Rnd(50,200)
					
			' shade is 0..1
			shade = 1 - z[i] / 200
			canvas.SetColor shade,shade,shade
			canvas.DrawOval 320 + x[i] / z[i] * 200, 240 + y[i] / z[i] * 200,3,3
		Next
	
		canvas.Flush()
		
		canvas.PopMatrix()
		
	End
 
End Class

Feel free to use this code.