--load image
font = Image.load("data/font.png")

Music.playFile( 'data/credits.s3m', true )

credits_bg = Image.load("data/credits_bg.png")
press_start = Image.load("data/menu_points.png")

function char2font(char_input)
	if char_input == '' or char_input==nil then
		--print('empty string or nil')
		return 0
	else
		char = string.byte(char_input)
		if char>=65 and char <=90 then --a..z
			return (char-65)*8+216
		elseif char>=48 and char<=57 then --0..9
			return (char-48)*8+103
		elseif char==33 then -- !
			return 8
		elseif char==34 then -- "
			return 16
		elseif char==37 then -- %
			return 24
		elseif char==38 then -- &
			return 32
		elseif char==39 then -- '
			return 40
		elseif char==40 then -- (
			return 48
		elseif char==41 then -- )
			return 56
		elseif char==44 then -- ,
			return 72
		elseif char==45 then -- -
			return 80
		elseif char==46 then -- .
			return 88
		elseif char==47 then -- /
			return 96	
		elseif char==58 then -- :
			return 184
		elseif char==63 then -- ?
			return 200
		elseif char==35 then -- (c) --> #
			return 208
		else
			return 0
		end
	end
end

--59--height
emptyline = '                                                                               '
spacer = '          - - -          '
srctext = emptyline..
					'All Official Game Content # Copyright to their respective owners, developers, and publishers.'..spacer..
					'PsPLF2 for PSP is Developed by PsPmad (http://PsPmad.t25.com/)'..spacer..
					'Lua Player is currently maintained by Frank Buß (aka Shine) and Joachim Bengtsson (aka Nevyn).'..spacer..
					'This Is Curently Under Development SO Please dont Expect Alot from it'..spacer..
					'Please Contact me at my webb site witch You saw earlier in the credits.'..spacer..
					'Thanks To all that has help in this project, witch is curently me...'..spacer
srctext = string.upper(srctext)
text={}
length=0
layer = {
	credits = Image.createEmpty(480, 272)
}

function scroll()
	posx=creditFrame;
	for i=1,60 do  --render 60 frames
		layer.credits:blit(0+i*8-8,	272/2+ math.sin(posx-i*-.1)*50,	font, char2font(text[i]),	0,8,13,	true)
	end
end

function string2array(inputstring)
	array = {}
	length=string.len(inputstring)
	for i=creditIndex,length do
		table.insert(array, string.sub(inputstring, i, i))
	end
	return array
end

creditIndex = 0
creditFrame = 0
startflash = 0
while not Controls.read():start() do
	creditIndex=creditIndex+1
	creditFrame=creditFrame+.1*-1
	startflash=startflash+1
	
	if creditIndex>length then
		creditIndex = 0
	end
	text = string2array(srctext)
	
	if startflash>10 then
		startflash=0
	end
	screen:clear()
	layer.credits:clear()	

	scroll()
	screen:blit(0,0,credits_bg)
	screen:blit(0,0,layer.credits,0,0,layer.credits:width(), layer.credits:height(), true)
	if startflash>=5 then
		screen:blit(168, 235, press_start,0,216,125, 16, true)
	else
		screen:blit(168, 235, press_start,125,216,125, 16, true)
	end		

	screen.waitVblankStart(2) -- set framerate to 30fps otherwise it may be too fast to reads
	screen.flip()
end
Music.stop()