sparcversion = "1.1"

function findobj (where,o,name)
	local searched = {}
	local browse
	local function rec(v,name,path)
		if searched[v] then return end
		searched[v] = true
		if type(v)=="table" then
			local mt = debug.getmetatable(v)
			if mt then rec(mt,name..".{mt}",path) end
			browse(v,path.."."..name)
		elseif type(v)=="userdata" then
			local mt = debug.getmetatable(v)
			if mt then rec(mt,name..".{mt}",path) end
		elseif type(v)=="function" then
			local i = 1
			while true do 
				local n,v = debug.getupvalue(v,i)
				if not n then break end
				if v then rec(v,name.."(UP:"..n..")",path) end
				i = i +1 
			end
		end
	end
	function browse(tab,path)
		searched[tab] = true
		
		for i,v in pairs(tab) do
			if i == o then print("key found at ",path,v) end
			if v == o then print("value found at ",path,i) end
			rec(i,"[["..tostring(i).."<-"..tostring(v).."]]",path)
			rec(v,"["..tostring(i).."]",path)
		end
	end
	
	rec(where,name or "","")
end


-- the new sparc
levelw = 500


do
	local w,h = window.size()
	local legalres = {{640,480},{800,600},{1024,768},{1280,1024}}
	
	local found = false
	local diff = 10000000000
	local closest = 0
	for i,res in ipairs(legalres) do
		if (w==res[1] and h==res[2]) then
			found = true
			break
		end
		local cd = math.abs((w*h)-(res[1]*res[2]))
		if ( cd < diff) then
			diff = cd
			closest = i
		end
	end
	if (not found) then
		window.size(unpack(legalres[closest]))
		window.update()
	end
	
end

window.refsize(window.size())

defaultlevel = "freeplay.lua"
function loadgameworld (file)
	file = file or defaultlevel
	file = system.projectpath().."/levels/"..file
	game = new(Game,file)
end

dofile "scripts/posteffects.lua"

function startgame(ondeath)
	Container.getRootContainer():deleteChilds()
	level = nil
	if EditEntity then EditEntity.deleteAll() end
	game = nil
	menu = nil
	dofile "scripts/res.lua"
	dofile "scripts/entity.lua"
	--dofile "scripts/level.lua"
	dofile "scripts/game.lua"
	
	--TimerTask.new(function () dofile "scripts/rungame.lua" end)
	--
	loadgameworld()
	game.onPlayerDeath = ondeath
	collectgarbage"collect"
end

function startedit()
	if EditEntity then EditEntity.deleteAll() end
	Container.getRootContainer():deleteChilds()
	game = nil
	level = nil
	menu = nil
	postfxinit(0)
	TimerTask.new(function()
		collectgarbage"collect"
		dofile "scripts/res.lua"
		dofile "scripts/entity.lua"
		dofile "scripts/level.lua"
		dofile "scripts/game.lua"
		collectgarbage"collect"
		loadworld()
	end,50)
end

function startmenu()
	if EditEntity then EditEntity.deleteAll() end
	Container.getRootContainer():deleteChilds()
	game = nil
	level = nil
	menu = nil
	collectgarbage"collect"
	dofile "scripts/menu.lua"
end

Keyboard.setKeyBinding("F5",startmenu)	
Keyboard.setKeyBinding("F8",startedit)
Keyboard.setKeyBinding("F9",startgame)
--pcall(startgame)
dofile "scripts/menu.lua"

Timer.set("game",function ()
	if game or level or menu then 
		xpcall(function () (menu or level or game):think() end, 
			function (err)
				print(debug.traceback(err))
				game,level,menu = nil
			end
		)
	end
end,20,3)