lum = Image.load("lum.png")
link = Image.load("./link.png")
hyperion = Font.load("hyperion.ttf")
hyperion2 = Font.load("hyperion.ttf")
hyperion:setPixelSizes(0, 18)
hyperion2:setPixelSizes(43,50)

red=Color.new(255,0,0)
blue=Color.new(0,0,255)
white=Color.new(255,255,255)

dofile("tablesave.lua") -- needed to load map data
map = {}     -- stores map data
map = table.load("projects/demo/demo.map")
menuitem = 1
loop = true
tiles = {}     -- stores tile images and filenames

-- map x,y position
xpos = 1 
ypos = 1 

-- offsets to keep the map x,y position on screen
Xoffset = 0 
Yoffset = 0

-- screen position
scposX=(((xpos*map.tilesize)-map.tilesize)+Xoffset)
scposY=(((ypos*map.tilesize)-map.tilesize)+Yoffset)

--counters
xc = 1
yc = 1

-- begin function defs
function getVP()
-- set up map viewport
	vpx = math.floor(500 / map.tilesize) + 1 -- width of map viewport
	vpy = math.floor(300 / map.tilesize) + 1 -- height of map viewport

-- start and end positions for map scanner
	Xstart = xpos - math.floor(vpx/2)
	if Xstart < 1 then Xstart = 1 end
	Xend = Xstart + vpx
	if Xend > map.xsize then
		Xend = map.xsize
		Xstart = Xend - vpx
	end
	Ystart = ypos - math.floor(vpy/2)
	if Ystart < 1 then Ystart = 1 end
	Yend = Ystart + vpy
	if Yend > map.ysize then
		Yend = map.ysize
		Ystart = Yend - vpy
	end
end


function drawmap() -- draw map
getVP()
--scan and draw map area
Image.startBlit()
	for i  = Xstart, Xend do
		for j = Ystart, Yend do
			if map[i][j].tile ~= nil
				then screen:fastBlit(((i*map.tilesize)+Xoffset)-map.tilesize, ((j*map.tilesize)+Yoffset)-map.tilesize, map.tileimg[map[i][j].tile])
			end
		end
	end
Image.endBlit()
end

function drawoverlay() -- draw map overlay
--Image.startBlit()
	for i  = Xstart, Xend do
		for j = Ystart, Yend do
			if map[i][j].otile ~= nil
				then screen:blit(((i*map.tilesize)+Xoffset)-map.tilesize, ((j*map.tilesize)+Yoffset)-map.tilesize, map.tileimg[map[i][j].otile], true)
			end 
		end
	end
--Image.endBlit()
end


-- load images from tiles folder
function loadtiles()
	screen:clear()
	for i = 1, table.getn(map.tilename) do
      screen:fontPrint(hyperion, 100, 100, "loading tiles", red)
      screen:flip()
      screen:clear()
      map.tileimg[map.tilename[i]] = Image.load("./tiles/"..map.tilename[i])
	end
	screen:fontPrint(hyperion, 100, 150, "done", blue)
	screen:flip()
   for i = 1, 100000 do end
   rotate = nil
	return
end	

-- end of functions




-- start menu screen


while loop do 
	screen:clear()
	screen:blit(0, 0, lum)
	
   if menuitem==1 then screen:fontPrint(hyperion, 10, 150,"Start demo", red) 
		else screen:fontPrint(hyperion, 10, 150,"Start demo", white)
	end

	if menuitem==2 then screen:fontPrint(hyperion, 10, 180,"Quit demo", red) 
		else screen:fontPrint(hyperion, 10, 180,"Quit demo", white) 
	end

	pad = Controls.read()
	if pad:up() then menuitem=1 end
	if pad:down() then menuitem=2 end
	if pad:start() then loop=false oldpad = pad end
	screen:waitVblankStart()
	screen:flip( )
   
end

if menuitem == 2 then break end


-- start demo
if menuitem==1 then 
   loadtiles()

   loop = true
   while loop do
      pad = Controls.read()
-- move link
      if pad:left() and pad ~= oldpad and xpos > 1 and map[xpos-1][ypos].object ~= 1  then xpos = xpos - 1 end
      if pad:right() and pad ~= oldpad and xpos < map.xsize and map[xpos+1][ypos].object ~= 1 then xpos = xpos + 1 end
      if pad:up() and pad ~= oldpad and ypos >1 and map[xpos][ypos-1].object ~= 1 then ypos =  ypos - 1 end
      if pad:down() and pad:down() and pad ~= oldpad and ypos < map.ysize and map[xpos][ypos+1].object ~= 1 then ypos = ypos + 1 end


		scposX=((xpos*map.tilesize)+Xoffset)
		scposY=((ypos*map.tilesize)+Yoffset)

		if scposX > (240+(map.tilesize/2)) 
			and ((map.xsize*map.tilesize)-(xpos*map.tilesize)) > (240-map.tilesize) 
			then Xoffset = Xoffset - map.tilesize end
		if scposY > (136+(map.tilesize/2))
			and ((map.ysize*map.tilesize)-(ypos*map.tilesize)) > (136-map.tilesize)
			then Yoffset = Yoffset - map.tilesize end
		if scposX < 240-(map.tilesize/2)
			and (xpos*map.tilesize) > (240-(map.tilesize/2)) 
			then Xoffset = Xoffset + map.tilesize end
		if scposY < 136-(map.tilesize/2)
			and (ypos*map.tilesize) > (136-(map.tilesize/2))
			then Yoffset = Yoffset + map.tilesize end

      drawmap()
      screen:blit(scposX-map.tilesize,scposY-map.tilesize,link)
      drawoverlay() 

      screen:print(10, 10, "xpos = " .. xpos .. " ypos = "..ypos, white)
      screen:waitVblankStart()
      screen:flip()
      screen:clear()
      if pad:start() and pad ~= oldpad then layer = 0  loop = false end
      oldpad = pad
   end
end
