--------------------------------------------------- 
-- FBA Tutorials
--
-- Tutorial / 18 / Particles and temporary images buffers
--
--------------------------------------------------- 
--
-- This sample shows how to draw particles on screen
-- using a temporary image buffer
--
--------------------------------------------------- 

local tree=loadimage("tree.png")

local buffer = createimage(screenwidth(), screenheight())

rendertarget(buffer)
clear(20,25,104)
drawimage(tree,50,130)

local part={}
local maxparts=200

for c=0, maxparts do
        part[c]={}
        part[c]["x"]=math.random(0, screenwidth())
        part[c]["y"]=math.random(-screenheight(), -20)
end


function onmainloop()
        local nextcol
        
        rendertarget(buffer)
        
        for c=0, maxparts do
                if part[c]["y"] >=1 then
                        
                        nextcol = imagecolor(buffer, part[c]["x"], part[c]["y"] + 1)
                        
                        if (nextcol == getcolor(20,25,104) ) then
                                
                                point(part[c]["x"], part[c]["y"], 20, 25, 104)
                                part[c]["y"] = part[c]["y"] + 1
                        else
                                part[c]["y"]=-1
                                part[c]["x"]=math.random(0, screenwidth())                                
                        end
                        point(part[c]["x"], part[c]["y"], 255, 255, 255)
                else
                        part[c]["y"] = part[c]["y"] + 1
                end
        end

        rendertarget()
        
        drawimage(buffer, 0, 0)
end

function onstylusdown(x,y)
        quit()
end
