--------------------------------------------------- 
-- FBA Tutorials
--
-- Tutorial / 13 / Using gamegrid
--
--------------------------------------------------- 
--
-- This sample shows how to use gamegrids in order to draw a 2d grid
-- filled with different tiles. This is faster than drawing single cells through a loop.
-- In this sample we will draw same grid of Tutorial 12 ;)
--
--------------------------------------------------- 


-- load our blocks image
local blocks = loadimage("blocks.png")

-- create grid and pass it previous image as tiles source
local grid = creategrid(blocks, 12, 16, 20, 20)

-- open map from resources and fill grid blocks
local numbytes = openres("map.txt")

x = 0
y = 0
for b=0, numbytes -1 do
        car = getres(b, b+1)
        gridcell(grid, x, y, car)
        x = x + 1
        if x > 11 then
                x = 0
                y = y + 1
        end
end

closeres()


function onmainloop()

        clear(0,0,0)
        
        --draw the entire grid
        drawgrid(grid, 0, 0, screenwidth(), screenheight())
        
        -- we can scroll the grid with arrows
        if (numkeypress(keyright()) > 0) then
                gridworldx( grid, gridworldx(grid) + 5)
        end
        if (numkeypress(keyleft()) > 0) then
                gridworldx( grid, gridworldx(grid) - 5)
        end
        if (numkeypress(keyup()) > 0) then
                gridworldy( grid, gridworldy(grid) - 5)
        end
        if (numkeypress(keydown()) > 0) then
                gridworldy( grid, gridworldy(grid) + 5)
        end
        
        if (mousedown() > 0) then
                quit()
        end

end
