-- ME MP3 Library by jsharrad
-- Functions:
-- Memp3.play("filename.mp3")
-- Memp3.stop()
---------------------------------------
--   ID3 TAGS ARE NOT SUPPORTED YET  --
---------------------------------------
-- if your mp3 has a tag, it won't play
--
-- subdirs are allowed, just use
--    Memp3.play("subdir/filename.mp3")


music = loadlib ("memp3", "init")
if music then music() end

size = 200
zMax = 5
speed = 0.1

width = 480
height = 272

starfield = {}
math.randomseed(os.time())

function createStar(i)
	starfield[i] = {}
	starfield[i].x = math.random(2*width) - width
	starfield[i].y = math.random(2*height) - height
	starfield[i].z = zMax
end

for i = 1, size do
	createStar(i)
	starfield[i].z = math.random(zMax)
end

white = Color.new(255, 255, 255)
black = Color.new(0, 0, 0)

Memp3.play("test.mp3")

while true do
	screen:clear(black)
	for i = 1, size do
		starfield[i].z = starfield[i].z - speed
		if starfield[i].z < speed then createStar(i) end
		x = width / 2 + starfield[i].x / starfield[i].z
		y = height / 2 + starfield[i].y / starfield[i].z
		if x < 0 or y < 0 or x >= width or y >= height then
			createStar(i)
		else
			screen:pixel(x, y, white)
		end
	end
	screen:print(0, 0, " - PSP Media Engine MP3 Playback library beta by jsharrad -", white)
	screen:print(40, 10, " - O to start playing mp3, X to stop playing mp3 - ", white)
	screen:print(272, 264, "Starfield for PSP by Shine", white)
	-- Memp3.play("filename.mp3") to load the file and start playing.
	if Controls.read():circle() then Memp3.play("test.mp3") end
	-- Memp3.stop() to stop the ME decoding the file.
	if Controls.read():cross() then Memp3.stop() end
	screen.waitVblankStart()
	screen.flip()
end
