Old School III – Another sine wave scroller made in Monkey X

Another sine wave scroller made in Monkey X. This one is more traditional..

In the video below the movement is a bit smoother since I used 15Hz update rate and fastened the video by 4x.

Below is the source code:

Import mojo2
Import brl.databuffer

Function Main()
	New MyApp
End

Class MyApp Extends App
	
	Const FONT_HEIGHT:Int = 135
	Const SCROLLTEXT:String = "                                   Old School III - Another sine wave scroller made in Monkey X with Mojo2... This video demonstrates the source code available at my site...                        "

	Global canvas:Canvas
	Global fontDat:DataBuffer
	Global gfxFont:Image
	Global gfxBG:Image
	Global chrStartOffset:Int
	Global firstChrWidth:Int
	Global chrEndOffset:Int

	Global lightAngle:Float, lightX:Float, lightY:Float
	
	Global x:Float
	
	Method OnCreate()
		
		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")

		canvas.SetAmbientLight .2,.3,.7
				
		x = 0
		chrStartOffset = 0
		firstChrWidth = charWidth(0)
		chrEndOffset = getTextEndOffset()
		
	End
	
	Method OnUpdate()
		
		lightX = Cos(lightAngle) * 150 + 640 / 2
		lightY = Sin(lightAngle) * 150 + 480 / 2
		
		lightAngle = lightAngle + 1
		
		' If the amount of scrolling is at least the width of the first
		' character to be drawn, increase the starting index where the next
		' character will be drawn by one and handle the end of the string
		If x <= -firstChrWidth Then

                        x = firstChrWidth + x

			chrStartOffset = chrStartOffset + 1
			
			If chrStartOffset > SCROLLTEXT.Length - 1 Then chrStartOffset = 0
			
			firstChrWidth = charWidth(chrStartOffset)
			chrEndOffset = getTextEndOffset()
			
			If chrEndOffset > SCROLLTEXT.Length - 1 Then chrEndOffset = 0
			
		Endif

		x = x - 4	' scrolling speed
				
	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)

		' draw string to position x
		drawString(x)
		
		canvas.Flush()
		canvas.PopMatrix()	

	End
	
	' This function draws text with sine wave from indexes
	' chrStartOffset to chrEndOffset of the scrolltext string
	Function drawString(x:Int)
		Local angle:Int
		Local len = SCROLLTEXT.Length()
		Local iii:Int = 0, xx:Float
		Local chrs:Int[]
		chrs = SCROLLTEXT.ToChars()
		
		xx = x
		
		For Local i = chrStartOffset To chrEndOffset
			For Local ii = 0 To fontDat.PeekInt((chrs[i]-32)*4*2 + 4) - 1
				canvas.DrawRect(xx, Sin(angle) * 32 + (480 - FONT_HEIGHT) / 2, 1, FONT_HEIGHT, gfxFont, fontDat.PeekInt((chrs[i]-32)*4*2)+ii, 0, 1, FONT_HEIGHT)
				angle = xx * 1.2
				xx = xx + 1
			Next
		Next
	End

	' get character's width in pixels
	Function charWidth:Int(offset:Int)
		Local chrs:Int[]
		chrs = SCROLLTEXT.ToChars()
		Return fontDat.PeekInt((chrs[offset]-32)*4*2 + 4)
	End
	
	Function getTextEndOffset:Int()
		Local chrs:Int[]
		Local c:Int = chrStartOffset
		Local textWidth:Int = 0
		
		chrs = SCROLLTEXT.ToChars()
		
		' Find the end offset for a character to build a string
		' that's width in pixels is at least 640 + first character's width
		While textWidth < 640 + firstChrWidth
			textWidth = textWidth + charWidth(c)
			c = c + 1

			If c > SCROLLTEXT.Length - 1 Then c = 0
			
		Wend
		
		Return c
		
	End
	
End Class

 

The idea behind this version is that each character is drawn with width of 1 pixel at y-coordinate that is calculated with sin from the screen x-position.

See the Font 2 PNG page for info about font.png and font.dat files.

Again, feel free to use and experiment with this code!

 

%d bloggers like this: