--------------------------------------------------- 
-- FBA Tutorials
--
-- Tutorial / 12 / Handling binary resources files
--
--------------------------------------------------- 
--
-- This sample shows you how to handle binary files inside
-- your resources
--
--------------------------------------------------- 

-- open a file from my resources. The "true" param, tell the engine
-- to automatically parse the file in order to split the file in several
-- lines separated by newline
--
openres("testfile.txt", true)

        -- print each splitted line (look at your status panel)
        --
        -- getresline() without params return number of lines of your resource
        -- getresline(0) - returns line number 0 (first line)
        -- getresline(1) - return the second line and so on..
        --
        for l=0, getresline()-1 do
                debug("Line " .. l+1 .. " " .. getresline(l))
        end
        
closeres()

-- now we want to load some binary data from a resource file (let's say for example
-- a map you created with your favourite map editor!)
--
-- openres returns the length of resource
--
-- please note: this time i don't want that FBA will parse the file for splitting in lines
--

local data = {}
local numbytes = openres("map.txt")

for b=0, numbytes -1 do
        car = getres(b, b+1)
        data[b]=car
end

closeres()

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

function onmainloop()

      clear(0,0,0)
      
      x = 0
      y = 0
      for b=0, numbytes-1 do
      
        drawimage(blocks, x * 20, y * 20, data[b]*20, 0, data[b]*20+20, 20)
        
        x = x + 1
        if x > 11 then
                x = 0
                y = y + 1
        end
      end
      
      if (mousedown() > 0) then
        quit()
      end

end
