SCREEN_WIDTH = 480
SCREEN_HEIGHT = 272

SCREEN_WIDTH2 = SCREEN_WIDTH / 2
SCREEN_HEIGHT2 = SCREEN_HEIGHT / 2

A_SPEED = 3
S_BORDER = 9

NILFUNC = function() end

PI2 = math.pi*2

-- force complete garbage collection
-- (sometimes important when launched repeatedly from lowser)
MusicPlayer = nil
Vera = nil
Crystal = nil
imgs = nil
sfx = nil
collectgarbage()
----

imgs =  {
	border = {
		corner = Image.load("imgs/bcorner.png"),
		hor = Image.load("imgs/bhor.png"),
		vert = Image.load("imgs/bvert.png")
	},
	window = {
		bg = Image.load("imgs/windowbg.png"),
		border = Image.load("imgs/windowborder.png"),
		fadeleft = Image.load("imgs/windowfadeleft.png"),
		faderight = Image.load("imgs/windowfaderight.png"),
	},
	crysts = {
		Image.load("imgs/crys.png"),
		Image.load("imgs/crys2.png"),
		Image.load("imgs/crys3.png")
	},
	crystboss = Image.load("imgs/crysboss.png"),
	alis = Image.load("imgs/alis.png"),
	hasty = Image.load("imgs/hasty.png"),
	bosshit = Image.load("imgs/bosshit.png"),
	scp = Image.load("imgs/scp.png"),
	title = Image.load("imgs/title.png"),
	turbobarbg = Image.load("imgs/turbobarbg.png"),
	stopbarbg = Image.load("imgs/stopbarbg.png"),
	timebarbg = Image.load("imgs/timebarbg.png"),
	nbombicon = Image.load("imgs/nbombicon.png"),
	nbombnuke = Image.load("imgs/nbombnuke.png"),
	texthelp = Image.load("imgs/texthelp.png")
}

sfx = {
	breakfiber = Sound.load("sfx/circlebreak.wav"),
	closecircle = Sound.load("sfx/closecircle.wav"),
	dirchange = Sound.load("sfx/dirchange.wav"),
	menumove = Sound.load("sfx/menumove.wav"),
	nbomb = Sound.load("sfx/nbomb.wav"),
	menuselect = Sound.load("sfx/menuselect.wav")
}

dofile("src/colors.lua")
dofile("src/misc.lua")
dofile("src/circle.lua")
dofile("src/fibers.lua")
dofile("src/alis.lua")
dofile("src/crysts.lua")
dofile("src/fonts.lua")
dofile("src/timing.lua")
dofile("src/menu.lua")
dofile("src/game.lua")
dofile("src/music.lua")
dofile("src/globalcallbacks.lua")
dofile("src/window.lua")
dofile("src/difficulty.lua")
dofile("src/levels.lua")
dofile("src/effects.lua")
dofile("src/textentry.lua")
dofile("src/highscores.lua")

Highscores:load()

GlobalCallbacks:register(function(ctrl)
	if ctrl:select() then
		local i, dir, fn = 0, System.listDirectory(), ""
		repeat
			i = i+1
			fn = "screenshot"..i..".png"
		until not dirContains(dir, fn)
		screen:save(fn)
	end
end)

GlobalCallbacks:register(function(ctrl)
	if ctrl:start() and not(ctrl:l() and ctrl:r()) then
		MusicPlayer:togglePause()
		Window:show(80, nil, function()
				Crystal:print("Game Paused", SCREEN_WIDTH2, SCREEN_HEIGHT2-30, Font.CENTER)
				Vera:print("Quit Crystalise?\nCircle to exit, X to continue.", 
					SCREEN_WIDTH2, SCREEN_HEIGHT2+4, Font.CENTER)
			end, 
			function()
				local ctrl = Controls.read()
				if ctrl:circle() then 
					globalquit = true
					Window:close()
				end
				if ctrl:cross() then Window:close() end
			end)
		MusicPlayer:togglePause()
	end
end)

GlobalCallbacks:register(function(ctrl)
	if ctrl:l() and ctrl:r() and ctrl:start() then
		MusicPlayer:togglePause()
		Window:show(120, nil, function()
				Crystal:print("Debug Mode", SCREEN_WIDTH2, SCREEN_HEIGHT2-50, Font.CENTER)
				Vera:print("Start to enable/disable USB,\nSquare/Triangle = 222/333 mhz\nL/R in/decrease Level ("..Game.levelnum..").\n\n Circle to restart, X to continue.", 
					SCREEN_WIDTH2, SCREEN_HEIGHT2-10, Font.CENTER)
			end, 
			function()
				local ctrl = Controls.read()
				if ctrl:start() then
					System.usbDiskModeActivate()
					repeat System.sleep(150) until Controls.read():start()
					System.usbDiskModeDeactivate()
					System.sleep(300)
				end
				if ctrl:circle() then
					globalquit = true
					Window:close()
					dofile("index.lua")
				end
				if ctrl:triangle() then
					System.setClock(333)
				end
				if ctrl:square() then
					System.setClock(222)
				end
				if ctrl:r() then
					Game.levelnum = Game.levelnum+1
					System.sleep(300)
				end
				if ctrl:l() and Game.levelnum > 0 then
					Game.levelnum = Game.levelnum-1
					System.sleep(300)
				end
				if ctrl:cross() then Window:close() end
			end)
		MusicPlayer:togglePause()
	end
end)

globalquit = false
